2

I'm currently getting started with Arduino, so I do not have very much knowledge. In this project I have a receiver and a transmitter with 2 buttons. So, I'd like to switch something to on, when I press the A button. If A is pressed longer I'd like something else to happen. Same thing goes on with the B button, but for some reasons, when I long press A, then B gets triggered and I have no clue why...

Btw. this code is not purely made by me, but copied and edited for my use. If anyone could help me I'd be very happy :)

code:

//ButtonAEvents

long prevA = 0;
long prevB = 0;
int buttonStateA = 0; // 0 = not pressed   --- 1 = long pressed --- 2 short pressed
int buttonStateB = 0; // 0 = not pressed   --- 1 = long pressed --- 2 short pressed
int ButtonAPin = 2;
int ButtonBPin = 3;
int DURATION_A_IN_MILLIS = 1000;
int DURATION_B_IN_MILLIS = 1000;

//ONE TIME SETUP

void setup()
{
    pinMode(ButtonAPin, INPUT);
    pinMode(ButtonBPin, INPUT);
    Serial.begin(9600);
}

//LOOP BEGIN

void loop()
{

    //BUTTON A CHECK
    buttonStateA = 0;
    if (digitalRead(ButtonAPin)) {
        prevA = millis();
        buttonStateA = 1;
        while ((millis() - prevA) <= DURATION_A_IN_MILLIS) {
            if (!(digitalRead(ButtonAPin))) {
                buttonStateA = 2;
                break;
            }
        }
    }

    if (!buttonStateA) {
        // Nothin pressed
    }
    else if (buttonStateA == 1) {
        //LONG PRESS

        Serial.println("LongpressA");
        digitalWrite(A1, HIGH);
        delay(100);
    }
    else if (buttonStateA == 2) {
        //SHORT PRESS
        digitalWrite(A1, HIGH);
        Serial.println("ShortpressA");
        delay(100);
    }

    //BUTTON B CHECK
    buttonStateB = 0;
    if (digitalRead(ButtonBPin)) {
        prevB = millis();
        buttonStateB = 1;
        while ((millis() - prevB) <= DURATION_B_IN_MILLIS) {
            if (!(digitalRead(ButtonBPin))) {
                buttonStateB = 2;
                break;
            }
        }
    }

    if (!buttonStateB) {
        // Nothin pressed
    }
    else if (buttonStateB == 1) {
        //SHORT PRESS

        Serial.println("LongpressB");
        delay(100);
    }
    else if (buttonStateB == 2) {
        //LONG PRESS
        digitalWrite(A1, LOW);
        Serial.println("ShortpressB");
        delay(100);
    }
}
  • All no reasons for your problem, so that's why this is a comment: you switched the words LongpressB and ShortpressB in the serial print statements, also it is adviced not to use the ! operator for integer (use buttonStateB == 0), or even better in your case a switch statement. – Michel Keijzers Sep 16 at 19:08
  • upvote for well written question – jsotola Sep 16 at 19:11
  • What are your hardware connections? The button pins are assumed to be HIGH when pressed and LOW when not pressed. You also do not use pullups/downs, so that must be external. – Maximilian Gerhardt Sep 16 at 19:16
  • 1
    I tested your code on my Arduino Uno with 2 buttons which go LOW when pressed and are pulled up externally, inverted the logic in the digitalRead() in the code and it works as expected. If pressing one button triggers the other one, I'd search for a fault in your hardware setup (crosstalk, accidentally bridged, wrong button polarity,..) Also signal bouncing can be an issue (and can be suppressed with capacitors) – Maximilian Gerhardt Sep 16 at 19:19
  • 1
    Hi again, I tried using the circuit diagram function, but I'm not very familiar with the symbols and electronics behind it, so I drew it in Photoshop, I hope it's understandable tho, sorry again... prntscr.com/p7vc8m – Nea Yjung Sep 18 at 17:47
1

Not sure if this is just an academic exercise on your part to learn (which is totally cool), but if you're just trying to move forward and get something working you may consider one of the many excellent button libraries out there for Arduino.

I recently used a library called ButtonKing that allowed me to do some very sophisticated menu interactions on a MKR1010 with a display and single button interface. There are several other libraries out there as well that take care of debouncing, long clicks, double clicks, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.