Struggling with a simple piece of code...

Posted on
  • Right, just so it's out the way... I've only just pick up JS w/ Espruino and I'm kind of struggling with a simple piece of code:

    const gnd = digitalWrite(B3, 0);
    const pwr = digitalWrite(B4, 1);
    const cw = 1;
    const ccw = 0;
    const dataCW = [];
    const dataCCW = [];
    const dataLength = 36;
    
    
    const getData = (dir, t) => {
    	const solarIn = () => analogRead(A5);
    	const dataRead = () => ((solarIn() * 65536) * 5.0) / 65536;
    	const dataPush = (data) => {
    		data.push(dataRead());
    	};
    
    	const peekVal = () => {
    		if(dir === 1) {
    			for(let i = 0; i < dataLength; i++) {
    				setTimeout(() => {
    					dataPush(dataCW);
    					if(dataCW.length === dataLength) {
    						let peekValCW = Math.max.apply(null, dataCW);
    						let peekValPosCW = () => dataCW.indexOf(peekValCW);
    						return peekValPosCW();
    					}
    				}, i * (t / dataLength));
    			}
    		}
    		else if(dir === 0) {
    			for(let i = 0; i < dataLength; i++) {
    				setTimeout(() => {
    					dataPush(dataCCW);
    					if(dataCCW.length === dataLength) {
    						let peekValCCW = Math.max.apply(null, dataCCW);
    						let peekValPosCCW = () => dataCCW.indexOf(peekValCCW);
    						return peekValPosCCW();
    					}
    				}, i * (t / dataLength));
    			}
    		}
    	};
    	return peekVal();
    };
    
    
    getData(cw, 1000);
    
    

    All I'm trying to do atm is to read a value from a POT(mimicking a future sensor) which then gets stored in an array depending on the two inputs which then would get the position in that array of the greatest value stored... now, all I want is to be able to "see"(print) that value-position, then store it under another const/ let and do something with it...
    In Python this would be something simple like:

     //to see/ print the data for visualisation purposes only:
    print(getData(cw, 1000)) 
    //to store it under another variable:
    var  =  getData(cw, 1000)
    
    

    Now the printing issue is kind of solved in the sense that I can just print the return of that conditional:

    //line 25 or 37
    print(peekValPosCW());
    
    

    But if I'm doing it from where I'm calling the function it's giving me "undefined"... now I get the fact that it's printing undefined because it calls for the print much faster than it takes the function to complete and output something out(at least I'm pretty sure that's what's going on)... but just as a test I've tried using a setTimeout on the print of about 3sec and I'm still getting undefined back which only confused me even more...

  • there is no return value from peekVal(), return value from method passed to setTimeout is something completely unrelated

    https://stackoverflow.com/questions/24928846/get-return-value-from-settimeout

    and BTW, I wouldn't consider that as 'simple piece of code', looks more like opposite of that :-)

  • Fri 2021.05.28

    'now, all I want is to be able to "see"(print) that value-position'

    Place L11 analogRead(A5) inside a small snippet, similarly to this example using an accelerometer:

    http://www.espruino.com/ADXL335


    ref: ' I've only just pick up JS w/ Espruino and I'm kind of struggling'

    Okay, @GeorgeM you did clarify indicating new to Espruino, does that include the interpreter language Javascript as well?

    The understanding of 'undefined' needs to be revisited.

    'that it's printing undefined because it calls for the print much faster than it takes the function to complete'

    '(at least I'm pretty sure that's what's going on)'

    The ninth bullet item down the list at:

    http://www.espruino.com/FAQ
    will take you to:
    http://www.espruino.com/FAQ#when-i-type-a-command-why-does-espruino-print-undefined-

    Espruino follows the convention established at MDN:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined
    See explanation below heading Description


    Other useful resource:

    http://www.espruino.com/Tutorials

  • @Robin Thanks for getting back with all the resources... I'll have a look asap; in the meantime I've managed to get closer to what I wanted using callbacks.
    And yes, new to JS as well... seems to be quite different when compared to Python or C/ C++.

  • Sat 2021.05.29

    Although this might be more of a user/author preference, I was taught one path for data in, one path for data out, otherwise there will come the day the trap will spring. Under some circumstances, it may be prudent to have more than one return statment within a function, such as trying a quick hack to test some other code block, but I think you will find that, that technique will lead to more errors and headaches over time.

    ref: L25 L37 L43 return

    Another useful tool using the WebIDE should than not be discovered yet. As simple as adding the line 'debugger;' as the breakpoint, then single step through or roll-over each variable to reveal it's value.

    http://www.espruino.com/Debugger

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Struggling with a simple piece of code...

Posted by Avatar for GeorgeM @GeorgeM

Actions