0

So I am programming an OLED display from my Arduino Uno. I am trying to have a button that switches the different words on the display. (The words are just placeholders for programs as of now.) My program has a button that changes a counter, and an if test to see what the counter is so it can display the appropriate word. The problem is, whenever I press the button, the display turns black and doesn't do anything anymore. When I start/reset the Arduino, it displays TEMPERATURE, but it should say TIME.

#include <OLED_I2C.h>

OLED  myOLED(SDA, SCL, 8);

extern uint8_t SmallFont[];
extern uint8_t MediumNumbers[];
extern uint8_t BigNumbers[];

//constants:
const int buttonPin = 2;

//variables:
byte oldButtonState = HIGH;
int screenNumber = 0;
byte buttonState = 0;

void setup()
{
  myOLED.begin();
  myOLED.setFont(SmallFont);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() { //myOLED.print formats with (string, text_placement,     pixel_location)
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // Test if the button is being pressed
  if (buttonState == HIGH) {
    myOLED.clrScr();
    myOLED.update();
    oldButtonState = buttonState;
    if (screenNumber != 2) {
      ++screenNumber;
    }
    else {
      screenNumber = 0;
    }
    //delay a little to avoid bouncing
    delay(200);
    }

  //Test for the screenNumber variable and change to the
  //according screen
  if (screenNumber == 0) {
    myOLED.print("TIME", CENTER, 16);
    myOLED.update();
  }
  else if (screenNumber == 1) {
    myOLED.print("TEMPERATURE", CENTER, 16);
    myOLED.update();
  }
  else if (screenNumber == 2) {
    myOLED.print("DIRECTION", CENTER, 16);
    myOLED.update();
  }
  else {
    myOLED.print("ERROR", CENTER, 16);
    myOLED.update();
  }
}
2
  • What is your question? What is your sketch doing that it's not suppose to be doing?
    – VE7JRO
    Aug 16, 2018 at 21:41
  • have a close look at your code and try to figure out why it is displaying screen #1
    – jsotola
    Aug 16, 2018 at 22:19

3 Answers 3

-1

I didn't see a problem in the code - but maybe I overlooked something.

Maybe your code is not the problem but your hardware. Did you make sure the input is connected to GND if it the button is not pressed?

Notes and Warnings

If the pin isn’t connected to anything, digitalRead() can return either HIGH or LOW (and this can change randomly).

https://www.arduino.cc/reference/en/language/functions/digital-io/digitalread/

I suggest you use serial to show the value of buttonState after this line

buttonState = digitalRead(buttonPin);

Maybe it is not what you expect it to be.

4
  • this doesn't apply. he has INPUT_PULLUP
    – Juraj
    Aug 17, 2018 at 4:59
  • I changed the input mode to INPUT and added the Serial.print(buttonState); but it only prints 0 even when I am pressing the button. Any clue why that might be?
    – FoxxTech
    Aug 18, 2018 at 17:05
  • 1
    So I switched the side that the wire connecting to PIN 2 was and now it says 1 when I am not pressing it and 0 when I am.
    – FoxxTech
    Aug 18, 2018 at 17:06
  • Good that you found the problem. Sometimes we forget to double check the little things (they connections) and concentrate only on the code...
    – Edgar
    Aug 18, 2018 at 23:28
1

You have pinMode(buttonPin, INPUT_PULLUP); so I assume, you connected the button between pin and ground. With pull-up the pin will read HIGH if it is not grounded. If the button is pushed, grounding the button pulls it LOW. So you should test if the button is pressed with comparing to LOW.

We have this misleading idea in our heads that active is HIGH and not active is LOW. But you can see it this way:

  • button pushed down (low) is LOW
  • button not pushed is up (high) reading HIGH
0

I fixed the problem. It turned out that the value being returned by the button was 1 when it was not being pressed and 0 when it was being pressed. My new code is:

#include <OLED_I2C.h>

OLED  myOLED(SDA, SCL, 8);

extern uint8_t SmallFont[];
extern uint8_t MediumNumbers[];
extern uint8_t BigNumbers[];

//constants:
const int buttonPin = 2;
const int debug = 0;

//variables:
int screenNumber = 0;
byte buttonState = 0;

void setup()
{
  myOLED.begin();
  myOLED.setFont(SmallFont);
  pinMode(buttonPin, INPUT);
  myOLED.clrScr();
  myOLED.print("TIME", CENTER, 16);
  myOLED.update();
  Serial.begin(9600);
}

void loop() { //myOLED.print() formats with (string, text_placement, pixel_location)
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);
  if (debug == 1) {
    myOLED.clrScr();
    myOLED.print("Start", CENTER, 16);
    myOLED.update();
    delay(debugDelay);
  }
  // compare the buttonState to its previous state
  if (buttonState == 0) {
    if (screenNumber < 2) {
      screenNumber = screenNumber + 1;
    }
    else {
      screenNumber = 0;
      }
    //delay a little to avoid bouncing
    delay(100);
    }

  //Test for the screenNumber variable and change to the
  //according screen

  if (screenNumber == 0) {
    myOLED.clrScr();
    myOLED.print("TIME", CENTER, 16);
    myOLED.update();
  }
  else if (screenNumber == 1) {
    myOLED.clrScr();
    myOLED.print("TEMPERATURE", CENTER, 16);
    myOLED.update();
  }
  else if (screenNumber == 2) {
    myOLED.clrScr();
    myOLED.print("DIRECTION", CENTER, 16);
    myOLED.update();
  }
  else {
    myOLED.clrScr();
    myOLED.print("ERROR", CENTER, 16);
    myOLED.update();
  }
}
2
  • 1
    and what did I write in my answer? you accepted the wrong answer
    – Juraj
    Aug 18, 2018 at 18:23
  • Edgar told me to use the Serial.print() to find out what the value of the button was. That helped me more. If you had just said it is the inverse of what I expected it would have made more sense.
    – FoxxTech
    Aug 19, 2018 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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