Go Down

Topic: Running more than one event listeners (Read 532 times) previous topic - next topic

Nethawk41

I've been snoozing the past year when I should have been learning more, thus newbie program. I've seen it before but can't remember a single thing about how to accomplish it. Here I have a LED blink on else if no button is pressed. I'm puzzled on how to make it do this without the delay to read the instruction to write to LED when the push button I have is triggered.

If I failed to explain, I wish to have the LED blink but respond to the push button listener that tells the program to turn the LED on. My program however, will only listen at one moment in the instructions as it runs the loop. How can I solve this?


Code: [Select]
const int ledPin = 13;
const int buttonPin = 53;
int buttonState = 0;

void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop(){  //
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)  {
  digitalWrite(ledPin, HIGH);
  }
else  {
   digitalWrite(ledPin, HIGH);
   delay(500);
   digitalWrite(ledPin, LOW);
   delay(500);
  }
    Serial.println(buttonState);
}


tuxduino

Open the Arduino IDE, go to File => Examples => Digital => Blink without delay.

Go Up