Attiny85 start/stop timer using chatgpt

Hello, I was trying to create a start/stop watch sketch in which when you press a button it will start, stop, then start the timer again when the button is pressed and it will display it on an OLED. I actually got this code using chatgpt. When I verify the code it says 'Tiny4kOLED' has not been declared. It will say that on any part that has Tiny4kOLED starting from the bottom of the code. If I comment the very bottom line that has Tiny4kOLED, it will just say the same message on the line above it. How can I fix this? Here is the code

#include <Tiny4kOLED.h>

#define BUTTON_PIN 2 // Connect the button to pin 2

unsigned long startTime = 0;
unsigned long elapsedTime = 0;
bool isRunning = false;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Tiny4kOLED::begin();
  Tiny4kOLED::clear();
  Tiny4kOLED::textWrap(false);
  Tiny4kOLED::setTextSize(1);
  Tiny4kOLED::setTextColor(TINY4K_OLED_WHITE);
  Tiny4kOLED::setCursor(0, 0);
  Tiny4kOLED::println("Stopwatch");
}

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    if (!isRunning) {
      // Start the stopwatch
      startTime = millis();
      isRunning = true;
    } else {
      // Stop the stopwatch
      elapsedTime = millis() - startTime;
      isRunning = false;
    }
    delay(200); // Debounce delay
  }

  if (isRunning) {
    // Display elapsed time
    Tiny4kOLED::clear();
    Tiny4kOLED::setCursor(0, 0);
    Tiny4kOLED::print("Time: ");
    Tiny4kOLED::print(elapsedTime / 1000); // Convert to seconds
    Tiny4kOLED::print(" s");
  } else {
    // Display "Paused"
    Tiny4kOLED::clear();
    Tiny4kOLED::setCursor(0, 0);
    Tiny4kOLED::println("Paused");
  }
}

I just changed the code to this. The oled display on an attiny must have this. I know because of several projects that I have with an OLED on an attiny. It did verify correctly but when I turn on the power, nothing will display. I know that the code should have a line in which it will clear out text. I'm guessing that's the case? Here is the code.

#include <Tiny4kOLED.h>

#define BUTTON_PIN 2 // Connect the button to pin 2

unsigned long startTime = 0;
unsigned long elapsedTime = 0;
bool isRunning = false;

void setup() {
   oled.begin();
  delay(1000);
  pinMode(BUTTON_PIN, INPUT);
  oled.setFont(FONT6X8);
  oled.clear();
  oled.switchRenderFrame();
  oled.clear();
  oled.switchRenderFrame();
  oled.setCursor(0, 0);
  oled.print("Time: ");
  oled.print(elapsedTime / 1000);
  oled.print(" s");
}

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    if (!isRunning) {
      // Start the stopwatch
      startTime = millis();
      isRunning = true;
    } else {
      // Stop the stopwatch
      elapsedTime = millis() - startTime;
      isRunning = false;
    }
    delay(200); // Debounce delay
  }

  if (isRunning) {
    // Display elapsed time
    oled.clear();
    oled.setCursor(0, 0);
    oled.print("Time: ");
    oled.print(elapsedTime / 1000); // Convert to seconds
    oled.print(" s");
  } else {
    // Display "Paused"
    oled.clear();
    oled.setCursor(0, 0);
    oled.println("Paused");
  }
}

You must object.display() the buffer data, in your case, oled.display()

1 Like

Please, post the picture of your OLED to check if it is I2C Bus driven or SPI Port driven.

The OLED only has

GND VCC SCL SDA

1 Like

Have you managed to make your Stopwatch working? Do you need a Tutorial?

1 Like

@spiderman288888

Check if the Tutorial of the attached file is helpful to you.
Attiny85-OLED Stop Watch (1).pdf (176.3 KB)

Why not use the inbuilt pullup resistors for the buttons and some external pullups on the I2C bus ?

Apart from the built-in pull-up resistors, the D+/D- lines of the Digispark Board have additional R-D network that has convinced me to install external pull-down in order to make the push button working.

I would suggest you start with some of the tutorials that are on line, sorry to say some are not so good but many are very good. Start by learning
the basics, you will need to control outputs as well as interpret inputs such as reading a switch, receiving a message etc. Start with the LED, they are not expensive and there is even one on most of the Arduinos. At this point you should have also found several tutorials on basic electronics that you
have gone through.

You should acquire a copy of the Arduino Cookbook and go through that. I have no clue as to how fast you will learn this but it will not be a one night study. During this process you will learn what an IDE is and how to use it to generate and upload your code to the Arduino. Let us know how you progress. For more help check this link: https://docs.arduino.cc/learn/starting-guide/getting-started-arduino/

Show where you connected these on the ATtiny85.

Why is no object declared? Is "oled" (lower case) assumed?

Have you run the sketch? It is a working sketch under Digispark ATtiny85 Dev Board. If the sketch runs and there is output on the OLED, then it means that C++ affairs have been taken care of by the Library

Note:

In C++, an object is created from Class Name (aka user defined data type).
Method is applied on an object.
object.method = function

It looks like the instantiation is here in the library:

SSD1306Device oled(&tiny4koled_begin_wire, &tiny4koled_beginTransmission_wire, &datacute_write_wire, &datacute_endTransmission_wire);

From: Tiny4kOLED/src/Tiny4kOLED_Wire.h at master · datacute/Tiny4kOLED · GitHub

I would, however, have expected an extern declaration in the .h file and the actual definition in a .cpp file.

1 Like

14 posts were split to a new topic: To Instantiate or To Not Instantiate

Have you installed the library?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.