Howdy-
so … made a Green LED turn on with one press of a button. Made an orange LED turn on when that same button was pressed twice. Made both LEDs turn off when the button was pressed a third time.
Here is a video example:
However, after this, I wanted to reset the button count to NIL if no other button presses followed. I tried to unsuccessfully attempt a “millis” into my code. Please see the comments section for that code, and any suggestions are greatly appreciated.
const int GREEN= 7;
const int ORANGE= 4;
const int button= 2;
int buttonPress=0;
int buttonState = 0; // current state of the button
int lastButtonState = 0;
long previousTime=0;
long interval=4000;
unsigned long timeatloop;
void setup(){
pinMode (GREEN, OUTPUT);
pinMode (ORANGE, OUTPUT);
pinMode (button, INPUT);
Serial.begin (9600); //this is the baud rate of the connection time (USB?)
}
//make the buttonPress reset to 0 if left untouched for 4thousand millis
//next; RED led turn on after buttonPress=1, RED blink every 2 thousand millis after buttonPress=2, stay on until 4ths millis after
//uttonPress
void loop(){
buttonState=digitalRead(button);
if(timeatloop- previousTime > interval){
previousTime =timeatloop;
digitalWrite (GREEN, LOW);
}
//when button is pressed, turn on one GREEN and turn off ORANGE
if (buttonState != lastButtonState){
if (buttonState ==HIGH){
buttonPress++;
timeatloop = millis();
}
}
if (buttonPress==1){
digitalWrite(GREEN, HIGH);
}
if (buttonPress==2){
digitalWrite(GREEN, HIGH);
digitalWrite(ORANGE, HIGH);
}
if (buttonPress==3){
digitalWrite(GREEN, LOW);
digitalWrite( ORANGE, LOW);
buttonPress=0;
}
lastButtonState =buttonState;
}