Hello im new to coding and i am working on a project with 2 buttons, arduino uno and oled display 1.3" 128x64 SPI. Everything is working as it should except for one issue. i have 3 differetn display layouts switching every 10 seconds. the first two work fine but the last one glitches in a weird way. the text on it disappears for a few miliseconds continuosly. mainly the last three lines. i will provide the code and some pictures.
this is the layout that gives me issues,
here is my code (there are no errors in the code):
// 2 screens, two buttons no monitor serial, everything works perfectly, motivacni quotes
#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <EEPROM.h> // For persistent storage
// Constructor for SH1106 SPI OLED
U8G2_SH1106_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
// Button pin
const int resetButtonPin = 3; // Reset button connected to pin 3
const int buttonPin = 7; // Button connected to pin 2
bool lastButtonState = LOW; // Previous button state
// Variables for cigarette counts and health
int dnesCigarettes = 0; // Cigarettes for the day
int totalCigarettes = 0; // Total cigarettes
int healthBarPixels = 0; // Pixels filled in the health bar (persistent)
int nicotineMg = 0; // Total mg of nicotine
int tarMg = 0; // Total mg of tar
int caloriesBurned = 0; // Total calories burned
// Rectangle dimensions and position
const int rectX = 25; // Start 25 pixels from the left
const int rectY = 50; // Y position
const int rectWidth = 98; // Width of the rectangle
const int rectHeight = 10; // Height of the rectangle
// Timing for screen switching
unsigned long lastSwitchTime = 0; // Last time the screen switched
int currentScreen = 0; // Tracks the current screen (0: Main, 1: Secondary, 2: New Screen)
void setup(void) {
u8g2.begin(); // Initialize the display
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor
pinMode(resetButtonPin, INPUT_PULLUP); // Set reset button pin as input with pull-up resistor
}
void loop(void) {
// Check if reset button is pressed
if (digitalRead(resetButtonPin) == LOW) { // Reset button pressed
totalCigarettes = 0; // Reset total count
healthBarPixels = 0; // Reset health bar
dnesCigarettes = 0;
nicotineMg = 0;
tarMg = 0;
caloriesBurned = 0;
delay(1000); // Debounce delay
}
handleButtonPress(); // Call to handle button press
if (millis() - lastSwitchTime > 10000) {
currentScreen = (currentScreen + 1) % 3; // Cycle between two screens
lastSwitchTime = millis(); // Update the last switch time
}
if (currentScreen == 0) {
drawMainScreen();
} else if (currentScreen == 1) {
drawPixelArtScreen();
} else if (currentScreen == 2) {
drawStatisticsScreen();
}
delay(100); // Small delay to avoid flickering
}
int motivationalQuoteIndex = 0; // Tracks the current quote index
const char* motivationalQuotes[] = {
"VICE DEHTU = VICE ZAZITKU!",
"DYCHEJ DO TOHO! JSI SAMPION!",
"TVE PLICE SI TO ZASLOUZI!",
"JEN TAK DAL!",
"DOKOURIL JSI... JSI VITEZ!",
"KAZDY POTAH SE POCITA!",
"KOURENI (NE)ZABIJI"
};
void displayMotivationalQuote() {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr); // Smaller font for better fit
// Break the quote into multiple lines if needed
const char* quote = motivationalQuotes[motivationalQuoteIndex];
char firstLine[64] = "";
char secondLine[64] = "";
char thirdLine[64] = "";
int len = strlen(quote);
if (len > 20) { // Split intelligently by spaces
int breakPoint = len / 2;
while (breakPoint > 0 && quote[breakPoint] != ' ') {
breakPoint--; // Find nearest space
}
strncpy(firstLine, quote, breakPoint);
firstLine[breakPoint] = '\0'; // Null-terminate
const char* remaining = "e[breakPoint + 1];
int remainingLen = strlen(remaining);
if (remainingLen > 20) {
int secondBreakPoint = remainingLen / 2;
while (secondBreakPoint > 0 && remaining[secondBreakPoint] != ' ') {
secondBreakPoint--; // Find nearest space
}
strncpy(secondLine, remaining, secondBreakPoint);
secondLine[secondBreakPoint] = '\0'; // Null-terminate
strcpy(thirdLine, &remaining[secondBreakPoint + 1]);
} else {
strcpy(secondLine, remaining);
}
} else {
strcpy(firstLine, quote);
}
// Calculate x position for centering each line
int textX1 = (128 - u8g2.getStrWidth(firstLine)) / 2;
int textX2 = (128 - u8g2.getStrWidth(secondLine)) / 2;
int textX3 = (128 - u8g2.getStrWidth(thirdLine)) / 2;
// Display the first line
u8g2.setCursor(textX1, 20); // Adjusted vertical position for first line
u8g2.print(firstLine);
// Display the second line if it exists
if (strlen(secondLine) > 0) {
u8g2.setCursor(textX2, 36); // Adjusted vertical position for second line
u8g2.print(secondLine);
}
// Display the third line if it exists
if (strlen(thirdLine) > 0) {
u8g2.setCursor(textX3, 52); // Adjusted vertical position for third line
u8g2.print(thirdLine);
}
u8g2.sendBuffer(); // Send data to the display
delay(5000); // Show the quote for 5 seconds
// Increment the quote index
motivationalQuoteIndex = (motivationalQuoteIndex + 1) % 6;
}
void handleButtonPress() {
bool currentButtonState = digitalRead(buttonPin) == LOW; // Read button state
if (currentButtonState && !lastButtonState) { // Detect button press
displayMotivationalQuote();
currentScreen = 0; // Switch to main screen
lastSwitchTime = millis(); // Reset the timer
dnesCigarettes++; // Increment daily count
totalCigarettes++; // Increment total count
nicotineMg += 2; // Increment nicotine count
tarMg += 11; // Increment tar count
caloriesBurned += 10; // Increment calories count
// Update health bar
if (totalCigarettes % 20 == 0 && healthBarPixels < rectWidth - 2) {
healthBarPixels += 1; // Add one pixel to the health bar for every 20 presses
}
}
lastButtonState = currentButtonState; // Update last button state
}
void drawMainScreen() {
u8g2.clearBuffer(); // Clear the internal memory
// Draw "DNES" (Centered Text)
u8g2.setFont(u8g2_font_ncenB14_tr); // Medium font
int dnesX = (128 - u8g2.getStrWidth("DNES: 00")) / 2; // Calculate centered X position
u8g2.setCursor(dnesX, 40);
u8g2.print("DNES: ");
u8g2.print(dnesCigarettes);
// Draw "CELKEM" (Top Left)
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.setCursor(0, 10);
u8g2.print("CELKEM: ");
u8g2.print(totalCigarettes);
// Draw Health Bar
u8g2.setFont(u8g2_font_ncenB08_tr); // Font for HP
u8g2.setCursor(0, 58); // Adjusted to align with the rectangle
u8g2.print("HP");
// Draw transparent rectangle (outline only)
u8g2.drawFrame(rectX, rectY, rectWidth, rectHeight);
// Draw filled part of the rectangle
if (healthBarPixels > 0) {
u8g2.drawBox(rectX + 1, rectY + 1, healthBarPixels, rectHeight - 2);
}
u8g2.sendBuffer(); // Transfer internal memory to display
}
void drawPixelArtScreen() {
u8g2.clearBuffer(); // Clear the internal memory
// Define pixel ranges for each line
u8g2.drawHLine(22, 23, 106); // Line 23
u8g2.drawHLine(22, 24, 106); // Line 24
u8g2.drawHLine(21, 25, 4); // Line 25: Part 1
u8g2.drawHLine(112, 25, 16); // Line 25: Part 2
u8g2.drawHLine(21, 26, 3); // Line 26: Part 1
u8g2.drawHLine(25, 26, 2); // Line 26: Part 2
u8g2.drawHLine(112, 26, 16); // Line 26: Part 3
u8g2.drawHLine(20, 27, 1); // Line 27: Part 1
u8g2.drawHLine(23, 27, 3); // Line 27: Part 2
u8g2.drawHLine(113, 27, 15); // Line 27: Part 3
u8g2.drawHLine(20, 28, 7); // Line 28
u8g2.drawHLine(113, 28, 14); // Line 28
u8g2.drawHLine(19, 29, 2); // Line 29: Part 1
u8g2.drawHLine(114, 29, 14); // Line 29: Part 2
u8g2.drawHLine(22, 30, 1); // Line 30
u8g2.drawHLine(113, 30, 14); // Line 30
u8g2.drawHLine(22, 31, 3); // Line 31
u8g2.drawHLine(113, 31, 13); // Line 31
u8g2.drawHLine(21, 32, 4); // Line 32
u8g2.drawHLine(112, 32, 15); // Line 32
u8g2.drawHLine(20, 33, 6); // Line 33
u8g2.drawHLine(111, 33, 16); // Line 33
u8g2.drawHLine(20, 34, 7); // Line 34
u8g2.drawHLine(111, 34, 15); // Line 34
u8g2.drawHLine(21, 35, 3); // Line 35
u8g2.drawHLine(113, 35, 14); // Line 35
u8g2.drawHLine(22, 36, 1); // Line 36
u8g2.drawHLine(114, 36, 14); // Line 36
u8g2.drawHLine(22, 37, 2); // Line 37
u8g2.drawHLine(113, 37, 13); // Line 37
u8g2.drawHLine(23, 38, 4); // Line 38
u8g2.drawHLine(112, 38, 16); // Line 38
u8g2.drawHLine(23, 39, 6); // Line 39
u8g2.drawHLine(111, 39, 15); // Line 39
u8g2.drawHLine(24, 40, 8); // Line 40
u8g2.drawHLine(110, 40, 17); // Line 40
u8g2.drawHLine(22, 41, 106); // Line 41 (Bottom line)
u8g2.sendBuffer(); // Transfer the buffer to the display
}
void drawStatisticsScreen() {
u8g2.clearBuffer(); // Clear the internal memory
// Adjusted Y positions for equal spacing
int topTextY = 10; // Align top of "PRUMERNE" with "CELKEM"
// Draw "PRUMERNE:"
u8g2.setFont(u8g2_font_ncenB08_tr); // Medium font
u8g2.setCursor(0, topTextY);
u8g2.print("PRUMERNE:");
int bottomLineY = 60; // Bottom of the last line (aligned with the rectangle's bottom)
int lineSpacing = (bottomLineY - topTextY) / 3; // Calculate uniform spacing
// Draw "NIKOTINU"
u8g2.setCursor(0, topTextY + lineSpacing); // Next line
u8g2.print(nicotineMg);
u8g2.print("mg NIKOTINU");
// Draw "DEHTU"
u8g2.setCursor(0, topTextY + 2 * lineSpacing); // Next line
u8g2.print(tarMg);
u8g2.print("mg DEHTU");
// Draw "KALORII"
u8g2.setCursor(0, topTextY + 3 * lineSpacing); // Next line
u8g2.print(caloriesBurned);
u8g2.print(" KALORII");
u8g2.sendBuffer(); // Transfer internal memory to display
}


