I was playing with the push button tutorial from a kit I got but wanted to play with some other functions. One thing I wanted to play with is pushing two buttons at the same time would start blinking. I got it to do so but it seems like a hack since the loop time is small enough to read the push button changes but if I wanted to change the duration of the blinking, it wouldn't suffice. I know there are interrupt functions but don't know how to use it. How would you suggesting doing this?
Here's what I ended up with for code. The WHILE loop at the end is what I wrote to enable the blinking.
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
const int ledPin = 13; // the number of the LED pin
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int lastState1 = 1;
int lastState2 = 1;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == LOW && buttonState2 == HIGH) {
// turn LED on:
digitalWrite(ledPin, LOW);
lastState1 = 0;
lastState2 = 1;
}
else if (buttonState2 == LOW && buttonState1 == HIGH) {
// turn LED off:
digitalWrite(ledPin, HIGH);
lastState1 = 1;
lastState2 = 0;
}
else if (buttonState1 == LOW && buttonState2 == LOW) {
lastState1 = 0;
lastState2 = 0;
delay(500);
}
while (lastState1 == 0 && lastState2 == 0 && digitalRead(buttonPin1) == HIGH && digitalRead(buttonPin2)==HIGH){
digitalWrite(ledPin, HIGH);
digitalRead(buttonPin1);
digitalRead(buttonPin2);
delay(1000);
digitalWrite(ledPin, LOW);
digitalReady(buttonPin1);
delay(1000);
}
}