2nd attempt Still does not work??????

I'm not getting very far with my code, I have tried to ask before, but I get the feeling people prefer to be little me rather than provide useful assistance. I am new to this so apologies.

These two codes work individually, the 1st puts on LEDS on by one per button push

#include <Bounce2.h>
#include <Button.h>


int trlpin_A = 13;
int trlpin_B = 12;
int trlpin_C = 11;
int push_button = 4;
int buttonstate = LOW;
int lastbuttonstate = HIGH;
int actual;

Bounce debouncer = Bounce(); 

void setup() {
  pinMode(trlpin_A, OUTPUT);
  pinMode(trlpin_B, OUTPUT);
  pinMode(trlpin_C, OUTPUT);
  pinMode(push_button, INPUT);
  
  debouncer.attach(push_button);
  debouncer.interval(50); // interval in ms

 }

void loop() {
   // Update the Bounce instance :
  debouncer.update();
// Get the updated value :
  int value = debouncer.read();

  if ( value == HIGH ) {
  
 buttonstate = digitalRead(push_button);
if (buttonstate == HIGH && buttonstate != lastbuttonstate)
   {
     if(actual == 3) actual = 0;
     else actual++;
   }
 lastbuttonstate = buttonstate;

 switch(actual)
 {
   case 0:
   digitalWrite(trlpin_C, LOW);
   digitalWrite(trlpin_A, HIGH);
   break;

   case 1:
   digitalWrite(trlpin_A, LOW);
   digitalWrite(trlpin_B, HIGH);
   break;

   case 2:
   digitalWrite(trlpin_B, LOW);
   digitalWrite(trlpin_C, HIGH);
   break;

  }
}
}

The next where I push the button it puts LEDs A and B on the off after 2 seconds OR i push and hold and it puts another 2 LEDs on then off.

#define debounce 20 
#define holdTime 2000 
#include <Button.h>

int trlpin_A = 13;
int trlpin_B = 12;
int trlpin_C = 11;
int trlpin_E = 10;
int push_button = 4;
int buttonVal = 0;
int buttonLast = 0;
long btnDnTime;
long btnUpTime;
boolean ignoreUp = false;

void setup()
{
  pinMode(trlpin_A, OUTPUT);
  pinMode(trlpin_B, OUTPUT);
  pinMode(trlpin_C, OUTPUT);
  pinMode(trlpin_E, OUTPUT);
  pinMode(push_button, INPUT);
  digitalWrite(push_button, LOW );
}

void loop()
{
  // Read the state of the button
  buttonVal = digitalRead(push_button);

  // Test for button pressed and store the down time
  if (buttonVal == HIGH && buttonLast == LOW && (millis() - btnUpTime) > long(debounce))
  {
    btnDnTime = millis();
  }

  // Test for button release and store the up time
  if (buttonVal == LOW && buttonLast == HIGH && (millis() - btnDnTime) > long(debounce))
  {
    if (ignoreUp == false) 
      {
        digitalWrite(trlpin_A, HIGH);
        digitalWrite(trlpin_B, HIGH);
        delay(2000);
        digitalWrite(trlpin_A, LOW);
        digitalWrite(trlpin_B, LOW);
      }
    else ignoreUp = false;
    btnUpTime = millis();
  }

  // Test for button held down for longer than the hold time
  if (buttonVal == HIGH && (millis() - btnDnTime) > long(holdTime))
  {

    digitalWrite(trlpin_C, HIGH);
    digitalWrite(trlpin_E, HIGH);
    delay(2000);
    digitalWrite(trlpin_C, LOW);
    digitalWrite(trlpin_E, LOW);

    ignoreUp = true;
    btnDnTime = millis();
  }

  buttonLast = buttonVal;


}

I am not able to join the two together so button1 can be either push..(run a function) push and hold (run another function)
Button 2 each press of the button turns on 1 LED
I have tried as per my next post:

#define debounce 20 
#define holdTime 2000 
#include <Button.h>
#include <Bounce2.h>

int trlpin_A = 13;
int trlpin_B = 12;
int trlpin_C = 11;
int trlpin_E = 10;
int push_button = 4;
int push_button2 = 3;
int buttonVal = 0;
int buttonLast = 0;
long btnDnTime;
long btnUpTime;
boolean ignoreUp = false;

int buttonstate = LOW;
int lastbuttonstate = HIGH;
int actual;

Bounce debouncer = Bounce();

void setup()
{
  pinMode(trlpin_A, OUTPUT);
  pinMode(trlpin_B, OUTPUT);
  pinMode(trlpin_C, OUTPUT);
  pinMode(trlpin_E, OUTPUT);
  pinMode(push_button, INPUT);
  pinMode(push_button2, INPUT);
  //digitalWrite(push_button, LOW );

  debouncer.attach(push_button2);
  debouncer.interval(50); // interval in ms
}

//byte pressCount = 0;
void loop()
{
  
  // Read the state of the button
  buttonVal = digitalRead(push_button);

  // Test for button pressed and store the down time
  if (buttonVal == HIGH && buttonLast == LOW && (millis() - btnUpTime) > long(debounce))
  {
    btnDnTime = millis();
  }

  // Test for button release and store the up time
  if (buttonVal == LOW && buttonLast == HIGH && (millis() - btnDnTime) > long(debounce))
  {
    if (ignoreUp == false)
    {
      digitalWrite(trlpin_A, HIGH);
      digitalWrite(trlpin_B, HIGH);
      delay(2000);
      digitalWrite(trlpin_A, LOW);
      digitalWrite(trlpin_B, LOW);
    }
    else ignoreUp = false;
    btnUpTime = millis();
  }

  // Test for button held down for longer than the hold time
  if (buttonVal == HIGH && (millis() - btnDnTime) > long(holdTime))
  {

    digitalWrite(trlpin_C, HIGH);
    digitalWrite(trlpin_E, HIGH);
    delay(2000);
    digitalWrite(trlpin_C, LOW);
    digitalWrite(trlpin_E, LOW);

    ignoreUp = true;
    btnDnTime = millis();
  }

  buttonLast = buttonVal;

  // Update the Bounce instance :
  debouncer.update();
  // Get the updated value :
  int value = debouncer.read();

  if ( value == HIGH ) {

    buttonstate = digitalRead(push_button);
    if (buttonstate == HIGH && buttonstate != lastbuttonstate)
    {
      if (actual == 3) actual = 0;
      else actual++;
    }
    lastbuttonstate = buttonstate;

    switch (actual)
    {
      case 0:
        digitalWrite(trlpin_C, LOW);
        digitalWrite(trlpin_A, HIGH);
        break;

      case 1:
        digitalWrite(trlpin_A, LOW);
        digitalWrite(trlpin_B, HIGH);
        break;

      case 2:
        digitalWrite(trlpin_B, LOW);
        digitalWrite(trlpin_C, HIGH);
        break;

    }
  }
}

The above sketch compiles, and when it runs, the button1 push puts on the LEDs as per the code, Press and Hold puts on the other 2 LEDs as per the code.
BUT
when I push button2 then it seems to get stuck on (case 0:)
After a couple of seconds if I push button1 then it seems to reset.

I am not after more abuse of how terrible I am at this, just a little help or to even tell me I am not going to get this to work.

Many thanks in advance
:slight_smile:

I should have asked is it because when I put them together, I am trying to count the number of button pushes on 2 buttons and Arduino can only count one set?

Cbrenn0823:
...I have tried to ask before, but I get the feeling people prefer to be little me...

Actually, I prefer to be Dr. Evil.

      if (actual == 3) actual = 0;
      else actual++;

This means "if actual is 3, set it to zero and if it is not 3, increment it. That will count 1, 2, 3, 0, 1, 2, 3, 0. Since you only have cases for 0, 1, and 2 you probably meant to say:

      if (actual < 3) 
        actual++;
      else 
        actual = 0;

That will count 0, 1, 2, 0, 1, 2.

johnwasser:

 you probably meant to say:

[code]      if (actual < 3)
        actual++;
      else
        actual = 0;




That will count 0, 1, 2, 0, 1, 2.

Ummm.... No, it won't. Those two snippets will both count 0, 1, 2, 3, 0, 1, 2, 3,...

Regards,
Ray L.

How 'bout:

if(++actual > 2) actual = 0:

Both the sketches in the 1st post work. The problem I have is trying to get them to both work together in the same sketch.

 if ( value == HIGH ) {

    buttonstate = digitalRead(push_button);
    if (buttonstate == HIGH && buttonstate != lastbuttonstate)

maybe

 if ( value == HIGH ) {

    buttonstate = digitalRead(push_button2);//added the 2
    if (buttonstate == HIGH && buttonstate != lastbuttonstate)

gpop1:

 if ( value == HIGH ) {

buttonstate = digitalRead(push_button);
    if (buttonstate == HIGH && buttonstate != lastbuttonstate)




maybe 



if ( value == HIGH ) {

buttonstate = digitalRead(push_button2);//added the 2
    if (buttonstate == HIGH && buttonstate != lastbuttonstate)

Ohh I didn't notice that! I will try when I get home

RayLivingston:
Ummm.... No, it won't. Those two snippets will both count 0, 1, 2, 3, 0, 1, 2, 3,...

Oops. Just shows how easy it is to introduce an Off-By-One error into code.
Might make sense to make the 'modulo 3' more explicit:

actual = (actual+1)%3;

I presume the code in Reply #1 is your attempt at the two pieces in a single program ?

I can't figure from the code or the description exactly what you want the combined program to do. It would clarify things if you write down all the steps each on one line (in English, not code) - like this

press button
    if held for < X
       do AA
    if held for >= X
       do BB

It is not a good idea to mix delay() with millis(). Remove all the delay()s.

Your code for determing the long press seems complex. I think it can work like this (pseudo code) which would be called from loop()

if (button not pressed)
    lastBtnUpMillis = millis()

else
   if ( millis() - lastBtnUpMillis > longInterval)
      do the long stuff
   else
      do the short stuff

...R

I'm not getting very far with my code, I have tried to ask before, but I get the feeling people prefer to be little me rather than provide useful assistance. I am new to this so apologies.

Don't worry about that. Some forum followers use the forum to showcase their sad lives. Usually somebody will provide useful information.

I have corrected the wrong button number as suggested by gpop1 and it all seems to work. I need more time to fully test and actually adapt into my main code.
Thanks for the help.