Coding sequence with button

So basically I have an assignment in class for arduino coding and we only did two things in class to "help" us get a better understanding of how to code this assignment we have, but we have not learned anything like this and ive been trying for days.

THIS IS WHAT I HAVE TO DO:

Code: The following sequence must be programmed into the Arduino.

Pressing the button will start the sequence.

Sequence 1 – Red on for 3 sec followed by Green on for 3 sec followed by Yellow on for 2 sec. Repeat.

Pressing the button will switch to sequence 2.

Sequence 2 – Red and green flashed alternately for 0.5 sec each for 5 sec followed by Yellow on for 5 sec. Stop

Pressing the button will switch to sequence 3.

Sequence 3 – Red and Green on for 3 sec followed by Yellow flashing on and off at 0.25 sec rate for 5 sec. Repeat.

Pressing the button will switch to sequence 4.

Sequence 4 – All lights flash alternately at a rate of 0.5 sec for 5 sec duration followed by Green on for 3 sec. Stop.

Pressing the button will switch to sequence 5.

Sequence 5 – Red on for 3 sec, then fades for 5 sec, Green on for 3 sec, then fades for 5 sec, followed by Yellow on for 2 sec, then fades for 3 sec, Repeat.

THIS IS THE CODE I HAVE TRIED TO MAKE:

int LEDred=2;
int LEDyellow=3;
int LEDgreen=4;
int button=5;
int buttonstatus=0;


void setup()
{
  pinMode(LEDred, OUTPUT);
  pinMode(LEDyellow, OUTPUT);
  pinMode(LEDgreen, OUTPUT);
  pinMode(button, INPUT);
}
void loop()
{  
  if (buttonstatus == HIGH)
  {
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite (LEDyellow, HIGH);
    delay(5000);
  }
  buttonstatus=digitalRead(button);
  digitalWrite(LEDred, HIGH);
  delay(3000);
  digitalWrite(LEDred, LOW);
  digitalWrite(LEDgreen, HIGH);
  delay(3000);
  digitalWrite(LEDgreen, LOW);
  digitalWrite(LEDyellow, HIGH);
  delay(2000);
  digitalWrite(LEDyellow, LOW);
  
}

AND THIS IS MY CIRCUIT:
https://ibb.co/bsBLvyZ

If someone can help me that would be great because this is due tomorrow on Wednesday jan 13 2021.

Your switch is wired wrong. Here is one correct way:

Oh, you forgot to tell us what it currently does that it shouldn't, or doesn't do that it should. :slight_smile:

Hello,

Have a read through
https://forum.arduino.cc/index.php?topic=719995.0

Then, when you understand that look at this
https://forum.arduino.cc/index.php?topic=720016.0

When you understand how to read buttons properly you can refine it to do what you want.

At the moment your buttons go unread for seconds at a time because of all the delays you have.

What are the other students doing? If you really didn't get enough instruction, the whole class will fail and you'll be Bell curved to a passing grade. :slight_smile:

aarg:
What are the other students doing? If you really didn't get enough instruction, the whole class will fail and you'll be Bell curved to a passing grade. :slight_smile:

I'm not sure what the other students are doing. I just don't understand how to just let sequences of code run and keep looping like Sequence 1 then when i press the button once it moves on to Sequence 2 which stops when the code is done. After that when you press the button it moves on to Sequence 3 and so on. Im sure my code is incorrect because I just need guidance on what to do because we just started this.

Maybe start here

I'd do something like this (after changing the button to connect the pin to Gnd when pressed, and using INPUT_PULLUP instead of INPT

void loop(){
buttonPress = digitalRead (button);
  if (buttonPress == LOW){
    sequence = sequence +1;
    if (sequence == 6){
    sequence =1;
    }
  }
  switch (sequence){
  case 1:
  // do things
  break;
  case 2:
  // do things
  break;
  case 3:
  // do things
  break;
  case 4:
  // do things
  break;
  case 5:
  // do things
  break;
  }
}

The sequence repeats in any case section if a low is not detected when loop restarts.
You could add reading the button during any case section and updating sequence as a way to jump the next case when the current case ends. Lots of ways to get there.

CrossRoads:
I'd do something like this (after changing the button to connect the pin to Gnd when pressed, and using INPUT_PULLUP instead of INPT

void loop(){

buttonPress = digitalRead (button);
  if (buttonPress == LOW){
    sequence = sequence +1;
    if (sequence == 6){
    sequence =1;
    }
  }
  switch (sequence){
  case 1:
  // do things
  break;
  case 2:
  // do things
  break;
  case 3:
  // do things
  break;
  case 4:
  // do things
  break;
  case 5:
  // do things
  break;
  }
}



The sequence repeats in any case section if a low is not detected when loop restarts.
You could add reading the button during any case section and updating sequence as a way to jump the next case when the current case ends. Lots of ways to get there.
int LEDred=2;
int LEDyellow=3;
int LEDgreen=4;
int button=5;
int buttonstatus=0;


void setup()
{
  pinMode(LEDred, OUTPUT);
  pinMode(LEDyellow, OUTPUT);
  pinMode(LEDgreen, OUTPUT);
  pinMode(button, INPUT);
}
void loop(){
buttonPress = digitalRead (button);
  if (buttonPress == LOW){
    sequence = sequence +1;
    if (sequence == 6){
    sequence =1;
    }
  }
  switch (sequence){
  case 1:
    digitalWrite(LEDred, HIGH);
    delay(3000);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(3000);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDyellow, HIGH);
    delay(2000);
    digitalWrite(LEDyellow, LOW);
  break;
  case 2:
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite (LEDyellow, HIGH);
    delay(5000);
    
  break;
  case 3:
  // do things
  break;
  case 4:
  // do things
  break;
  case 5:
  // do things
  break;
  }
}

so that's what i did i am getting a few errors though because i don't really know what i am doing but i did change one of the wires for the button. And i dont really understand where and how to use INPUT_PULLUP OR INPT

i am getting a few errors

and now you have to wait five minutes to post them.

Take your time

TheMemberFormerlyKnownAsAWOL:
and now you have to wait five minutes to post them.

Take your time

im talking about the code CrossRoads sent

...and another wasted 300 seconds

flameprod:
And i dont really understand where and how to use INPUT_PULLUP OR INPT

Your image shows the button wired with a *pull down * resistor so buttonPress will be true/high/1 when button is pressed. If using INPUT_PULLUP (pick one or the other) it would be false/low/0 when button is pressed. INPT is probably an unfortunate misspelling.

It would help, too to add some debouncing and state change detection to the button. IDE >file/examples/digital.

im sure if my teacher only gave us a few lessons on coding simple things like, Push Button and LED, and Fading Light then i shouldn't be so complicated right?

Just needs a couple of small changes to compile

byte sequence = 0;
byte buttonPress;


void setup()
{
  pinMode(LEDred, OUTPUT);
  pinMode(LEDyellow, OUTPUT);
  pinMode(LEDgreen, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}

ok so this is what i have but the code has errors still like Screenshot-2021-01-12-184121 hosted at ImgBB — ImgBB

byte sequence = 0;
byte buttonPress;


void setup()
{
  pinMode(LEDred, OUTPUT);
  pinMode(LEDyellow, OUTPUT);
  pinMode(LEDgreen, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}
void loop(){
buttonPress = digitalRead (button);
  if (buttonPress == LOW){
    sequence = sequence +1;
    if (sequence == 6){
    sequence =1;
    }
  }
  switch (sequence){
  case 1:
    digitalWrite(LEDred, HIGH);
    delay(3000);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(3000);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDyellow, HIGH);
    delay(2000);
    digitalWrite(LEDyellow, LOW);
  break;
  case 2:
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite (LEDyellow, HIGH);
    delay(5000);
   
  break;
  case 3:
  // do things
  break;
  case 4:
  // do things
  break;
  case 5:
  // do things
  break;
  }
}

Why did you leave all these out?

int LEDred=2;
int LEDyellow=3;
int LEDgreen=4;
int button=5;
int buttonstatus=0;

oh mb those got deleted by accident

int LEDred=2;
int LEDyellow=3;
int LEDgreen=4;
int button=5;
int buttonstatus=0;
byte sequence = 0;
byte buttonPress;


void setup()
{
  pinMode(LEDred, OUTPUT);
  pinMode(LEDyellow, OUTPUT);
  pinMode(LEDgreen, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}
void loop(){
buttonPress = digitalRead (button);
  if (buttonPress == LOW){
    sequence = sequence +1;
    if (sequence == 6){
    sequence =1;
    }
  }
  switch (sequence){
  case 1:
    digitalWrite(LEDred, HIGH);
    delay(3000);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(3000);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDyellow, HIGH);
    delay(2000);
    digitalWrite(LEDyellow, LOW);
  break;
  case 2:
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite(LEDred, HIGH);
    delay(500);
    digitalWrite(LEDred, LOW);
    digitalWrite(LEDgreen, HIGH);
    delay(500);
    digitalWrite(LEDgreen, LOW);
    digitalWrite (LEDyellow, HIGH);
    delay(5000);
   
  break;
  case 3:
  // do things
  break;
  case 4:
  // do things
  break;
  case 5:
  // do things
  break;
  }
}

this works but the 1st seq doesnt loop and when i press the button it doesnt do anything

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.