Home > database >  Raspberry Pi 4: My Led blinks only 8 time when I expect it 16 times
Raspberry Pi 4: My Led blinks only 8 time when I expect it 16 times

Time:12-12

Please, find the below screenshot of my program run, compilation, and result. As you see below from the LED blink numbers, I saw my LED blink only 8 times but I anticipated 16 times. How 16 times: blink every 0.25 s for 4s, this gives a total of 16. What is wrong? am I assuming something wrong?

My code:

 // include onoff to interact with RPi board GPIO pins
 var gpio = require("onoff").Gpio;
 // specify GPIO27 pin as output and we connected and LED  resistor to this pin already 
 var ledPin = new gpio(27,'out');
 // set blinking time for 250 ms; Executing following line alone makes LED flash continuously
 var blinkInterval = setInterval(blinkLedFunction,250) 
 // Now define the blinkLedFunction
 var ledBlink = 0;
 function blinkLedFunction(){
       // Start blinking LED
       // Check if LED presently set to OFF or 0
       if (ledPin.readSync()===0){
             // Then, set this to ON
             ledBlink  ;
             console.log("LED blink " ledBlink)
             ledPin.writeSync(1);
             // else, set it off
      }else {ledPin.writeSync(0);}
}
// set turn off time of LED blinking for 5s
var stopBlinking = setTimeout(stopBlinkingFunction,4000);
// Now define the stopBlinkingFunction
function stopBlinkingFunction(){
       // stop the function 
       clearInterval(blinkInterval);
       // set ledPin to low
       ledPin.writeSync(0);
       // Unexport GPIO to free resources; I still don't know yet. Maybe, we freeing the pin, in this case 27, for use elsewhere
       ledPin.unexport();
       }

CodePudding user response:

It lasts 4 seconds. You call blinkLedFunction every 250 ms. So you call it 16 times. 8 times to switch on the led. 8 times to switch it off. That is 8 "blinks".

Solution: either expect 8 blinks, or wait for 8 seconds, or call blinkLedFunction every 125 ms :D

  • Related