Using a button to change display information

Hello fellow tinkerers,

I am definitely at the start of my arduino journey but already appreciate all the knowledge that is gathered here.

The task at hand now is designing a digital speedometer. One of the things I'd like it to to is to change the display on pushing a button - so that I can see total distance traveled as well as speed. Since I am an arduino newbie, I dumb the code down to something simple so I can troubleshoot more easily, but this specific task (changing at a push of a button) has me stumped.

Below is my code as it stands:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

const int buttonPin = 7;     // the number of the pushbutton pin

int buttonPushCounter = 0;   // counter for the number of button presses
boolean buttonState = LOW;         // current state of the button
boolean lastButtonState = LOW;     // previous state of the button

void setup() {
  Serial.begin(115200);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

  // Below is the setup code for the screen only along with the welcome message

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  delay(2000);
  display.clearDisplay();

  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  // Display static text
  display.println("Hello");
  display.display();
  delay(2000);
  display.clearDisplay();
  display.display();

}

void loop() {

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH)
    {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;  // add one to counter
      display.clearDisplay();
      display.display();
      if (buttonPushCounter > 2)
      {
        buttonPushCounter = 0;
      }
      Serial.println(buttonPushCounter);
      switch (buttonPushCounter) // choose what to display based on buttonPushCounter value
      {
        case 0:

          display.setTextColor(WHITE);
          display.setCursor(0, 10);
          // Display static text
          display.println("How");
          display.display();


        case 1:

          display.setTextColor(WHITE);
          display.setCursor(0, 10);
          // Display static text
          display.println("Are");
          display.display();

        case 2:

          display.setTextColor(WHITE);
          display.setCursor(0, 10);
          // Display static text
          display.println("You");
          display.display();

      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;
  }


}

Essentially what I want from this code is to display a welcome message (in this instance "Hello"), then display a message "How", and with a button push to flick through to "Are" and then onto "You", and on the next button push I'd like to go back to "How".

Things that it does at the moment:
Displays Hello & Goes to all black
At a button press 1: Displays "How" and "Are" at the same time
Button Press 2: Displays "you"
Further button presses just flick between the two displays mentioned above.

What am I missing here? I read the code out loud a few times, makes sense in my head but clearly something is wrong.

All help is much appreciated.

Thanks,

You have left out the breaks at the end of each case so the code falls through and executes the code for all of the cases

What Bob said. In case it is not clear:

Thanks, that resolved half of my problem!

However, the other issue still persists -> after displaying the Hello message, the display then goes to blank, and on first button press it's "Are".

I would like to display "How" automatically after "Hello" - in my head, it's case 0, buttonPushCounter is equal to 0, so why isn't it displaying?

Look at you code and follow (in your mind or on paper) what the value of the buttonPushCounter variable is.

buttonPushCounter is zero when you start running the code, but when a switch is pressed you increment the count BEFORE the case statement. So the first time you use it in the case statement it is already 1.

Thanks marco_c, you're right on the money - the display code was burried in an if statement that was only triggered when the button was pressed. I took it out, it works like a dream.

For those interested and for completeness, here's the working code:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

const int buttonPin = 7;     // the number of the pushbutton pin

int buttonPushCounter = 0;   // counter for the number of button presses
boolean buttonState = LOW;         // current state of the button
boolean lastButtonState = LOW;     // previous state of the button

void setup() {
  Serial.begin(115200);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

  // Below is the setup code for the screen only along with the welcome message

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  delay(2000);
  display.clearDisplay();

  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  // Display static text
  display.println("Hello");
  display.display();
  delay(2000);
  display.clearDisplay();
  display.display();

}

void loop() {

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  Serial.println(buttonPushCounter);
  switch (buttonPushCounter) // choose what to display based on buttonPushCounter value
  {
    case 0:

      display.setTextColor(WHITE);
      display.setCursor(0, 10);
      // Display static text
      display.println("How");
      display.display();
      break;


    case 1:

      display.setTextColor(WHITE);
      display.setCursor(0, 10);
      // Display static text
      display.println("Are");
      display.display();
      break;

    case 2:

      display.setTextColor(WHITE);
      display.setCursor(0, 10);
      // Display static text
      display.println("You");
      display.display();
      break;

  }

  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH)
    {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;  // add one to counter
      display.clearDisplay();
      display.display();
      if (buttonPushCounter > 2)
      {
        buttonPushCounter = 0;
      }

    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;
  }


}

You had better believe it! :sunglasses:

Marco is a display specialist, having written - among other things - the definitive "Parola" and associated libraries for the MAX7219-based matrix displays.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.