Need help to handle buttons state with switch code

Hi guys!!

noob here (started with this fantastic tool few weeks ago!) and as I'm quite young (55 in June :wink: )I'm in the "I want it all, I want it now" status :), so while trying to understand the most of it, I've stated with "cut&paste" technic (I know I shouldn't... but .... I do learn something reading other codes).

Ok my problem is that I need to control buttons state before the "switching code", and this is done, then have that states put to 0, and wait for a new state, otherwise the "switching code" case is always executed, I need it to stay there.

TBH I'm quite sure I've gone lost in the "if"s nests..

What the code does
one single button could have 3 states, 1 (click) 2 (double click) 3 (hold), and it gets multiplies by 10.
then 6 buttons could, then pushed states become 1 and get added to the above. Then the switching part do something based on sum of buttons values (ie single button 2, and 3rd switch pressed, get the sum is 23, and so on.
The main problem is that I need to get rid of that value(23) for the whole time until another button is pressed, them sum it again ecc.
I've succeeded to control the single switch values, but cannot get rid of the summed value.

If I get rid of the switch code, I can have on monitor the status of the single button and of which of the 6 buttons has been pressed

(sorry for a so long post....)

so here's my code, didn't put all the variables and void setup to keep the code short, but I will if needed

(please don't sniff at it, it's almost all cut&paste of other code :slight_smile:

code is an attachment, being quite long...

A huge thanks to everybody here!!
Ciao
Armando

Q2 looper concept.txt (7.37 KB)

Here's his code:

void loop()   
{
    int buttonSingle = checkButton(); 
    if (buttonSingle!=0 && StateofbuttonSingle!=buttonSingle)  
    {
        StateofbuttonSingle=buttonSingle;
        StateofbuttonPressed=0;
        if (StateofbuttonSingle==1)
        {
            whichState="Select Loop";
        }
        if (StateofbuttonSingle==2)
        {
            whichState="Build Loop";
        }
        if (StateofbuttonSingle==3)
        {
            whichState="Save Loop";
        }
        Serial.print("Button single ");
        Serial.println(whichState);
    }
    int buttonPressed = rreadbutton(); 
    if (buttonPressed !=0 && buttonPressed != StateofbuttonPressed)    
    {
        StateofbuttonPressed = buttonPressed;
    } 
    if (StateofbuttonPressed != btnNONE)
    {     Serial.println(actTotalsumofbutton);
        actTotalsumofbutton =Totalsumofbutton;
        Totalsumofbutton=StateofbuttonPressed+(StateofbuttonSingle*10) ;
        if (actTotalsumofbutton !=Totalsumofbutton)
        {
            actTotalsumofbutton =Totalsumofbutton;
        }

        sumofButtonspressed= actTotalsumofbutton;//(StateofbuttonPressed+(buttonSingle*10));
        runswitch();
        if (buttonSingle== 1) //single click edit happen on edit&save button - Preset
        {
            if (digitalRead(ledPin2) == 1)
            {
                ledVal2 = !ledVal2;      
                digitalWrite(ledPin2, 0);
            }
            if (digitalRead(ledPin3) == 1)
            {
                ledVal3 = !ledVal3;      
                digitalWrite(ledPin3, 0);
            }
            ledVal1 = !ledVal1;      
            digitalWrite(ledPin1, ledVal1);
            stateSingleButton1=buttonSingle;
            stateSingleLed1=digitalRead(ledPin1);
        }
        if (buttonSingle== 2 && digitalRead(ledPin2)==0) //double click edit happen on edit&save button - Edit
        {
            if (digitalRead(ledPin1) == 1)
            {
                ledVal1 = !ledVal1;      
                digitalWrite(ledPin1, 0);
            }
            if (digitalRead(ledPin3) == 1)
            {
                ledVal3 = !ledVal3;      
                digitalWrite(ledPin3, 0);
            }
            ledVal2 = !ledVal2;
            digitalWrite(ledPin2, ledVal2);
            stateSingleButton1=buttonSingle;
            stateSingleLed1=digitalRead(ledPin2); 
        }  
        if (buttonSingle== 3 && digitalRead(ledPin1)==0) //hold click edit happen on edit&save button - Save and led 1 is off
        {
            if (digitalRead(ledPin1) == 1)
            {
                ledVal1 = !ledVal1;      
                digitalWrite(ledPin1, 0);
            }
            if (digitalRead(ledPin2) == 1)
            {
                ledVal2 = !ledVal2;      
                digitalWrite(ledPin2, 0);
            }
            ledVal3 = !ledVal3;      
            digitalWrite(ledPin3, ledVal3);
            stateSingleButton1=buttonSingle;
            stateSingleLed1=digitalRead(ledPin3);
        }

    }  
}//--(end main loop )---


void runswitch(){
    switch (sumofButtonspressed) // 1 or 2 or 3 = 1 Select Loop - 2 Build Loop - 3 Save loop - + 1 to 6 buttonpressed
    {
    case 21:  // BuildLoop(int relay)
        StateofbuttonPressed = btnNONE;
        sumofButtonspressed=0;
        BuildLoop(0);
        break;
    case 22:
        BuildLoop(1);
        break;
    case 23:
        BuildLoop(2);
        break;
    case 24:
        BuildLoop(3);
        break; 
    case 25:
        BuildLoop(4);
        break; 
    case 26:
        BuildLoop(5);
        break; 
        /****************************** STORE PRESET MODE */      
    case 31: //SaveLoop(int addr, int led)
        SaveLoop(11,11);
        break;
    case 32:
        SaveLoop(21,12);
        break;
    case 33:
        SaveLoop(31,13);
        break;
    case 34:
        SaveLoop(41,3);
        break;
    case 35:
        SaveLoop(51,2);
        break;
    case 36:
        SaveLoop(61,1);
        break;
        /****************************** READ PRESET MODE */     
    case 11: //LiveModePreset(int addr, int pcNum, int led)
        LiveModePreset(11, 1, 11);
        break;
    case 12:
        LiveModePreset(21, 2, 12);
        break;
    case 13:
        LiveModePreset(31, 3, 13);
        break;
    case 14:
        LiveModePreset(41, 4, 3);
        break;  
    case 15:
        LiveModePreset(51, 5, 2);
        break;  
    case 16:
        LiveModePreset(61, 6, 1);
        break;  
    }
}


int checkButton()
{
    int event = 0;
    // Read the state of the button
    buttonVal = digitalRead(buttonPin);
    // Button pressed down
    if (buttonVal == LOW && buttonLast == HIGH && (millis() - upTime) > debounce) {
        downTime = millis();
        ignoreUp = false;
        waitForUp = false;
        singleOK = true;
        holdEventPast = false;
        longHoldEventPast = false;
        if ((millis() - upTime) < DCgap && DConUp == false && DCwaiting == true) DConUp = true;
        else DConUp = false;
        DCwaiting = false;
    }
    // Button released
    else if (buttonVal == HIGH && buttonLast == LOW && (millis() - downTime) > debounce) {
        if (not ignoreUp) {
            upTime = millis();
            if (DConUp == false) DCwaiting = true;
            else {
                event = 2;
                DConUp = false;
                DCwaiting = false;
                singleOK = false;
            }
        }
    }
    // Test for normal click event: DCgap expired
    if ( buttonVal == HIGH && (millis() - upTime) >= DCgap && DCwaiting == true && DConUp == false && singleOK == true && digitalRead(ledPin1) == 0) {
        event = 1;
        DCwaiting = false;
    }
    // Test for hold
    if (buttonVal == LOW && (millis() - downTime) >= holdTime) {
        // Trigger "normal" hold
        if (not holdEventPast) {
            event = 3;
            waitForUp = true;
            ignoreUp = true;
            DConUp = false;
            DCwaiting = false;
            //downTime = millis();
            holdEventPast = true;
        }
        // Trigger "long" hold
        if ((millis() - downTime) >= longHoldTime) {
            if (not longHoldEventPast) {
                event = 4;
                longHoldEventPast = true;
            }
        }
    }
    buttonLast = buttonVal;
    return event;
}


int rreadbutton() { 
    // Update the Bounce instances of 6 buttons:
    //Serial.print("pressed =");
    //Serial.println(rpressed);
    debouncer1.update();
    debouncer2.update();
    debouncer3.update();
    debouncer4.update();
    debouncer5.update();
    debouncer6.update();

    // Get the updated value :
    int value1 = debouncer1.read();
    if (value1!=1)
    {
        rpressed=1;
    }
    int value2 = debouncer2.read();
    if (value2!=1)
    {
        rpressed=2;
    }
    int value3 = debouncer3.read();
    if (value3!=1)
    {
        rpressed=3;
    }
    int value4 = debouncer4.read();
    if (value4!=1)
    {
        rpressed=4;
    }
    int value5 = debouncer5.read();
    if (value5!=1)
    {
        rpressed=5;
    }
    int value6 = debouncer6.read();
    if (value6!=1)
    {
        rpressed=6;
    }
    
    //buttonPressed=pressed;
    if (rlastButtonpressed !=rpressed)
    {
        rlastButtonpressed=rpressed;
    }
    //    delay(700);

    delay(60);
    if (rpressed == 1) return buttonPressed = 1;                     
    if (rpressed == 2) return buttonPressed = 2;
    if (rpressed == 3) return buttonPressed = 3;
    if (rpressed == 4) return buttonPressed = 4;
    if (rpressed == 5) return buttonPressed = 5;
    if (rpressed == 6) return buttonPressed = 6;
    return btnNONE; // when all others fail, return this...
}

This is not all your code. Please post all your code. Always post all your code. We can ignore code easily, but we can't see code that isn't there.

There is nothing wrong with cut and paste. I have been doing this a long time, and still use cut and paste all the time. It's often quicker. But be sure you understand what you are cutting and pasting, and be sure you are not duplicating code.

And don't attach code. Post it directly inside code tags so it looks like the above. Please read the "how to post to this forum" thread.

I too got a bit lost in your 'if' and 'select' statements. :o

A reworded description of what you're trying to achieve with these buttons would be helpful so that rather than trying to correct your code, new suggestions could be made on how to better organise your code.

For example, having lots of flags (ignoreUp, waitForUp, singleOK...) throughout your code indicates that there may be some refactoring needed to simplify it a tad.

Finally I would suggest that you create sideways copy of your code and strip it down to a single button to get the click, double-click and hold functions working reliably and posting that back before attempting to do it for all six in one go. That way you might see the woods for the trees even without further help.

Just sayin' :slight_smile:

thx all for replay!!

@arduinofb, will try again to post the whole code but when I wrote the poste and include the code in the tags I got a message that I posted over 9000 charactes, that's why I've attached the code and not posted inside the code tags.

@Ralph_S_Bacon, all those flags are part of 2 function proved to be working correctly. I have a version of the code that
just priny on screen the value of single button (1. 2 or 3) and the which of the multiple buttons have been presse (1 to 6).
so, at the moment, I don't worry about what's inside checkButton() it returns "event" and rreadbutton() it returns "buttonPressed"
To reword what I'm trying to achieve
Singlebutton will have 3 status
1 - select
2 - build
3 - save

6 buttons to select loop 1 2 3 4 5 6

6 relays
6 indicator LEDs
status of a loop relays[]+led[] will be stored in EEPROM sequentially in a for next loop

the idea is a guitar pedal looper with 6 loops.

1,2 and 3 are sequentially select on with click, double click and hold click respectively with singlebutton,

1- select
pressing one of the 6 button it loads from EEPROM, in a for next loop, relay[]status+led[]stats

2- build
pressing one of the 6 button it turns on/off relay[]+led[]

3 save
save the status (on/off) off all relay[]+led[] in a for next loop

what to do on 1, 2 or 3 is done in this way
value of single button (1, 2 or 3) is multiplied by 10
value of pressed button (of the 6) is added to above
switch cases take action based on this sum

so
single = 1
pressed button n. 3
gives a total of 13
read from EEPROM stored value of the 6 relays[], and turn on correspondent LED

single = 2
pressed button n. 6
gives a total of 26
turn on/off relay[] and led[]

single = 3
pressed button n. 6
gives a total of 36
save status of relay[] in a for next loop

LED parts is not yet completed.

....easier to use than to describe...
and here is the whole code...

(still get the error about 9000 characters , trying to separate code from post.

thanks!!!!

and the code

...no way, still getting the 9000 characters exceeded, so here attached

Q3 looper concept.txt (17.8 KB)

I'll be honest, I'm having a bit of trouble following your code.

I've simplified it a bit here, but it could do with some more simplification.

See attached file.

temp2.cpp (14.2 KB)

arduinodlb:
I'll be honest, I'm having a bit of trouble following your code.

See attached file.

You? imagine a noob like me?

arduinodlb:
I've simplified it a bit here, but it could do with some more simplification.

Thx!! I've seen the simplification in the bounce part and the one in the switch code, incredible how 10 lines can be reduced at 2 or 3!!! indeed looking at other's code one can really learn a lot!!

Thanks again will have a go tonight and report back, still need to fix the LED part.
Ciao

HI

trying the code not and looks like this line give an error,

debouncer = new Bounce(); *
this is the error message
Arduino:1.7.8 (Windows 8.1), Scheda:"Arduino Uno"
sketch_feb09a.ino: In function 'void setup()':
sketch_feb09a.ino:85:22: error: no match for 'operator=' (operand types are 'Bounce' and 'Bounce')

sketch_feb09a.ino:85:22: note: candidate is:
In file included from sketch_feb09a.ino:3:0:
C:\Users\arma61\Documents\Arduino\libraries\Bounce2/Bounce2.h:42:7: note: Bounce& Bounce::operator=(const Bounce&)
class Bounce

  • ^*
    C:\Users\arma61\Documents\Arduino\libraries\Bounce2/Bounce2.h:42:7: note: no known conversion for argument 1 from 'Bounce*' to 'const Bounce&'
    I've changed to
    _ debouncer*.update() ;*_
    is this correct ?
    Thanks

arma61:
HI

trying the code not and looks like this line give an error,

debouncer = new Bounce(); *
this is the error message
Arduino:1.7.8 (Windows 8.1), Scheda:"Arduino Uno"
sketch_feb09a.ino: In function 'void setup()':
sketch_feb09a.ino:85:22: error: no match for 'operator=' (operand types are 'Bounce' and 'Bounce')

sketch_feb09a.ino:85:22: note: candidate is:
In file included from sketch_feb09a.ino:3:0:
C:\Users\arma61\Documents\Arduino\libraries\Bounce2/Bounce2.h:42:7: note: Bounce& Bounce::operator=(const Bounce&)
class Bounce

  • ^*
    C:\Users\arma61\Documents\Arduino\libraries\Bounce2/Bounce2.h:42:7: note: no known conversion for argument 1 from 'Bounce*' to 'const Bounce&'
    I've changed to
    _ debouncer*.update() ;_
    is this correct ?
    Thanks
    _
    [/quote]*_
    Oh, sorry. I was having a JAVA moment.
    Just remove lines 82 - 85.

Thx arduinodb!!

and thanks also for this .... :wink: :wink: :wink: :wink: :wink: :wink: :wink:

for (int i=0; i > NUM_DEBOUNCERS; i++)
for (int i=0; i > NUM_DEBOUNCERS; i++)

you did it on purpose to proof and improve my debugging skills, isn't it? :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

Working now!!

And I've even find a solution to have a 3 states LED with one pin changing the delay time in a for next loop (slow, medium and fast blinking) .... and I didn't copied it, just got the idea and developed myself!!! :slight_smile: :slight_smile:

@Ralph_S_Bacon

following you suggestion I've stripped it down to bits and pieces, getting rid of the most of the ifs, and indeed find the solution I was looking for!!! thx

Ciao

arma61:
Thx arduinodb!!

and thanks also for this .... :wink: :wink: :wink: :wink: :wink: :wink: :wink:

for (int i=0; i > NUM_DEBOUNCERS; i++)
for (int i=0; i > NUM_DEBOUNCERS; i++)

you did it on purpose to proof and improve my debugging skills, isn't it? :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

Ummm...., that was not in the code I gave you. That code is wrong in all kinds of ways.

Working now!!

Well done. Congratulations!

arduinodlb:
Ummm...., that was not in the code I gave you. That code is wrong in all kinds of ways.

those lines are not in my code but in the temp2.cpp u attached yesterday... :wink:

for (int i=0; i > NUM_DEBOUNCERS; i++)
{
debouncer*.update();*

  • }*
  • // Get the updated value :*
  • for (int i=0; i > NUM_DEBOUNCERS; i++)*
  • {*
    _ if (debouncer*.read() != 1)_
    _
    {_
    _
    rpressed=i+1;_
    _
    }*_

Anyway who cares, just learnt that........... when you eliminated the impossible, whatever remains......... :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

arduinodlb:
Well done. Congratulations!

Thx!!

arma61:
those lines are not in my code but in the temp2.cpp u attached yesterday... :wink:

for (int i=0; i > NUM_DEBOUNCERS; i++)
{
debouncer*.update();*

  • }*
  • // Get the updated value :*
  • for (int i=0; i > NUM_DEBOUNCERS; i++)*
  • {*
    _ if (debouncer*.read() != 1)_
    _
    {_
    _
    rpressed=i+1;_
    _
    }*_

Anyway who cares, just learnt that........... when you eliminated the impossible, whatever remains......... :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:
[/quote]
Oh yeah. Sorry about that. :slight_smile:

arma61:
@Ralph_S_Bacon

following you suggestion I've stripped it down to bits and pieces, getting rid of the most of the ifs, and indeed find the solution I was looking for!!! thx

Ciao

Excellent, glad I could be of some help :slight_smile:

Hi all!!

this is just to say I've succeeded in completing this sketch!! now I have one button (with relevant 3 status blinkg LED) managing what the other 6 buttons are doing, 6 LEDs on/off in relation with each of the 6 buttons state, and 6 relays on/off with relevant LEDs.... so it's working!!!!

Now, if I only can find an additional pin..... all those in my Arduino UNO are gone..... 6 LEDs, 6 relays, 6 buttons, 1 single buttom and 1 status LED..... :(......

any easy solution to have one additional pin ?

Ciao!!

Have you also used all your analog pins too?

yes! 6+6+6+1+1

Analog pins used as digital, thinking about use one analog with 2 buttons and 2 resistors, I've seen on the net something called "restistor ladder" , though I'd like to be as much precise as possible....

Is using an Arduino Mega a possibility? Only about £10 / $15 from the Far East... and it has something like 50+ GPIO pins!