/*
Arduino | coding-help
changing text on OLED screen using buttons
creaxyy — 6/14/24 at 6:03 AM
so, ive never had these type of problems so im very
confused what to do. Its not any fault of screen or
buttons, its something with the code. I tried asking
chat gpt to fix it, but he got it even worse. When this
button on left got clicked it should change the numer
for example from 2 to 3. This button on right is supposed
to do it from 3 to 2. Max number that there is should
be 7 and min is 1. Can someone help me?
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int UP_BTN_PIN = 7;
const int DN_BTN_PIN = 6;
int currentGear = 1;
int oldUpBtn = HIGH;
int oldDnBtn = HIGH;
void setup() {
Serial.begin(9600);
pinMode(UP_BTN_PIN, INPUT_PULLUP);
pinMode(DN_BTN_PIN, INPUT_PULLUP);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
display.clearDisplay();
display.display();
displayCurrentGear();
}
void loop() {
int upButtonState = digitalRead(UP_BTN_PIN);
if (upButtonState != oldUpBtn) {
oldUpBtn = upButtonState;
if (upButtonState == LOW) {
Serial.println("Up button pressed");
if (currentGear < 7) {
currentGear++;
displayCurrentGear();
}
}
delay(20); // debounce
}
int downButtonState = digitalRead(DN_BTN_PIN);
if (downButtonState != oldDnBtn) {
oldDnBtn = downButtonState;
if (downButtonState == LOW) {
Serial.println("Down button pressed");
if (currentGear > 1) {
currentGear--;
displayCurrentGear();
}
}
delay(20);
}
//Serial.print("Up button state: ");
//Serial.println(upButtonState);
//Serial.print("Down button state: ");
//Serial.println(downButtonState);
}
void displayCurrentGear() {
display.setTextSize(2);
display.setCursor(40, 0);
display.print(F("BIEG:"));
display.setTextSize(4);
display.setCursor(55, 30);
display.print(currentGear);
display.display();
}