Trying to press a button and toggle an LED flash at 2Hz until i press the button again

  arduino, c++, led, toggle

i’m trying to learn coding and this has really stumped me so i thought i would ask you lovely people.

basically i’m trying to press a button and have an LED toggle on that is flashing on and off twice in a second, this will be continuous until i press the button again that will turn it off.

here is my code so far.

bool latch = false;

void setup(){
pinMode(1, INPUT);
pinMode(13, OUTPUT);
}
void loop(){
if (digitalRead(1)){
  latch = !latch;
}
if (latch == 1){
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}else{
  digitalWrite(13, LOW)
}
}

Source: Windows Questions C++

LEAVE A COMMENT