Problem letting leds turn on simultaneously, and then letting them turn off

My subject line might be a little vague, so I'll try to explain my goal as clear as possible. I'm new to arduino and working on this project for school.

The goal of my Arduino project is to create "pressure sensitive" tiles. When someone stands on the tile it should light up, and then turn of after a few seconds. For my prototype I am using pushbuttons and leds to test it out.

This was my initial code:

int led_Rood = 13;
int led_Groen = 12;
int led_Geel = 11;
int led_Blauw = 10;
int button_Rood = 2;
int button_Groen = 3;
int button_Geel = 4;
int button_Blauw = 5;
int button_Voet = 6;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;

void setup() {
  pinMode (led_Rood, OUTPUT);
   pinMode (led_Groen, OUTPUT);
    pinMode (led_Geel, OUTPUT);
     pinMode (led_Blauw, OUTPUT);
      pinMode (button_Rood, INPUT);
       pinMode (button_Groen, INPUT);
        pinMode (button_Geel, INPUT);
         pinMode (button_Blauw, INPUT);

}

void loop() {
 buttonState1 = digitalRead(button_Rood);
 buttonState2 = digitalRead(button_Groen);
 buttonState3 = digitalRead(button_Geel);
 buttonState4 = digitalRead(button_Blauw);

 if (buttonState1 == HIGH){
    digitalWrite(led_Rood, HIGH);
 }
 if (buttonState2 == HIGH){
    digitalWrite(led_Groen, HIGH);
 }
 if (buttonState3 == HIGH){
    digitalWrite(led_Geel, HIGH);
 }
 if (buttonState4 == HIGH){
    digitalWrite(led_Blauw, HIGH);
 }

}

Everything worked as I expected afterwards(I was quite proud of that actually). LED's turning on when the corresponding button was pushed. But they won't turn off again. I tried to fix that by putting in a delay for every button press. Like this:

if (buttonState1 == HIGH){
    digitalWrite(led_Rood, HIGH);
    delay(2500);
    digitalWrite(led_Rood, LOW);
 }

When I did this, the light does turn off after 2.5 seconds, but the rest of the code doesn't respond because the delay is still there. So the lights can't be on simultaneously. Can anyone give me advice on how to make sure all lights respond to their button at all times, but have a individual timer as well? I would be very grateful.

The blink without delay example in the IDE (File, Examples, Digital) will show how to use the millis (and micros) functions to do non blocking timing. The several things at a time thread expands on BWOD.

Thank you very much for this information. I will check it out. I'm very new to Arduino and coding, so I didn't even know how to call what I'm looking for. It also made Googling pretty difficult. The hyperlink you added doesn't send me anywhere by the way, but i'll search for it myself. Thanks again!

I fixed the link, should work, now.

To expand @groundfungus suggestion...

The issue you have is delay(), which blocks anything else from happening until the most recent delay() period has elapsed.
With you multiple switches and LEDs, you want to have each switch/LED operating on separate time-lines.

millis() and the links above will do exactly what you want, but you have to apply the tools to your problem.

Good luck.

so I didn't even know how to call what I'm looking for.

It is called a state machine. Read another take on it here http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

Hi,
How have you got your press buttons wired?
If they pull the digital input pin to 5V when pressed, you must have a 10K resistor between the digital input and gnd to pull the inut down when the button is open.

Also use if .. else statements

 if (buttonState3 == HIGH)
 {
    digitalWrite(led_Geel, HIGH);
 }
else
  {
     digitalWrite(led_Geel,LOW);
   }

If you look at your code, you are looking for the button to be HIGH, but not checking if it is LOW to turn your LED OFF.
Tom... :slight_smile:

Hey everyone,

Thanks a lot for the input. Today I had a sit down with one of my more arduino oriented classmates and we figured the whole thing out. For me it was not really clear if I had to define everything for every led. In the end I did, and the result is what I was looking for. I've even incorporated a different tone for every button. Now I have to make sure the tone stops even when the button stays completely pressed the whole time.

My code looks like this now:

int led_Rood = 13;
int led_Groen = 12;
int led_Geel = 11;
int led_Blauw = 10;

int button_Rood = 2;
int button_Groen = 3;
int button_Geel = 4;
int button_Blauw = 5;

int piezo = 8;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;

unsigned long previousMillisRood = 0;
unsigned long previousMillisGroen = 0;
unsigned long previousMillisGeel = 0;
unsigned long previousMillisBlauw = 0;

const long interval = 5000;

unsigned long currentMillisRood = 0;
unsigned long currentMillisGroen = 0;
unsigned long currentMillisGeel = 0;
unsigned long currentMillisBlauw = 0;


void setup() {
  pinMode (led_Rood, OUTPUT);
   pinMode (led_Groen, OUTPUT);
    pinMode (led_Geel, OUTPUT);
     pinMode (led_Blauw, OUTPUT);
      pinMode (button_Rood, INPUT);
       pinMode (button_Groen, INPUT);
        pinMode (button_Geel, INPUT);
         pinMode (button_Blauw, INPUT);
          pinMode (piezo, OUTPUT);

}

void loop() {
 buttonState1 = digitalRead(button_Rood);
 buttonState2 = digitalRead(button_Groen);
 buttonState3 = digitalRead(button_Geel);
 buttonState4 = digitalRead(button_Blauw);
   currentMillisRood = millis();
   currentMillisGroen = millis();
   currentMillisGeel = millis();
   currentMillisBlauw = millis();
  
   if (currentMillisRood - previousMillisRood >= interval) {
    
    digitalWrite(led_Rood, LOW);
  }

   if (currentMillisGroen - previousMillisGroen >= interval) {
    
    digitalWrite(led_Groen, LOW);
  }

   if (currentMillisGeel - previousMillisGeel >= interval) {
   
    digitalWrite(led_Geel, LOW);
  }

   if (currentMillisBlauw - previousMillisBlauw >= interval) {
   
    digitalWrite(led_Blauw, LOW);
  }

 if (buttonState1 == HIGH){
      previousMillisRood = currentMillisRood;
      digitalWrite(led_Rood, HIGH);
      tone(piezo,550,375);

 }
 if (buttonState2 == HIGH){
     previousMillisGroen = currentMillisGroen;
     digitalWrite(led_Groen, HIGH);
     tone(piezo,500,325);
    
 }
 if (buttonState3 == HIGH){
    previousMillisGeel = currentMillisGeel;
    digitalWrite(led_Geel, HIGH);
    tone(piezo,450,225);
  
 }
 if (buttonState4 == HIGH){
    previousMillisBlauw = currentMillisBlauw;
    digitalWrite(led_Blauw, HIGH);
    tone(piezo,400,200);
    
    
 }

}