I am working on a project that requires the screen of an OLED display to switch to the next screen with the push of a button and I can't seem to figure out how to code it. Any help would be appreciated. I will paste my code below.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define height 128
#define width 32
int button = 2;
int screen_current = 1;
int screen_total = 3; // Change this to however many screens you have
int btn_allow = 1; // Don't change this
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
pinMode(button, INPUT_PULLUP);
display.clearDisplay();
}
void loop() {
// put your main code here, to run repeatedly:
screen_current = 1;
while(digitalRead(btn_pin) == HIGH && btn_allow = 1) // Button depressed
{
// Go to the next screen
screen_current++;
// Stop the button from working again until you let go
btn_allow = 0;
// Reset to screen one
if(screen_current > screen_total)
{
screen_current = 1;
}
}
if(digitalRead(btn_pin) == LOW) // Button not depressed
{
// Allow the button to work again
btn_allow = 1;
}
if(screen_current == 1)
{
// Whatever you want screen 1 to do
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(30,9);
display.println("SCREEN 1");
display.display();
}
if(screen_current == 2)
{
// Whatever you want screen 2 to do
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(30,9);
display.println("SCREEN 2");
display.display();
}
if(screen_current == 3)
{
// Whatever you want screen 3 to do
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(30,9);
display.println("SCREEN 3");
display.display();
}
}
Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE
The pin is kept LOW by the connection to GND via the resistor and pressing the button takes the pin HIGH, but the connection to GND is via the 10K resistor. That is not a short
The code works by detecting the pin connection to 5v as HIGH
I am still a beginner trying to learn some code to make some projects as a hobby so I don't have much experience.
I also ran into an error with the code, but I was unsure what the error meant and how to solve it.
I plan to have a couple of screens of text to change between with a button. I am unsure how many screens I will use right now but I wanted to learn how to code a simple screen switch with a button.
I am planning to have a button move to the next page and another button to return to the last page.
I'm also confused about what you mean when you say a counter begins at 0. Could you please elaborate?
Thanks for the reply. Any tips or solutions would be appreciated!