My TA does not know what is going on. I am trying to modify this code so that the LED is off initially, I press the button once the led comes on, I press it once more, and the LED blinks with one second intervals, then the process repeats.
const int switchPin = 2;
const int ledPin = 13;
int buttonstate;
int lightState;
int blinkstate = LOW;
long previousmillis = 0;
long interval = 1000;
unsigned long currentmillis = 0;
// the setup routine runs once when you press reset:
void setup() {
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
buttonstate = digitalRead(switchPin);
}
void loop() {
int val = digitalRead(switchPin);
delay(10);
int val2 = digitalRead(switchPin);
if (val == val2){
if (val != buttonstate) {
if (val == LOW){
if(lightState == 0){
lightState = 1;
Serial.println("Button just pressed, Blinking");
currentmillis = 0;
previousmillis = 0;
blinkstate = LOW;
}
else{
lightState = 0;
digitalWrite(ledPin,LOW);
Serial.println("Button just pressed, light off");
}
}
}
}
// Blinking Section
if (lightState == 1){
currentmillis = millis();
if (currentmillis - previousmillis > interval)
previousmillis = currentmillis;
if (blinkstate == LOW)
blinkstate = HIGH;
else
blinkstate = LOW;
digitalWrite(ledPin,blinkstate);
}
buttonstate = val;
}
--Thanks