Programming leds

Hello guys,

I stuck with my homework so last chance I came here for help cause my teacher isn't helping so maybe you guys can help me

So my last homework is to make a program when you press the press button the red led goes on and stays on when I press for the second time on the same press button the green led goes on and stays on when I press the third time on the same press button the blue led goes on and stays on and the fourth time I press on the press button the yellow led goes on and the fifth time they blink all

when I press the button all the LEDs go on

I'm using arduino nano

LED RED = code 4
LED GREEN = code 5
LED BLUE = code 6
LED YELLOW= 7
PRESS BUTTON= 8

Welcome to the forum

What have you tried so far ?

You may find the StateChangeDetection example in the IDE helpful in detecting the keypresses together with an array of LED pin numbers

Hello thanks for the quick response i'm little late but this is what I tried but lights up all pins when I press the button

int buttonState = 1;

void setup () {
pinMode(4, OUTPUT);
pinMode(9,INPUT_PULLUP);
pinMode(5, OUTPUT);
pinMode(9, INPUT_PULLUP);
pinMode(6, OUTPUT);
pinMode(9, INPUT_PULLUP);
pinMode(7, OUTPUT);
pinMode(9, INPUT_PULLUP);
}
void loop() {
buttonState = digitalRead(9);
if(buttonState == LOW) {
digitalWrite(4, HIGH);
}
else {
digitalWrite(4,LOW);
}
while(digitalRead(9)==HIGH);
buttonState = digitalRead(9);
if(buttonState == LOW) {
digitalWrite(5, HIGH);
}
else {
digitalWrite(5,LOW);
}
while(digitalRead(9)==HIGH);
buttonState = digitalRead(9);
if(buttonState == LOW) {
digitalWrite(6, HIGH);
}
else {
digitalWrite(6,LOW);
}
while(digitalRead(9)==HIGH);
buttonState = digitalRead(9);
if(buttonState == LOW) {
digitalWrite(6, HIGH);
}
else {
digitalWrite(6,LOW);
}
while(digitalRead(9)==HIGH);
buttonState = digitalRead(9);
if(buttonState == LOW) {
digitalWrite(7, HIGH);
}
else {
digitalWrite(7,LOW);
}
while(digitalRead(9)==HIGH);
}

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.


Show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


FYI

my teacher just send only this to me

Now program the LEDs as follows;

1st press of the push button, the LED lights up red,
2nd press of the push button, the LED lights up green,
3rd press of the push button LED lights up blue,
4th press of the push button, the LED lights up yellow,
5th press of the push button the LEDs will all flash.

Do you know what it means when we say “look at when a switch changes state” means.


“ 5th press of the push button the LEDs will all flash.”
What happens when you pres the 6th time ?

No, my teacher sucks so i don't know it

What have you done about this excellent piece of advice?
It is the key to getting your project to function.

. . .

Did not help me that much

There is no 6th time really in the programming there happens nothing

Read this discussion:

What did you do about it, did you try it? Did you even look at it?
If it did not help you then you will never be able to do your project, do better.

How well did you study the code ?

Did you note that a counter is incremented every time the button becomes pressed ?

Do you know how to flash a LED ?

Did any teacher not teach you how to write in sentences?
Or are they missing on your keyboard?

Yes you are a beginner which is why we are trying to help you, but help is a two way street, it requires some effort on your part.

Have you been playing attention for your other assignments or did you just get someone else to do them for you, like you are trying now?

You have 10 days left, how long was it set before you begin to consider it. I wouldn't take you more than an hour to go through that example and throughly learn what it does. What you are being asked to do is so simple but you seem to not to want to even engage with the problem. Tell me why you think you deserve to pass this homework?

I'm with you regarding teachers that cannot provide a resource(s) but expect you to go cold turkey. Fortunately, your assignment is easy when approached from the tasks:

Consider 4 colored paper cutouts: all have a white backside, but the frontside is different: one is red, one is blue, one is green, and one is yellow. Put the cutouts on a table with all the colors facing down. You have a photographic memory and although you cannot see the colors, you know which disk is which.

Your friend shouts "one" and you turn over the red disk.
Your friend shouts "two" and you turn over the green disk.
Your friend shouts "three" and you turn over the blue disk.
Your friend shouts "four" and you turn over the yellow disk.
Your friend shouts "five" and you jump up, turn off and on the room light a few times and YOU shout back to your friend, "That was fun, let's do it all again!"

You politely thank her for shouting the words. She looks at you funny and says, "I was not shouting words, I was shouting numbers!

You guys decide to play the game again, this time with numbers. She shouts "one" but the phone rings and after taking the call, you politely say, "I'm sorry about the interruption, but I forgot what number you shouted. She becomes very angry with you because you think more of the phone call than playing the game. She knocks all the disks on to the floor and stomps out of the house.

You make a mental note that if there is a next time, you'll use stick-tally so you will not forget where you are in the game.
image

Sure enough, your friend comes over the next day, you play the game. After turning over the 4th disk (yellow) You jump up, rush to the light switch, toggle the switch a few times, get confirmation she wants to play again, you return to the table and tear-off the sheet of paper and turn over the colored disks. You are ready to make your first mark... she shouts "one" and you mark your tally
image

and turn over the red disk. And the game continues ...

In a computer program, a button could be the "shout" and the number shouted could be a program variable that starts at 0 and increments on each press like this:
0 + buttonPress = 1
1 + buttonPress = 2
2 + buttonPress = 3
3 + buttonPress = 4
4 + buttonPress = 5

If we inspect with software the variable buttonPress, then the program logic would be:
buttonPress == 1 then turn on Red LED
buttonPress == 2 then turn on Green LED
buttonPress == 3 then turn on Blue LED
buttonPress == 4 then turn on Yellow LED
buttonPress == 5 (flash LEDs)

turn off all,
turn on all,
turn off all,
set buttonPress = 0,
go back and restart waiting for buttonPress

We would start our sketch by declaring an integer to be a counter

int buttonPress = 0 ; 

Then, a simple change in the loop{} function:

buttonState = digitalRead(9);
  if(buttonState == LOW) {
     buttonPress = buttonPress + 1 ;
     }

Now, we have options like using if()

if (buttonPress == 1) { }

but I prefer to use switch case (if you think your teacher would approve.)

Good luck.

1 Like
#define PRESSED            LOW
#define RELEASED           HIGH

#define LEDon              HIGH
#define LEDoff             LOW

const byte LEDred        = 4;
const byte LEDgreen      = 5;
const byte LEDblue       = 6;
const byte LEDyellow     = 7;

const byte heartbeatLED  = 13;

const byte mySwitch      = 9;

bool flashFlag           = false;

byte lastSwitchState;
byte buttonState         = HIGH;
byte counter;

//timing stuff
unsigned long heartbeatMillis;
unsigned long switchMillis;
unsigned long LEDflashMillis;


//**********************************************************************
void setup ()
{
  pinMode(LEDred, OUTPUT);
  digitalWrite(LEDred, LEDoff);

  pinMode(LEDgreen, OUTPUT);
  digitalWrite(LEDgreen, LEDoff);

  pinMode(LEDblue, OUTPUT);
  digitalWrite(LEDblue, LEDoff);

  pinMode(LEDyellow, OUTPUT);
  digitalWrite(LEDyellow, LEDoff);

  pinMode(heartbeatLED, OUTPUT);

  pinMode(mySwitch,  INPUT_PULLUP);

} //END of setup()


//**********************************************************************
void loop()
{
  //******************************************************
  if (millis() - heartbeatMillis >= 500)
  {
    //restart this TIMER
    heartbeatMillis = millis();

    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //******************************************************
  if (millis() - switchMillis >= 50)
  {
    //restart this TIMER
    switchMillis = millis();

    checkSwitches();
  }

  //******************************************************
  if (flashFlag == true && millis() - LEDflashMillis >= 200)
  {
    //restart this TIMER
    LEDflashMillis = millis();

    digitalWrite(LEDred,    !digitalRead(LEDred));
    digitalWrite(LEDgreen,  !digitalRead(LEDgreen));
    digitalWrite(LEDblue,   !digitalRead(LEDblue));
    digitalWrite(LEDyellow, !digitalRead(LEDyellow));
  }

} //END of   loop()


//**********************************************************************
void checkSwitches()
{
  //******************************************************
  byte switchState = digitalRead(mySwitch);

  //has there been a change in the switch state ?
  if (lastSwitchState != switchState)
  {
    //update to the new state switchState
    lastSwitchState = switchState;

    //*********************
    if (switchState == PRESSED)
    {
      switch (counter++)
      {
        //********
        case 0:
          {
            digitalWrite(LEDred, LEDon);
          }
          break;

        //********************
        case 1:
          {
            digitalWrite(LEDgreen, LEDon);
          }
          break;

        //********************
        case 2:
          {
            digitalWrite(LEDblue, LEDon);
          }
          break;

        //********************
        case 3:
          {
            digitalWrite(LEDyellow, LEDon);
          }
          break;

        //********************
        case 4:
          {
            flashFlag = true;
          }
          break;
      }
    }
  }

} //END of   checkSwitches()

mrburnette and LarryD i'm very thankful to both of you guys you really helped me out god bless you ,man both of you helped me without you're help i wouldnt come out this easy out of homework thank you again

You have an opportunity here to learn a lot.

If you hand in the sketch without asking questions, you will have learned nothing.

:roll_eyes: