for loop

Posted on
  • Hi Gordon,

    This for loop throws an error:

    for(i=0,j=7;i<10;i++,j++) { console.log('a'); }
    ERROR: Got ',' expected ')' at line 1 col 22
    for(i=0,j=7;i<10;i++,j++) { console.log('a'); }

                      ^
    

    Is working on jconsole.com.

    http://jsconsole.com/?for(i%3D0%2Cj%3D7%­3Bi%3C10%3Bi%2B%2B%2Cj%2B%2B)%20%7B%20co­nsole.log(%27a%27)%3B%20%7D

    Greetings

    Sacha

  • Hmm - thanks. It should be an easy fix.

  • Ok - fixed for 1v61 :)

  • Thank you.

  • If a js code breaks from a for loop as follows, the loop index i should not be changed!

    for(var i=0; i<10; i++) if(i==3)break; console.log(i)

  • Thanks for letting me know. I've just fixed this, so it'll be in the 1v71 release of Espruino.

  • do you know how to use this for loop similar to way like Arduino ?
    eg.

    for(int i=0 ; i<10 ; i++){
        digitalWrite(13, HIGH);
        delay(500);
        digitalWrite(13, LOW);
        delay(500);
    }
    

    I was trying to use tutorial from buttons example, and change it a bit, like when I press the button LED1 will blink let say twice or triple times, in arduino modification it is easy, how to do it in espruino?

  • Delay is not a good way to manage time dependant tasks as it uses all the resources for itself
    You can do it this way:

    var maxTime = 500;
    var maxStep = 10;
    var count = 0;
    var timer;
    var state = false;
    var blinkActive = false;
    
    function blink(){
      state = !state;
      digitalWrite(LED1,state);
      if (!state) count--;
      if (count===0){
        clearInterval(timer);
        blinkActive = false;
      }
    }
    
    function launchBlink(){
      if(blinkActive === false){
        count = maxStep;
        timer = setInterval(blink,maxTime);
        blinkActive = true;
      }
    }
    
    function onInit(){
      clearInterval();
      LED1.write(0);
      setWatch(launchBlink,BTN,{repeat: true, edge: 'rising', debounce: 100});
    }
    
    onInit();
    
  • delay() is a scourge upon Arduino, and a plague that we are largely free from over here in Espruino-land. delay() is horribly overused on Arduino, and it's only simpler for very basic cases - as soon as you start making things more complicated, you end up in a mess.

    We have modern, sensible tools for non-blocking delays - setInterval(), setTimeout(), and setWatch() (for reacting to a pin change).

    All of those tasks on Arduino would either block, or require a "blinkWithoutDelay" type approach

    
    unsigned long lastBlink=0;
    unsigned long turnOnLedAt=30000;
    byte led1st=0;
    byte led2st=0;
    [#define](http://forum.espruino.com/sear­ch/?q=%23define) ledpin 13 
    [#define](http://forum.espruino.com/sear­ch/?q=%23define) ledpin2 14 //note that there's no led connected here on normal arduino board
    
    void loop() {
    if (millis()+500 > lastBlink) {
    led1st=!led1st;
    digitalWrite(ledpin,led1st);
    }
    if (millis>turnOnLedAt && led2st==0) {
    led2st=1;
    digitalWrite(ledpin2,led2st);
    }
    }
    
    

    vs Espruino:

    
    ledpin1 = LED1;
    ledpin2 = LED2;
    var ledst=0;
    function onInit () {
    setTimeout("digitalWrite(ledpin2)",30000­); 
    setInterval("ledst=!ledst;digitalWrite(l­edpin1,ledst);",500);
    }
    
    

    or

    
    var ledst=0;
    ledpin1 = LED1;
    ledpin2 = LED2;
    function onInit () {
    setTimeout(function() {digitalWrite(ledpin2);},30000); 
    setInterval(function() {ledst=!ledst;digitalWrite(ledpin1,ledst­);},500);
    }
    
    
  • it's working, now I have equivalent to for loop operations ;), thank you fdufnews
    DrAzzy, your code generate some error

  • Thanks!

    note that I have made a small correction to the code in order to prevent double launch of the blink sequence if it is already active.

  • cool, I had some counting problem when disconnected and connected board again, everything was working till I didn't use "save()" command and turned board off and back on, or disconnected and connected again, will check how it is going on now ;)

    fdufnews - everything is workin now, even after power of and on again the board,
    very helpful ;)

  • I fixed the code so it runs out of the box now - I had just omitted the definitions for the ledpin1/ledpin2 variables because I was illustrating how to write the code (just like how in the Arduino example, I didn't include definitions for the LED pins).

    I also missed the second argument to digitalWrite() in one case.

    Note that sometimes when copy/pasting code from these forums, it inserts non-printing junk characters. These get red-marked in the IDE, remove them where they appear.

  • DrAzzy, thank you, your code is also working now ;)

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

for loop

Posted by Avatar for Sacha @Sacha

Actions