Go Down

Topic: Pushbutton to trigger loop (Read 272 times) previous topic - next topic

Calfskin

Hi all, I am currently a student in high school and have just started using the Arduino Uno microcontroller.

My question is, how do I alter my program so that once a pushbutton is pressed (not held), the entire LED sequence will light up? Right now, the best I can do is that the LED will only be on when the pushbutton is pressed, and goes off when the pushbutton is released.

This is my code so far (just started), how can I change it so that the LEDs will flash indefinitely when the pushbutton is pressed once?
Code: [Select]
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;
int led5 = 9;
int led6 = 8;
int PushButton = 1;
int val = 0;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT); //set the LEDs as outputs
  pinMode(PushButton, INPUT);
}

void loop() {
  val = digitalRead(PushButton); //read pushbutton value
  if (val == HIGH) { //check if input is high
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, HIGH);
  delay(1000);
  }
 
}
 


Thanks alot!

PaulMurrayCbr

Code: [Select]

boolean weAreCurrentlyFlashing = false;
int buttonState;

void loop() {
  int prevButtonState = buttonState;
  buttonState = digitalRead(THE_BUTTON);
  if(prevButtonState==HIGH && buttonState==LOW) {
    weAreCurrentlyFlashing = !weAreCurrentlyFlashing;
  }


  if(weAreCurrentlyFlashing) {
    do_flash_thing_loop();
  }
}
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

Calfskin

So something like this maybe?

Code: [Select]
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;
int led5 = 9;
int led6 = 8;
int PushButton = 1;
int val = 0;
int buttonState;
boolean currentFlash = false;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT); //set the LEDs as outputs
  pinMode(PushButton, INPUT);
}

void loop() {
  int prevButtonState = buttonState;
  buttonState = digitalRead(PushButton);
  if (PushButton == HIGH && buttonState==LOW) {
  currentFlash =! currentFlash;
  }
 
 if(currentFlash) {
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, HIGH);
  delay(1000);
  }
 
}
 

Go Up