Different setInterval

Posted on
  • Is it possible to set different intervals?

    I have two operations using setInterval().
    One is to read out a sensor via I²C and on the other hand I would like to let a LED blink, depents on the counter value, e. g. counter == 3 >>> LED should blink 3 times all 10 seconds.

    My problem is that the LED blinks continiously - I tried it with arguments but it does not work.

    ///Sensor_Data        
         setInterval(() => {
         i2c.writeTo(0x18, 0xAA, 0x00, 0x00); 
         setTimeout(() => {
         let output = i2c.readFrom(0x18, 5);
         var output_bit = output[1] *100 *100 + output[2]*100 + output[3];    
         var pressure1 = (output_bit-1200000)*(1-(-1));
         var pressure2 = pressure1/(1500000-1200000);
         var pressure_final = pressure2 + (-1) - 0.29;    
         log(pressure_final, counter);  
    ///Counter    
         if (pressure_final < -0.2) {
         status_counter = 1;
         }
         if ((status_counter == 1) && (pressure_final > -0.2) && (pressure_final < 0.2)) {
         counter = counter + 1;            
           if (counter + 1) {
           status_counter = 0;}
           }
             if (counter > 6){
             counter = 1;
             }
    
  • Sat 2021.04.03

    'Is it possible to set different intervals?'

    Yes

    'LED should blink 3 times all 10 seconds'

    Unsure of intention here ref 'all 10'

    'the LED blinks continiously - I tried it with arguments but it does not work'

    Missing logic for LED control and the 'tried it' code block


    Hi @psc1988 in post #1 code snippet L2, L4, L15, L22, it appears there are mismatched closing curly braces, along with no LED control logic, making comprehension difficult, so I created one possible stand alone code block.


    const BLINK_TIMES = 3;
    const PIN_LED = B4;  // Device dependant - external LED
    
    var doOnce = 1;
    
    // sec
    var durationTest = 12;
    var durationWait = 2;
    
    // msec 
    var delay = 600;  // Arbitrary screen update interval
    var timeout = 3500;
    
    var durationBlink = 40;
    
    
    var timeStart = 0;
    var timeStop  = 0;
    var timeCurr  = 0;
    var timeElapsed = 0;
    
    // http://www.espruino.com/Reference#l__global_getTime
    // Return current system time in Seconds (as a floating point number)
    //>getTime()
    //=946685458.84333419799
    
    
    // Interval timer
    
    var intervalID = {};
    var timeoutID  = {};
    var blinkID    = {};
    
    function ci() { clearInterval( intervalID ); }
    function cb() { clearInterval( blinkID ); }
     
    
    function startTest() {
      var toggle = false;
      var nCount = BLINK_TIMES;
      
      timeStart = getTime();
      timeStop  = timeStart + durationTest;
      
      
      intervalID = setInterval(function () {
    
          timeCurr = getTime();
    
          // When duration of test is over stop the timer
          if ( timeCurr >= timeStop ) { ci(); intervalID = null; }
    
          // Send our write command to our external device
          if( ( timeCurr >= (timeStart + durationWait) )  &&  ( doOnce ) ) {
        
            doOnce--;
        
            // Send a command instruction to external device
            Serial1.write([0xB5,0x62]);   // arbitrary - modify as required
            console.log( "L60     Sending write command to device" );
    
            timeoutID = setTimeout(function () {
    
              // After a specific time, attempt to read that device response
              Serial1.write([0x85]);   // arbitrary - modify both as required
              var dataRecd = getData(); 
              console.log( "L67    Sending read command to device" );
    
    
              blinkID = setInterval(function () {
    
                if ( nCount < 0 ) {  cb(); blinkID = null; }
    
                // ternary operator
                toggle = toggle ? false : true;
    
    //DEBUG:    console.log( "L77  toggle: " + toggle );
                console.log( "L78  State LED: " + ( toggle ? "on" : "off" ) );
    //DEBUG:    console.log( "L79  count: " + nCount + "   toggle: " + toggle );
    
                // Set state of LED - watch pin mode - If LED anode tied hi
                digitalWrite( PIN_LED, toggle );   // a lo will turn on
    
                nCount--;
               }, durationBlink);
          
            console.log( "L87   TERMINATING Test" );
            }, timeout)
    
            // if
            }
    
        console.log( "L93  time: " + getTime() );
        }, delay);
    
    // function
    }
      
    
    startTest();
      
    
    
    // Simple watchdog in case our test loop above runs amuck
    setTimeout(function () {    
       console.log( "L106  WatchDog  TERMINATING   should never get here though" );
       ci();
       cb();
    }, ( (durationTest*1000) + 2000 ) );
    


     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v04 (c) 2019 G.Williams
    >
    L93  time: 946685464.98464584350
    L93  time: 946685465.58461284637
    L93  time: 946685466.18463134765
    L60     Sending WRITE command to device
    L93  time: 946685466.78632259368
    L93  time: 946685467.38473415374
    L93  time: 946685467.98462772369
    L93  time: 946685468.58465099334
    L93  time: 946685469.18467044830
    L93  time: 946685469.78472423553
    L67     Sending READ command to device
    L78  State LED: on
    L78  State LED: off
    L78  State LED: on
    L78  State LED: off
    L78  State LED: on
    L78  State LED: off
    L93  time: 946685470.98464107513
    L93  time: 946685471.58464050292
    L93  time: 946685472.18469142913
    L93  time: 946685472.78465366363
    L93  time: 946685473.38461399078
    L93  time: 946685473.98462963104
    L93  time: 946685474.58469295501
    L87     TERMINATING   Test
    > 
    

    More Info:

    https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals#setinterval

  • @psc1988 have o look at this led sample

    https://www.espruino.com/Flashing+Lights

    it is also possible to use toggle()

    https://www.espruino.com/Reference#l_Pin_toggle

    there are more samples in the forum - use seach in section conversation.

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

Different setInterval

Posted by Avatar for psc1988 @psc1988

Actions