One push button, multiple functions

Hello !
I'm not very good writing sketch for arduino.
I have 1 neopixel led

I would like to connect the neopixel led to one pushbotton so that when I

click the pushbotton 1 time, the neopixel become and stays red

click the pushbotton 2 times, the neopixel
become and stays yellow.

click the pushbotton 3 times and the neopixel become and stays green

hold the pushbotton for 1 second and the neopoxel become blinking yellow.

Could anybody help me with the sketch?

thank you very much

David

do you know ?

Keep track of the number of button pushes, when it exceeds the maximum reset it to zero. Then use the button value do do what you want. The Do Case statement will make it easy.

does this mean a 2nd time or 2 times within some period of time (e.g. 1 second)

2 times within a certain period of time

thank you for your help

no need for an FSM, but tricky

look this over

const byte PinBut = A1;
byte       butState;

unsigned long  MsecOneSec = 1000;
unsigned long  msec0;

int            cnt;

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

    // check for timeout if msec0 set
    if (0 != msec0 && msec - msec0 >= MsecOneSec)  {
        if (LOW == butState)  
            Serial.println ("blinking yellow");
        else  {
            switch (cnt)  {
            case 1:
                Serial.println ("red");
                break;

            case 2:
                Serial.println ("yellow");
                break;

            case 3:
                Serial.println ("green");
                break;
            }
        }

        cnt   = 0;
        msec0 = 0;
    }

    // check for button press
    byte but = digitalRead (PinBut);
    if (butState != but)  { // button state changed
        butState = but;
        delay (20);         // debounce

        if (LOW == but)  {  // pressed
            msec0 = 0 == msec ? 1 : msec;   // set msec0, not zero
            cnt++;                // will inc >1 if sooner than timeout
            Serial.println (cnt);
        }
    }
}


void setup () {
    Serial.begin (9600);

    pinMode (PinBut, INPUT_PULLUP);
    butState = digitalRead (PinBut);
}

So I have done this sketch, and works perfect with 4 pushbottons. (one botton is for green, one botton for yellow, one botton for red, one botton for blinking yellow)

I would like to achieve the same result but with only one botton (when I press it 1 time I have green, when I press it 2 times in 1 second I have yellow, when I press it 3 times in 1 second I have red, when I press it down for 1 second I have it yellow blinking)
What should I change in the sketch?

thank you very much

#include<Adafruit_NeoPixel.h>
Adafruit_NeoPixel neopixel(1, 2, NEO_GRB + NEO_KHZ800);
int giallo =6;
int verde =5;
int rosso =7;
int giallolamp =8;

void setup() {
neopixel.begin();
neopixel.show();
pinMode(giallo, INPUT_PULLUP);
pinMode(verde, INPUT_PULLUP);
pinMode(rosso, INPUT_PULLUP);
pinMode(giallolamp, INPUT_PULLUP);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
// int i = 0;
}

void loop() {

//int i = 0;
int stato = digitalRead(verde);
if(stato ==LOW){

neopixel.clear();

digitalWrite(11,HIGH);
digitalWrite(12,LOW);
neopixel.setPixelColor(0, neopixel.Color(60, 200, 0));
neopixel.show();
neopixel.clear();
}

int pippo = digitalRead(giallo);
if(pippo ==LOW){
neopixel.clear();
digitalWrite(11,LOW);
digitalWrite(12,LOW);
neopixel.setPixelColor(0, neopixel.Color(255, 200, 0));
neopixel.show();
neopixel.clear();
}
int pappa = digitalRead(rosso);
if(pappa ==LOW){

neopixel.clear();
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
neopixel.setPixelColor(0, neopixel.Color(250, 0, 0));
neopixel.show();
neopixel.clear();

}
int peppa = digitalRead(giallolamp);
int i = 0;
if(peppa == LOW)
while (i<2){
i;
int stato = digitalRead(verde);
if (stato == LOW) break;
int pappa = digitalRead(rosso);
if(pappa == LOW)break;
int pippo = digitalRead(giallo);
if(pippo ==LOW)break;

digitalWrite(11,LOW);
digitalWrite(12,LOW);
neopixel.clear();
neopixel.setPixelColor(0, neopixel.Color(92,59,9));
neopixel.show();
delay(800);
neopixel.clear();
neopixel.setPixelColor(0, neopixel.Color(0, 0, 0));
neopixel.show();
delay(300);
neopixel.clear();

}}

setup a loop function that checks for the button being pressed, and each time having a variable being added one. But before that, have a function get the time (aka, 2:59 and 13 seconds) then in the loop function have an if function wait until the time is that time plus one second (aka 259 and 14 sec). Now get the variable and if it is one, turn the led red, etc. Hope this helps!

it's not that simple, you need a different approach.

one that captures the time of each button press, that recognizes when a timeout period has expired and processes the # of button presses that has occurs.

see my previous post

thank you very much for your help, it s a bit difficult for me

it's just not that simple a problem to solve

ask questions

Thank you for your kind help.

I have done this new code, 2 neopixel controlled with 7 pushbottons for having different colours possibilities.

Everything works fine, the only problem is that when I have the Yellow and Green blinking at the same time (in the code is GxVx) from there if I press the botton for RGx(red and yellow blinking) or the botton for GxVxALT (yellow and green blinking alternatively) it doesn t work.
But if I press one of the other buttons they work.

What I want to say is that when I have the GxVx, I can t go to RGx nor GxVxALT.

Any idea why it doesn t work?

Thank you so much for your great help.

Here is the code

#include<Adafruit_NeoPixel.h>
Adafruit_NeoPixel neopixel(2, 2, NEO_GRB + NEO_KHZ800);
int G_G =6;
int G_V =5;
int R_V =7;
int R_G =8;
int Gx_Vx =9;
int Gx_VxALT =10;
int R_Gx =13;

void setup() {
neopixel.begin();
neopixel.show();
pinMode(G_G, INPUT_PULLUP);
pinMode(G_V, INPUT_PULLUP);
pinMode(R_V, INPUT_PULLUP);
pinMode(R_G, INPUT_PULLUP);
pinMode(Gx_Vx, INPUT_PULLUP);
pinMode(Gx_VxALT, INPUT_PULLUP);
pinMode(R_Gx, INPUT_PULLUP);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
// int i = 0;

}
void loop() {

//int i = 0;

int stato = digitalRead(G_G);
if(stato ==LOW){

neopixel.clear();

digitalWrite(11,HIGH);
digitalWrite(12,LOW);
neopixel.setPixelColor(0, neopixel.Color(250, 190, 0));
neopixel.setPixelColor(1, neopixel.Color(250, 190, 0));
neopixel.show();
neopixel.clear();
}

int pippo = digitalRead(G_V);
if(pippo ==LOW){
neopixel.clear();
digitalWrite(11,LOW);
digitalWrite(12,LOW);
neopixel.setPixelColor(0, neopixel.Color(250, 190, 0));
neopixel.setPixelColor(1, neopixel.Color(60, 200, 0));
neopixel.show();
neopixel.clear();
}

int pappa = digitalRead(R_V);
if(pappa ==LOW){

neopixel.clear();
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
neopixel.setPixelColor(0, neopixel.Color(200, 0, 0));
neopixel.setPixelColor(1, neopixel.Color(60, 200, 0));
neopixel.show();
neopixel.clear();
}
int puppa = digitalRead(R_G);
if(puppa ==LOW){

neopixel.clear();
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
neopixel.setPixelColor(0, neopixel.Color(200, 0, 0));
neopixel.setPixelColor(1, neopixel.Color(250, 190, 0));
neopixel.show();
neopixel.clear();

}
int peppa = digitalRead(Gx_Vx);
int i = 0;
if(peppa == LOW)
while (i<2){
i;
int stato = digitalRead(G_G);
if(stato == LOW) break;
int pappa = digitalRead(G_V);
if(pappa == LOW)break;
int pippo = digitalRead(R_V);
if(pippo == LOW)break;
int puppa = digitalRead(R_G);
if(puppa == LOW)break;

digitalWrite(11,LOW);
digitalWrite(12,LOW);
neopixel.clear();
neopixel.setPixelColor(0, neopixel.Color(250,190,0));
neopixel.setPixelColor(1, neopixel.Color(60,200,0));
neopixel.show();
delay(500);
neopixel.clear();
neopixel.setPixelColor(0, neopixel.Color(0, 0, 0));
neopixel.setPixelColor(1, neopixel.Color(0,0,0));
neopixel.show();
delay(500);
neopixel.clear();
}

int david = digitalRead(Gx_VxALT);
if(david == LOW)
while (i<2){

int stato = digitalRead(G_G);
if(stato == LOW) break;
int pappa = digitalRead(G_V);
if(pappa == LOW)break;
int pippo = digitalRead(R_V);
if(pippo == LOW)break;
int puppa = digitalRead(R_G);
if(puppa == LOW)break;
int peppa = digitalRead(Gx_Vx);
if(peppa == LOW)break;

digitalWrite(11,LOW);
digitalWrite(12,LOW);
neopixel.clear();
neopixel.setPixelColor(0, neopixel.Color(250,190,0));
neopixel.setPixelColor(1, neopixel.Color(0, 0, 0));
neopixel.show();
delay(500);
neopixel.clear();
neopixel.setPixelColor(0, neopixel.Color(0, 0, 0));
neopixel.setPixelColor(1, neopixel.Color(60, 200, 0));
neopixel.show();
delay(500);
neopixel.clear();
}
int captain = digitalRead(R_Gx);
if(captain == LOW)
while (i<2){

int stato = digitalRead(G_G);
if(stato == LOW) break;
int pappa = digitalRead(G_V);
if(pappa == LOW)break;
int pippo = digitalRead(R_V);
if(pippo == LOW)break;
int puppa = digitalRead(R_G);
if(puppa == LOW)break;
int peppa = digitalRead(Gx_Vx);
if(peppa == LOW)break;
int david = digitalRead(Gx_VxALT);
if(david == LOW)break;

digitalWrite(11,LOW);
digitalWrite(12,LOW);
neopixel.clear();
neopixel.setPixelColor(0, neopixel.Color(200,0,0));
neopixel.setPixelColor(1, neopixel.Color(0, 0, 0));
neopixel.show();
delay(500);
neopixel.clear();
neopixel.setPixelColor(0, neopixel.Color(0, 0, 0));
neopixel.setPixelColor(1, neopixel.Color(250, 190, 0));
neopixel.show();
delay(500);
neopixel.clear();
}
}

when i look at each of your cases (below), i don't see your "exit" case for Gx_VxALT under G_Vx

    int peppa = digitalRead (Gx_Vx);
        int stato = digitalRead (G_G);
        int pappa = digitalRead (G_V);
        int pippo = digitalRead (R_V);
        int puppa = digitalRead (R_G);
    int david = digitalRead (Gx_VxALT);
        int stato = digitalRead (G_G);
        int pappa = digitalRead (G_V);
        int pippo = digitalRead (R_V);
        int puppa = digitalRead (R_G);
        int peppa = digitalRead (Gx_Vx);
    int captain = digitalRead (R_Gx);
        int stato = digitalRead (G_G);
        int pappa = digitalRead (G_V);
        int pippo = digitalRead (R_V);
        int puppa = digitalRead (R_G);
        int peppa = digitalRead (Gx_Vx);
        int david = digitalRead (Gx_VxALT);

code is awkward.

why not call digitalwrites() or setPixelColor() once when a button ir pressed instead of using while loops that essentially check for other buttons to be pressed