Hi, I can't make this work.
What I want to do is turn a led on with a press of a button and then turn it off with another press.
const int but = 2;
const int led = 5;
int state = 0;
int stateLast = 0;
void setup() {
// put your setup code here, to run once:
pinMode(but, INPUT);
pinMode(led, INPUT);
Serial.begin(9600);
}
void loop() {
state = digitalRead(but);
if (state == HIGH && stateLast == LOW){
digitalWrite (led, HIGH);
}
else if (state == HIGH && stateLast == HIGH){
digitalWrite (led, LOW);
}
stateLast = state;
delay(100);
}