NodeJS Blynk - Code Examples for Basic Tasks (Work in Progress)

9 - Blinking LED Widget from Button Widget

Extrapolate the various timer options for all your timing needs.

  • setInterval(functionCall(), timeInMillis); // Repeating interval

  • setTimeout(functionCall(), timeInMillis); // Run function once in x amount of time

  • myTimerID = setInterval(functionCall(), timeInMillis); // Use IDs for use with other options like...

  • clearInterval(myTimerID); // stop a normally repeating timer.

And probably more that I haven’t tried yet :stuck_out_tongue_winking_eye:

// Timed function to blink virtual LED
const BlinkButton = new blynk.VirtualPin(26);  // Setup Blinking Button Widget
const BlinkLED = new blynk.VirtualPin(27);  // Setup Blinking LED Widget
var intervalId;  // Assign this name to a variable

BlinkButton.on('write', function(param) {  // Watches for virtual Blinking Button
  if (param == 1) {  // If Button OFF
		run();  // Start the timer
		} else if (param == 0) {  // If Button ON
			stop()  // End the timer
			}
});


function run() {
	intervalId = setInterval(blinkON, 1500);  // Assign an ID to a timer and repeat function call every 1.5 seconds
}

function stop() {
	clearInterval(intervalId);  // Stop any running timer with this ID
}

function blinkON() {
	blynk.virtualWrite(27, 255);  // Virtual Blinking Blue LED Widget ON
	setTimeout(blinkOFF, 750);  // Run function call once in 0.75 seconds
}

function blinkOFF() {
	blynk.virtualWrite(27, 0);  // Virtual Blinking Blue LED Widget OFF
}
1 Like