0

I'm using an OLED display connected to a push button. I am trying to get my display to show two different values independently by using the push button to switch between them. Here is a snap of my code from the loop function:

    if (buttonState == HIGH) {
    x=x+1;
  }    
  if (x == 1) {
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.setTextSize(2);
  display.print(T);
  display.print(" degree");
  display.display();
  display.print(x);

}
    else if (x == 2) {
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.setTextSize(2);
  display.print(perventage);
  display.print(" %");
  display.display();
  display.print(x);
}
    else  {
      x=1; 
      }


  delay(10);
}

x is defined as 1 initially out side the loop. What happens is that when the circuit is turned on, the screen displays the value of T then what should happen is when I push the button it switches to the second value which is percentage, and I push again to go back to T. My issue is that the Arduino doesn't response to the push button all of the time sometimes it takes 1 click and other times it takes 6 or 8 fast clicks to switch between them and it doesn't stick to a specific number of pushes to switch. What would you suggest me to change to fix it.

Edit: here is pic of my wiring diagram Wiring diagram

  • How is your button wired? – Majenko Aug 4 at 15:08
  • It should switch between the values from 1 push as it's designed from the code, but In reality it rarely switches between values from 1 push, it takes 4,5,6,8 pushes just to get it to switch between values. I don't understand why it behave like that – Jacob Aug 4 at 15:15
  • Yes, that's what you said in your question. But I asked "How is your button wired?", not "Reiterate what you said in your question about how you want it to behave". – Majenko Aug 4 at 15:16
  • Please show a picture of how you have your button connected to the Arduino. – Majenko Aug 4 at 15:18
  • how i the push button connected to the oled display? – jsotola Aug 4 at 15:18
0

One core problem is that you have a simple "If the button is HIGH then cycle through the numbers". The longer you hold the button the more times it will cycle through the numbers. It's pure 50/50 chance that you happen to release the button on a different number to the one it started on.

Instead you need to look for a change in the state of the button. Has it changed from LOW to HIGH:

static uint8_t oldState = LOW;

uint8_t buttonState = digitalRead(BUTTON_PIN);
if (buttonState != oldState) {
    oldState = buttonState;

    if (buttonState == HIGH) {
        // This only runs once at the moment of pressing
        // the button - not continuously while you're
        // holding the button in.
        // Cycle through your displays here.
    }
}
1

You are reading the raw button state without debouncing it, so you may be getting 2 (or 4, or ...) pulses so quickly - off the same button-push - that the display or your eye can't respond that quickly. Here is Nick Gammon's tutorial on switches and buttons, including debouncing.

Also make sure you use a pull-down resistor on the button input-pin. Floating inputs are noise and will give you a lot of false signals.

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.