In Wokwi my code only does the beginning

#include <Adafruit_GFX.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
int secs = 0;
Adafruit_SSD1306 oled = Adafruit_SSD1306(128, 64, &Wire, OLED_RESET);

void setup()
{
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.display();
  delay(500);
}

void loop()
{
  oled.setCursor(10,10);
  oled.print("Hello, Arduino Uno R3!");
  oled.display();

  oled.setCursor(40,10);
  oled.print("Timer:");
  oled.print(secs);
  oled.print(" sec");
  oled.display();
  delay(1000);
  secs += 1;
}

I want to make a timer with an OLED SSD1306 display and an Arduino. The code only does the beginning. Can you help me fix this?

do you want to print this every second ? this should be in the setup

also you never erase the display so the prints will be in overlay...

try something like this, typed here fully untested

#include <Adafruit_GFX.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
uint32_t  secs = 0;
Adafruit_SSD1306 oled = Adafruit_SSD1306(128, 64, &Wire, OLED_RESET);

void setup() {
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.clearDisplay();
  oled.setCursor(10,10);
  oled.print("Hello, Arduino Uno R3!");
  oled.display();
  delay(500);
}

void loop() {
  oled.fillRect(0, 30, 128, 20, BLACK); // make sure you erase the time area
  oled.setCursor(40,30);
  oled.print("Timer: ");
  oled.print(secs);
  oled.print(" sec");
  oled.display();
  delay(1000);
  secs += 1;
}

you could avoid some blinking by keeping the text always in the same place (here "sec" will move around as you add digits) and only erasing the part for the number of seconds

What exactly is "the beginning"?

I see what the likely problem is. Had you had the common courtesy to respond on your previous topics on this matter, an answer would have been forthcoming.

2 Likes

Add

oled.setTextColor(SSD1306_WHITE,SSD1306_BLACK);

before

oled.setCursor(40,10);

#include <Adafruit_GFX.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
int secs = 0;
Adafruit_SSD1306 oled (128, 64, &Wire, OLED_RESET);

void setup()
{
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.display();
  delay(500);
  oled.clearDisplay();
  oled.setTextSize(1);
}

void loop()
{
  oled.setTextColor(SSD1306_WHITE);

  oled.setCursor(0,10);
  oled.print("Hello, Arduino Uno R3");

  oled.setCursor(40,20);
  oled.print("Timer:");

  oled.setCursor(40,30);
  oled.setTextColor(SSD1306_WHITE);
  oled.print(secs);
  oled.print(" sec");
  oled.display();

  delay(1000);

  oled.setCursor(40,30);
  oled.setTextColor(SSD1306_BLACK);
  oled.print(secs);
  oled.print(" sec");
  oled.display();

  secs += 1;
}

the number looks bad:

Not with my code.