execute code for a specified time in Arduino

const int ledPin =  13; // the number of the LED pin
int ledState = 0; // ledState used to set the LED
unsigned long previousMillis = 0; //will store last time LED was updated
unsigned long interval = 1000; //interval at which to blink (milliseconds)
void setup() {
  pinMode(ledPin, OUTPUT); //set the digital pin as output:

void loop() {
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis; //save the last time you blinked the LED
    //if the LED is off turn it on and vice-versa:
    ledState ^= 1;
    digitalWrite(ledPin, ledState);


Leave a Reply

Your email address will not be published. Required fields are marked *