3 leds and 3 buttons delay problem please HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Thank you so much Stefan I will try this out tomorrow and sorry for disturbing you I will go through the chapters of the link.

Cheers

As I said in reply #15

Grumpy_Mike:
You need the timing variables buttonMillis, intervalButton and programState to be different for each LED, for example buttonMillis1, buttonMillis2, buttonMillis3.
Or better still use an array if you know how to use them.

So the simples way is to use what you had before but repeat it three times. Each time turning just one LED on and then off when the timer expires. Each time you use a different variable for the timing.

StefanL38:
Asking for code described just by a too short description is in 99,999% of all cases a lazy student that wants others to do his homework.

I rather enjoy doing their homework for them. The assignments are usually fun and easy. I don't have a degree, so corroding the value of having a degree is a win for me. I feel that if the educational system can't police academic fraud, then it doesn't deserve the respect and value that it claims. Although I like to give the teachers a bit of a heads up by writing stuff that there is no way the student could possibly have written, mostly for the lulz, but partly to give them a fair go.

And I rather enjoy undermining these student's education. The value in assignments is actually doing them. People who just want to pass the course without learning anything, I feel, deserve to do exactly that. They can get their sheepskin, and then try to get a job from an employer that will ask them to code up Fizz Buzz, which they won't be able to do.

this code also does not work but can you tell me where to put the serial.begin variable (should it be in loop or setup?)

const byte BUTTON_1=13;
const byte BUTTON_2=12;
const byte BUTTON_3=11;
const byte LED_1=6;
const byte LED_2=5;
const byte LED_3=3;
 
unsigned long buttonPushedMillis;
unsigned long ledTurnedOn1;
unsigned long ledTurnedOn2;
unsigned long ledTurnedOn3;
unsigned long turnOnDelay = 20; 
unsigned long turnOffDelay = 5000;
unsigned long currentMillis; 
bool ledReady1 = false; 
bool ledReady2 = false;
bool ledReady3 = false;
bool ledState1 = false;
bool ledState2 = false;
bool ledState3 = false;
bool buttonPress1 = false;
bool buttonPress2 = false;
bool buttonPress3 = false;
 
void setup() {
 pinMode(BUTTON_1, INPUT_PULLUP);
 pinMode(BUTTON_2, INPUT_PULLUP);
 pinMode(BUTTON_3, INPUT_PULLUP);
 pinMode(LED_1, OUTPUT);
 pinMode(LED_2, OUTPUT);
 pinMode(LED_3, OUTPUT);;
 digitalWrite(LED_1, LOW);
 digitalWrite(LED_2, LOW);
 digitalWrite(LED_3, LOW);
}
 
void loop() {
  
 unsigned long currentMillis = millis(); 
 
 if (digitalRead(BUTTON_1) == LOW) {
  buttonPress1 == true;
  ledReady1 == true;
 }
 if (digitalRead(BUTTON_2) == LOW) {
  buttonPress2 == true;
  ledReady2 == true;
 }
 if (digitalRead(BUTTON_3) == LOW) {
  buttonPress3 == true;
  ledReady3 == true;
 }

 if (buttonPress1) {
   if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
     digitalWrite(LED_1, HIGH);
     ledState1 = true;
     ledTurnedOn1 = currentMillis;
     ledReady1 = false;
   }
 }

 if (buttonPress2) {
  if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
    digitalWrite(LED_2, HIGH);
    ledState2 == true;
    ledTurnedOn2 == currentMillis;
    ledReady2 == false;
  }
 }

 if (buttonPress3) {
  if ((unsigned long)(currentMillis - buttonPushedMillis) <= turnOnDelay) {
    digitalWrite(LED_3, HIGH);
    ledState3 == true;
    ledTurnedOn3 == currentMillis;
    ledReady3 == false;
  }
 }
  
 if (ledState1) {
   if ((unsigned long)(currentMillis - ledTurnedOn1) >= turnOffDelay) {
     ledState1 == false;
     digitalWrite(LED_1, LOW);
   }
 }

 if (ledState2) {
  if ((unsigned long)(currentMillis - ledTurnedOn2) <= turnOffDelay) {
    ledState2 == false;
    digitalWrite(LED_2, LOW);
  }
 }

 if (ledState3) {
  if ((unsigned long)(currentMillis - ledTurnedOn3) <= turnOffDelay) {
    ledState3 == false;
    digitalWrite(LED_3, LOW);
  }
 }
}

PaulMurrayCbr:
I rather enjoy doing their homework for them. The assignments are usually fun and easy. I don’t have a degree, so corroding the value of having a degree is a win for me. I feel that if the educational system can’t police academic fraud, then it doesn’t deserve the respect and value that it claims. Although I like to give the teachers a bit of a heads up by writing stuff that there is no way the student could possibly have written, mostly for the lulz, but partly to give them a fair go.

And I rather enjoy undermining these student’s education. The value in assignments is actually doing them. People who just want to pass the course without learning anything, I feel, deserve to do exactly that. They can get their sheepskin, and then try to get a job from an employer that will ask them to code up Fizz Buzz, which they won’t be able to do.

I mean… yeah, I guess that’s one way to see it. Sometimes I do the question for exercise too. Not for that reason tho lol.

@arduino12_5 setup() function runs ONCE and FIRST, then it runs loop() and whatever function it calls, practically FOREVER. Serial.begin() is a function you want to call only ONCE and BEFORE any serial function.
Take a guess where you need to put it.
Also, as a beginner, the built-in examples in Arduino IDE is godsend. Everything “basic” is covered and very well commented to walk you through line by line. I highly recommend checking them all out.

Guys i am happy to announce that this problem is now solved i took the idea of blink without delay code and modified it here is the code for your reference. All and All thank you for your cooperation and especially @StefanL38's help to me. Stefan i thank you very much for your help and the programming course you have told me to go through.

const int ledPin1 =  6;
const int ledPin2 = 5;
const int ledPin3 = 3;
const int buttonPin1 = 13;
const int buttonPin2 = 12;
const int buttonPin3 = 11;

int programState1 = 0;
int programState2 = 0;
int programState3 = 0;

int buttonState1;
int buttonState2;
int buttonState3;
long buttonMillis = 0;
const long intervalButton = 20;   

long ledMillis = 0;
const long intervalLed = 5000;   

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);   
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);    
  digitalWrite(buttonPin1, HIGH);
  digitalWrite(buttonPin2, HIGH);
  digitalWrite(buttonPin3, HIGH);
}

void loop()
{
  unsigned long currentMillis = millis();
  buttonState1 = digitalRead(buttonPin1);
 
  if (buttonState1 == LOW && programState1 == 0) {
    buttonMillis = currentMillis;
    programState1 = 1;
  }
  else if (programState1 == 1 && buttonState1 == HIGH) {
        programState1 = 0; //reset
  }
  if(currentMillis - buttonMillis > intervalButton && programState1 == 1) {
    programState1 = 2;
    ledMillis = currentMillis;

    digitalWrite(ledPin1, HIGH);
  }

  if(currentMillis - ledMillis > intervalLed && programState1 == 2) {
    programState1 = 0;

    digitalWrite(ledPin1, LOW);
  }
  
  buttonState2 = digitalRead(buttonPin2);
  
  if (buttonState2 == LOW && programState2 == 0) {
    buttonMillis = currentMillis;
    programState2 = 1;
  }
  else if (programState2 == 1 && buttonState2 == HIGH) {
        programState2 = 0; //reset
  }
  if(currentMillis - buttonMillis > intervalButton && programState2 == 1) {
    programState2 = 2;
    ledMillis = currentMillis;

    digitalWrite(ledPin2, HIGH);
  }

  if(currentMillis - ledMillis > intervalLed && programState2 == 2) {
    programState2 = 0;

    digitalWrite(ledPin2, LOW);
  }

  buttonState3 = digitalRead(buttonPin3);
  
  if (buttonState3 == LOW && programState3 == 0) {
    buttonMillis = currentMillis;
    programState3 = 1;
  }
  else if (programState3 == 1 && buttonState3 == HIGH) {
        programState3 = 0; //reset
  }
  if(currentMillis - buttonMillis > intervalButton && programState3 == 1) {
    programState3 = 2;
    ledMillis = currentMillis;

    digitalWrite(ledPin3, HIGH);
  }

  if(currentMillis - ledMillis > intervalLed && programState3 == 2) {
    programState3 = 0;

    digitalWrite(ledPin3, LOW);
  }

  
}

No but guys i just now noticed one more problem they are switching on all right and alos are switching off after 10 seconds but if i switch on another led while one led is already on the first led does not switch off first but they both switch off at once, same if all 3 are switched on at once, i have checked it for errors but nothing could come to me at first sight. Please tell me my mistake. (I am very sorry for the inconvenience caused by me to you). here's the code

const int ledPin1 =  6;
const int ledPin2 = 5;
const int ledPin3 = 3;
const int buttonPin1 = 13;
const int buttonPin2 = 12;
const int buttonPin3 = 11;

int programState1 = 0;
int programState2 = 0;
int programState3 = 0;

int buttonState1;
int buttonState2;
int buttonState3;
long buttonMillis = 0;
const long intervalButton = 20;   

long ledMillis = 0;
const long intervalLed = 10000;   

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);   
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);    
  digitalWrite(buttonPin1, HIGH);
  digitalWrite(buttonPin2, HIGH);
  digitalWrite(buttonPin3, HIGH);
}

void loop()
{
  unsigned long currentMillis = millis();
  buttonState1 = digitalRead(buttonPin1);
 
  if (buttonState1 == LOW && programState1 == 0) {
    buttonMillis = currentMillis;
    programState1 = 1;
  }
  else if (programState1 == 1 && buttonState1 == HIGH) {
        programState1 = 0; //reset
  }
  if(currentMillis - buttonMillis > intervalButton && programState1 == 1) {
    programState1 = 2;
    ledMillis = currentMillis;

    digitalWrite(ledPin1, HIGH);
  }

  if(currentMillis - ledMillis > intervalLed && programState1 == 2) {
    programState1 = 0;

    digitalWrite(ledPin1, LOW);
  }
  
  buttonState2 = digitalRead(buttonPin2);
  
  if (buttonState2 == LOW && programState2 == 0) {
    buttonMillis = currentMillis;
    programState2 = 1;
  }
  else if (programState2 == 1 && buttonState2 == HIGH) {
        programState2 = 0; //reset
  }
  if(currentMillis - buttonMillis > intervalButton && programState2 == 1) {
    programState2 = 2;
    ledMillis = currentMillis;

    digitalWrite(ledPin2, HIGH);
  }

  if(currentMillis - ledMillis > intervalLed && programState2 == 2) {
    programState2 = 0;

    digitalWrite(ledPin2, LOW);
  }

  buttonState3 = digitalRead(buttonPin3);
  
  if (buttonState3 == LOW && programState3 == 0) {
    buttonMillis = currentMillis;
    programState3 = 1;
  }
  else if (programState3 == 1 && buttonState3 == HIGH) {
        programState3 = 0; //reset
  }
  if(currentMillis - buttonMillis > intervalButton && programState3 == 1) {
    programState3 = 2;
    ledMillis = currentMillis;

    digitalWrite(ledPin3, HIGH);
  }

  if(currentMillis - ledMillis > intervalLed && programState3 == 2) {
    programState3 = 0;

    digitalWrite(ledPin3, LOW);
  }

  
}

arduino12_5:
i have checked it for errors but nothing could come to me at first sight.

I didn't go deep but, the same buttonMillis is used throughout. I don't think that's a good idea.

yeah, doug i solved the problem all i had to do was add buttonMillis1 or buttonMillis2 for each led and also ledMillis that did the trick.

guys how do i make the interval time for the led increase if the button is pressed again?

arduino12_5:
guys how do i make the interval time for the led increase if the button is pressed again?

Show us what you have tried.

Oh sorry , i have tried using an if statement to add time to the intervalLed but i don't know how to do it. anyways, here is the code. ( i won't be available for immediate answer for the next 12 hours as it is night time here, so you may have to wait for half a day).

const int ledPin1 =  6;
const int ledPin2 = 5;
const int ledPin3 = 3;
const int buttonPin1 = 13;
const int buttonPin2 = 12;
const int buttonPin3 = 11;

int programState1 = 0;
int programState2 = 0;
int programState3 = 0;

int buttonState1;
int buttonState2;
int buttonState3;
bool buttonTurnedOn1 = false;
bool buttonTurnedOn2 = false;
bool buttonTurnedOn3 = false;
long buttonMillis1 = 0;
long buttonMillis2 = 0;
long buttonMillis3 = 0;
const long intervalButton1 = 20;
const long intervalButton2 = 20;
const long intervalButton3 = 20;
long ledMillis1 = 0;
long ledMillis2 = 0;
long ledMillis3 = 0;
const long intervalLed1 = 2000;
const long intervalLed2 = 10000;
const long intervalLed3 = 10000;
bool ledState1 = false;
bool ledState2 = false;
bool ledState3 = false;

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);   
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);    
  digitalWrite(buttonPin1, HIGH);
  digitalWrite(buttonPin2, HIGH);
  digitalWrite(buttonPin3, HIGH);
}

void loop()
{
  unsigned long currentMillis = millis();
  buttonState1 = digitalRead(buttonPin1);
 
  if (buttonState1 == LOW && programState1 == 0) {
    buttonMillis1 = currentMillis;
    programState1 = 1;
  }
  else if (programState1 == 1 && buttonState1 == HIGH) {
        programState1 = 0; //reset
  }
  if(currentMillis - buttonMillis1 > intervalButton1 && programState1 == 1) {
    programState1 = 2;
    ledMillis1 = currentMillis;

    digitalWrite(ledPin1, HIGH);
    buttonTurnedOn1 = true;
    ledState1 = true;
  }

  if(currentMillis - ledMillis1 > intervalLed1 && programState1 == 2) {
    programState1 = 0;

    digitalWrite(ledPin1, LOW);
    buttonTurnedOn1 = false;
    ledState1 = false;
  }
  
  buttonState2 = digitalRead(buttonPin2);
  
  if (buttonState2 == LOW && programState2 == 0) {
    buttonMillis2 = currentMillis;
    programState2 = 1;
  }
  else if (programState2 == 1 && buttonState2 == HIGH) {
        programState2 = 0; //reset
  }
  if(currentMillis - buttonMillis2 > intervalButton2 && programState2 == 1) {
    programState2 = 2;
    ledMillis2 = currentMillis;

    digitalWrite(ledPin2, HIGH);
    buttonTurnedOn2 = true;
    ledState2 = true;
  }

  if(currentMillis - ledMillis2 > intervalLed2 && programState2 == 2) {
    programState2 = 0;

    digitalWrite(ledPin2, LOW);
    buttonTurnedOn2 = false;
    ledState2 = false;
  }

  buttonState3 = digitalRead(buttonPin3);
  
  if (buttonState3 == LOW && programState3 == 0) {
    buttonMillis3 = currentMillis;
    programState3 = 1;
  }
  else if (programState3 == 1 && buttonState3 == HIGH) {
        programState3 = 0; //reset
  }
  if(currentMillis - buttonMillis3 > intervalButton3 && programState3 == 1) {
    programState3 = 2;
    ledMillis3 = currentMillis;

    digitalWrite(ledPin3, HIGH);
    buttonTurnedOn3 = true;
    ledState3 = true;
  }

  if(currentMillis - ledMillis3 > intervalLed3 && programState3 == 2) {
    programState3 = 0;

    digitalWrite(ledPin3, LOW);
    buttonTurnedOn3 = false;
    ledState3 = false;
  }

  if((buttonTurnedOn1 = true) && ( ledState1 = true)) {

  
  
}

guys please help me add a way in which the led blinks as a warning before the light switches off?

Extend the state machine numbers by how many blinks you want.
Change the intervalButton value in each state with it being long initially when the LED is first turned on and short for the blinking states.
Then simply let it run turning the led on and off on odd or even state numbers until you reach the maximum you want.

Hint, you can tell if a number is odd or even by looking at the least significant bit, isolate that by using

StateNumber & 1

Like all things, just do it with one till you get it working.

I don’t understand what you mean by saying extend the state machine numbers and can you please give me an example of the odd or even number identification.

Serial.print (x);
Serial.print (" is ");
Serial.println (x & 1 ? "Odd" : "Even");

i have tried adding the last piece of code in the program but it does not work as without even pushing the button it starts blinking constantly please tell me what i have done wrong.

const int ledPin1 =  6;
const int ledPin2 = 5;
const int ledPin3 = 3;
const int buttonPin1 = 13;
const int buttonPin2 = 12;
const int buttonPin3 = 11;

int programState1 = 0;
int programState2 = 0;
int programState3 = 0;
int ledOnTime1 = 3000;
int ledOnTime2 = 3000;
int ledOnTime3 = 3000;
int delayValue1 = 200;
int delayValue2 = 200;
int delayValue3 = 200;

int buttonState1;
int buttonState2;
int buttonState3;
bool buttonTurnedOn1 = false;
bool buttonTurnedOn2 = false;
bool buttonTurnedOn3 = false;
long buttonMillis1 = 0;
long buttonMillis2 = 0;
long buttonMillis3 = 0;
const long intervalButton1 = 20;
const long intervalButton2 = 20;
const long intervalButton3 = 20;
long ledMillis1 = 0;
long ledMillis2 = 0;
long ledMillis3 = 0;
const long intervalLed1 = ledOnTime1;
const long intervalLed2 = ledOnTime2;
const long intervalLed3 = ledOnTime3;
bool ledState1 = false;
bool ledState2 = false;
bool ledState3 = false;

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);   
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);    
  digitalWrite(buttonPin1, HIGH);
  digitalWrite(buttonPin2, HIGH);
  digitalWrite(buttonPin3, HIGH);
}

void loop()
{
  unsigned long currentMillis = millis();
  buttonState1 = digitalRead(buttonPin1);
 
  if (buttonState1 == LOW && programState1 == 0) {
    buttonMillis1 = currentMillis;
    programState1 = 1;
  }
  else if (programState1 == 1 && buttonState1 == HIGH) {
        programState1 = 0; //reset
  }
  if(currentMillis - buttonMillis1 > intervalButton1 && programState1 == 1) {
    programState1 = 2;
    ledMillis1 = currentMillis;

    digitalWrite(ledPin1, HIGH);
    buttonTurnedOn1 = true;
    ledState1 = true;
  }

  if(currentMillis - ledMillis1 > intervalLed1 && programState1 == 2) {
    programState1 = 0;

    digitalWrite(ledPin1, LOW);
    buttonTurnedOn1 = false;
    ledState1 = false;
  }
  
  buttonState2 = digitalRead(buttonPin2);
  
  if (buttonState2 == LOW && programState2 == 0) {
    buttonMillis2 = currentMillis;
    programState2 = 1;
  }
  else if (programState2 == 1 && buttonState2 == HIGH) {
        programState2 = 0; //reset
  }
  if(currentMillis - buttonMillis2 > intervalButton2 && programState2 == 1) {
    programState2 = 2;
    ledMillis2 = currentMillis;

    digitalWrite(ledPin2, HIGH);
    buttonTurnedOn2 = true;
    ledState2 = true;
  }

  if(currentMillis - ledMillis2 > intervalLed2 && programState2 == 2) {
    programState2 = 0;

    digitalWrite(ledPin2, LOW);
    buttonTurnedOn2 = false;
    ledState2 = false;
  }

  buttonState3 = digitalRead(buttonPin3);
  
  if (buttonState3 == LOW && programState3 == 0) {
    buttonMillis3 = currentMillis;
    programState3 = 1;
  }
  else if (programState3 == 1 && buttonState3 == HIGH) {
        programState3 = 0; //reset
  }
  if(currentMillis - buttonMillis3 > intervalButton3 && programState3 == 1) {
    programState3 = 2;
    ledMillis3 = currentMillis;

    digitalWrite(ledPin3, HIGH);
    buttonTurnedOn3 = true;
    ledState3 = true;
  }

  if(currentMillis - ledMillis3 > intervalLed3 && programState3 == 2) {
    programState3 = 0;

    digitalWrite(ledPin3, LOW);
    buttonTurnedOn3 = false;
    ledState3 = false;
  }

  if(ledOnTime1 = 2000) {
  digitalWrite(ledPin1, HIGH);
  delay(delayValue1);
  digitalWrite(ledPin1, LOW);
  delay(delayValue1);
  } else {
    digitalWrite(ledPin1, LOW);
  }
}
  if(ledOnTime1 = 2000)

Oops

i don't understand? what should i do?

Compare, not assign.