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,