Multitask push-button

I have a problem with this... easy program I am currently working in. I have to:

With button 1 we have to make 3 different functions.

A momentary press ( <400ms approx.) turn green on with just a flash of 30-40ms

Long press (> 500 and <1100 ms approx.) turn amber on with just a flash of 30-40ms

a long press (> 1300 -1500 ms approx) turn red on with just a flash of 30-40ms

Any help will be apreciated... :disappointed_relieved:

int pulsador1 = 6;
int pulsador2 = 7;
int ledVerde = 3;
int ledAmbar = 4;
int ledRojo = 5;
long tiempo;

void setup(){
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
Serial.begin(9600);

Well, first look at the logic. The code design should follow very directly from it.

  1. wait for a button on state
  2. record the time
  3. wait for a button off state
  4. record the new time and compare it with the old.
  5. use the difference to decide which LED to flash.
  6. rinse and repeat

Oh, you should debounce the switch and also use millis() for timing.

Use code tags as explained in the forum stickies, when posting code.

This Thread has a simple way to deal with different button presses.

...R

aarg:
Well, first look at the logic. The code design should follow very directly from it.

  1. wait for a button on state
  2. record the time
  3. wait for a button off state
  4. record the new time and compare it with the old.
  5. use the difference to decide which LED to flash.
  6. rinse and repeat

Oh, you should debounce the switch and also use millis() for timing.

Use code tags as explained in the forum stickies, when posting code.

How can I record and compare the second time? I'm a newcomer to this platform.

MrQueroseno:
How can I record and compare the second time? I'm a newcomer to this platform.

secondPress = millis();
if (secondPress - firstPress > 1400) {...flash red}
else if (secondPress - firstPress > 1100) {...flash amber}
else if (secondPress - firstPress > 600) {...flash green}
//..etc...

aarg:

secondPress = millis();

if (secondPress - firstPress > 1400) {...flash red}
else if (secondPress - firstPress > 1100) {...flash amber}
else if (secondPress - firstPress > 600) {...flash green}
//..etc...

I have this for now, but only the amber is working... :disappointed_relieved:

int ledverde=3;
int ledambar=4;
int ledrojo=5;
int pulsador1=6;
int pulsador2=7;
int pulsador3=8;
long firstPress=10000;
long secondPress=10000;



void setup() {
  pinMode (ledverde, OUTPUT);
  pinMode (ledambar, OUTPUT);
  pinMode (ledrojo, OUTPUT);
  pinMode (pulsador1, INPUT);
  pinMode (pulsador2, INPUT);
  pinMode (pulsador3, INPUT);
  Serial.begin(9600);
}

void loop() {
  
  if  (((digitalRead(pulsador1)) == (HIGH))){
    firstPress=millis ();
     if  (((digitalRead(pulsador1)) == (LOW))){
     secondPress = millis ();
     if ((secondPress - firstPress)>1400) {
     digitalWrite (ledrojo, HIGH);
     delay (35);
     digitalWrite (ledrojo, LOW);
     }
     else if (700<(secondPress - firstPress)<=1400) {
     digitalWrite (ledambar, HIGH);
     delay (35);
     digitalWrite (ledambar, LOW);
     }
     else if ((secondPress - firstPress)<=700) {
     digitalWrite (ledverde, HIGH);
     delay (35);
     digitalWrite (ledverde, LOW);
     }
  }
 }
}

Okay, stop right in your tracks! Before you do anything more with millis(), change any time variables you use (like firstPress) to the unsigned long type. If you don't, you will have rollover issues when millis() resets to 0.

The data collection and action on the data should be in separate parts:

int ledverde = 3;
int ledambar = 4;
int ledrojo = 5;
int pulsador1 = 6;
int pulsador2 = 7;
int pulsador3 = 8;
unsigned long firstPress;
unsigned long secondPress;

void setup() {
  pinMode (ledverde, OUTPUT);
  pinMode (ledambar, OUTPUT);
  pinMode (ledrojo, OUTPUT);
  pinMode (pulsador1, INPUT);
  pinMode (pulsador2, INPUT);
  pinMode (pulsador3, INPUT);
  Serial.begin(9600);
}

void loop() {

  // first get the button press:
  while  (((digitalRead(pulsador1)) == (LOW))); //wait for press
  firstPress = millis ();
  delay (20); // debounce the switch
  while  (((digitalRead(pulsador1)) == (HIGH))); //wait for release
  secondPress = millis ();

  // secondly, blink the LED:
  if ((secondPress - firstPress) > 1400) {
    digitalWrite (ledrojo, HIGH);
    delay (35);
    digitalWrite (ledrojo, LOW);
  }
  else if ((secondPress - firstPress) > 700) {
    digitalWrite (ledambar, HIGH);
    delay (35);
    digitalWrite (ledambar, LOW);
  }
  else {
    digitalWrite (ledverde, HIGH);
    delay (35);
    digitalWrite (ledverde, LOW);
  }
}

Testing for <=1400 is redundant in the second else if because you already screened it out in the first if.
Let us know how it runs. I don't have hardware to test it with today.

aarg:
The data collection and action on the data should be in separate parts:

int ledverde = 3;

int ledambar = 4;
int ledrojo = 5;
int pulsador1 = 6;
int pulsador2 = 7;
int pulsador3 = 8;
unsigned long firstPress;
unsigned long secondPress;

void setup() {
  pinMode (ledverde, OUTPUT);
  pinMode (ledambar, OUTPUT);
  pinMode (ledrojo, OUTPUT);
  pinMode (pulsador1, INPUT);
  pinMode (pulsador2, INPUT);
  pinMode (pulsador3, INPUT);
  Serial.begin(9600);
}

void loop() {

// first get the button press:
  while  (((digitalRead(pulsador1)) == (LOW))); //wait for press
  firstPress = millis ();
  delay (20); // debounce the switch
  while  (((digitalRead(pulsador1)) == (HIGH))); //wait for release
  secondPress = millis ();

// secondly, blink the LED:
  if ((secondPress - firstPress) > 1400) {
    digitalWrite (ledrojo, HIGH);
    delay (35);
    digitalWrite (ledrojo, LOW);
  }
  else if ((secondPress - firstPress) > 700) {
    digitalWrite (ledambar, HIGH);
    delay (35);
    digitalWrite (ledambar, LOW);
  }
  else {
    digitalWrite (ledverde, HIGH);
    delay (35);
    digitalWrite (ledverde, LOW);
  }
}




Testing for <=1400 is redundant in the second else if because you already screened it out in the first if.
Let us know how it runs. I don't have hardware to test it with today.

Okay, now I see where I was having the trouble. Anyway, thank you very much, kind user of this forum, you helped me a lot :smiley: