Press the button and blink differently

Hi everyone,

I have a project that I'm responsible but I'm having some issue about it because I'm new at Arduino programming.

So here is project details,

There is a button and LED that I press each and everytime the button, blink LED differently like strip.

I'm using Arduino Nano for this project.

Please help me out about the codes. Thank you!

You must write your code in Arduino language, which is essentially C++ with some standard Arduino libraries for controlling the Nano's pins and so on.

I hope this answer is what you needed. If you need more specific advice, ask a more specific question.

Sorry this is not very clear.
Are you using an LED strip?
If so are the individual LEDs addressable?
What does "blink LED differently" mean? Is it a different colour, a different on / off time ratio or a different speed?

What purpose does this serve in the project?

What code have you written so far?

You might want to look at this How to get the best from this from this Forum before you proceed any further.

Hello arduinoacl

Welcome to the Forum :wave:

Try the BLINKOUTWITHDELAY example of the IDE with some mods.

Yes, you might think like LED strip but I'm only seeking for help about button codes.
I just need the code that for exp. if I press the button, LED blink once and press again LED blink twice than I'm good to go. This is where I'm stuck if I skip this I can handle the rest of it.
Nope, I'm using non-adressable LED's, it's PowerLED's.

It's a personal project and I will publish all the codes when I'm done the work.

It seems to be a school assignment.

So look at the state change detection example in the IDE under the menu chain
File -> Examples -> 02.Digital
That will tell you how to detect when a button becomes pressed and not just is a button being pressed.

Yes, but I need more specific help guys. This time I need to the fish itself because deadline is tomorrow morning. We have to solve this out today!

So, I need a code that everytime I press the button Arduino changes the LED blink mode

but then you say

So it is not a personal project it is an assignment.

Sorry but I used to be a lecturer at a UK university and while I am more than willing to help you, I will not do your homework for you.

I'm not a student mate so this is not my homework obviously. This project is being used for a police car flasher. I'm just need a bit of help about button. I can deal with the rest of the project but I ONLY need a piece of code that everytime I press the button LED have to blink differently. Believe me I'm struggling with this since the last whole week. So will you help me?

Show your current sketch to see how we can help.

This is the codes that I write so far.

void setup() {

pinMode(2, INPUT); //Mode Button

pinMode(5, OUTPUT); // LED RED

pinMode(6, OUTPUT); // LED BLUE

pinMode(7, OUTPUT); // LED WHITE

pinMode(8, OUTPUT); // LED WHITE

}

void loop() {

if (digitalRead == (2, HIGH)); // When Mode button is pushed...

digitalWrite(5,7 HIGH);

delay(50);

digitalWrite(5,7 LOW);

delay(50);

digitalWrite(5,7 HIGH);

delay(50);

digitalWrite(5,7 LOW);

delay(50);

digitalWrite(5,7 HIGH);

delay(50);

digitalWrite(5,7 LOW);

delay(50);

digitalWrite(6,8 HIGH);

delay(50);

digitalWrite(6,8 LOW);

delay(50);

digitalWrite(6,8 HIGH);

delay(50);

digitalWrite(6,8 LOW);

delay(50);

digitalWrite(6,8 HIGH);

delay(50);

digitalWrite(6,8 LOW);

delay(50);

//so on...

}

presumably you have code to flash the LED(s) in different ways and just need a way to sequence thru the different routines.

one way is to have a variable tracking the state and which routine is running. and is incremented each time the button is pressed. that variable is reset to zero after exceeding the last routine

millis() can be used to periodically invoke the different routine without blocking button detection.

if there are no sufficient delays in processing after each button press, a but state variable can be used to track the state and recognize a change in state so the the sequence increments one step at a time

and you still haven’t told us what the blink mode is !
… and your existing code is not going anywhere.
You’d better tell the police that you won’t be ready.
I’d suggest as above, you read up about blinking a LED, millis timing and a few more fundamentals.

The style of your code is very linear, and will be almost unmaintainable until you learn the language and common coding techniques.

The blinking and switches are the least of your problems. If it helps, there is a PAID section of this forum, where you can negotiate for someone to write your code.

This is a pretty simple project, I’m sure it won’t cost much.

either you get it or you don't

const byte PinBut = 2;
const byte PinLeds [] = { 10, 11, 12, 13 };
#define Nleds    (int)sizeof (PinLeds)
byte butState;

enum { Off = HIGH, On = LOW };

const unsigned long MsecPeriod = 200;
unsigned long msecLst;

int ledIdx;
int mode;
#define M_LAST 4

// -----------------------------------------------------------------------------
void
mode1 ()
{
    if (Nleds <= ++ledIdx)
        ledIdx = 0;

    digitalWrite (PinLeds [ledIdx], ! digitalRead (PinLeds [ledIdx]));
}

void
mode2 ()
{
    if (0 > --ledIdx)
        ledIdx = Nleds-1;

    digitalWrite (PinLeds [ledIdx], ! digitalRead (PinLeds [ledIdx]));
}

void
mode3 ()
{
    digitalWrite (PinLeds [ledIdx], Off);
    if (Nleds <= ++ledIdx)
        ledIdx = 0;
    digitalWrite (PinLeds [ledIdx], On);
}

void
mode4 ()
{
    digitalWrite (PinLeds [ledIdx], Off);
    if (0 > --ledIdx)
        ledIdx = Nleds-1;
    digitalWrite (PinLeds [ledIdx], On);
}

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    if (msec - msecLst >= MsecPeriod)  {
        msecLst = msec;

        switch (mode) {
        case 1:
            mode1 ();
            break;
        case 2:
            mode2 ();
            break;
        case 3:
            mode3 ();
            break;
        case 4:
            mode4 ();
            break;
        }
    }


    byte but = digitalRead (PinBut);
    if (butState != but)  {
        butState = but;
        
        if (LOW == but)
            if (M_LAST == ++mode)
                mode = 0;
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    pinMode (PinBut, INPUT_PULLUP);
    butState = digitalRead (PinBut);

    for (unsigned n = 0; n < Nleds; n++)  {
        pinMode (PinLeds [n], OUTPUT);
        digitalWrite (PinLeds [n], Off);
    }
}

Hello arduinoacl

Your current sketch won´t compile.
Have you been skipping class?

First of all, I didn't say I'm pro or Arduino Guru or something? I said I'm new at this stuff and still learning. Have some respect. Thanks for your understanding.

And, this code is already working(on breadboard right now) and I have to proceed over it.

The main idea of this project is 4(2+2) outputs which left and right, with 50ms intervals! (In total 4 outputs that I need) Look at the police flashers. Just like that. This was mode 1.

Mode 2: All LED's has to slinding forward and backward.
Mode 3: The LED's has to flash all together with 50ms intervals.
Mode 4, Mode 5 and so on, it's up to my imagination.
But, I was asking help for mode switching. I can manage the Modes leave it to me. Please help for just mode switching on my Code that already upload to Arduino.

Here it is;

void setup() {
pinMode(2, INPUT); //Mode Button
pinMode(5, OUTPUT); // LED RED
pinMode(6, OUTPUT); // LED BLUE
pinMode(7, OUTPUT); // LED WHITE
pinMode(8, OUTPUT); // LED WHITE
}

void loop() {

if (digitalRead == (2, HIGH)); // When Mode button is pushed...
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
delay(50);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
delay(50);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
delay(50);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
delay(50);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
delay(50);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
delay(50);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
delay(50);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
delay(50);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
delay(50);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
delay(50);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
delay(50);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
delay(50);

//so on...
}

Thank you much appreciated but for begginers like me, the language is a little heavy. My bad. Sorry.

it seems to be a xy-problem

Something like that. Think about 10 different modes and link them together with botton. When you press it changes to the other one. I hope I was able to explain my case.