Hello, I'm trying to shrink this project down from an arduino nano to attiny85. It's an oled with 2 buttons. A button will start a timer and the other will stop the timer. What do I need to change in order to convert to attiny85? Here is the code.
So I figured out that the <Tiny4koled.h> library can display an oled. Anything that says
display must be change to oled such as:
display.print();
MUST be changed to
oled.print();
OR
display.clearDisplay();
MUST be changed to
oled.clear();
and
display.setCursor(8,4);
MUST be changed to
oled.setFont(FONT6X8);
I can get the oled to display ("Press button to start"), however when a button is pressed it will not show the count down timer. I know that there is a line of code that I am missing telling the attiny to display the timer on the oled. That is the part I need help with in the code. I went ahead and left all the forward slashes to let the arduino code know to ignore those lines. I left them there to show the changes needed to go from an arduino to attiny85.
Here is the code and how I modified it as well as all the different libraries that I used.
//#include <ssd1306xled.h>
#include <Tiny4kOLED.h>
//#define DHT21_PIN PB1
//#include <EEPROM.h>
//#include "font6x8AJ2.h"
//dht DHT;
//#include <Adafruit_SSD1306.h>
//#define OLED_RESET 4
//Adafruit_SSD1306 display(OLED_RESET);
//display(OLED_RESET);
unsigned long startMillis;
unsigned long currentMillis;
unsigned long elapsedMillis;
boolean running = false;
const int startButtonPin = 3;
const int pauseButtonPin = 4;
const int resetButtonPin = 1;
void setup() {
// pinMode(DHT21_PIN, INPUT);
// oled.begin(128, 64, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br);
oled.begin();
oled.clear();
oled.on();
oled.switchRenderFrame();
//delay(3000);
pinMode(startButtonPin, INPUT_PULLUP);
pinMode(pauseButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
oled.clear();
// The characters in the 8x16 font are 8 pixels wide and 16 pixels tall
// 2 lines of 16 characters exactly fills 128x32
oled.setFont(FONT8X16);
// Position the cusror
// usage: oled.setCursor(X IN PIXELS, Y IN ROWS OF 8 PIXELS STARTING WITH 0);
oled.setCursor(32, 0);
// Write the text to oled RAM (which is not currently being displayed)
// Wrap strings in F() to save RAM!
oled.print(F("Press button to start"));
// The characters in the 6x8 font are 6 pixels wide and 8 pixels tall
// 4 lines of 21 characters only fills 126x32
// oled.setFont(FONT6X8);
// Position the cusror
// Two rows down because the 8x16 font used for the last text takes two rows of 8 pixels
// oled.setCursor(13, 2);
// Position the cusror
// Cursor X is in pixels, and does not need to be a multiple of the font width
//oled.setCursor(16, 3);
// Swap which half of RAM is being written to, and which half is being displayed
oled.switchFrame();
/*display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
display.print("Press button to start");
display.display();
*/
}
//}
void loop() {
static long startTime = 0;
long currentTime;
// DHT.read22(DHT21_PIN);
//oled.clear();
if (!running){
if (digitalRead(startButtonPin) == LOW) {
startTimer();
}
} else {
if (digitalRead(pauseButtonPin) == LOW) {
pauseTimer();
} else {
currentMillis = millis();
elapsedMillis = currentMillis - startMillis;
// display.clearDisplay();
oled.clear();
// display.setCursor(8,4); //0,16 this changes size
oled.setFont(FONT6X8);
// display.setTextSize(4);
oled.setCursor(13, 2);
// display.setTextColor(WHITE);
if (elapsedMillis < 60000) {
// display.print(elapsedMillis / 1000);
oled.print(elapsedMillis / 1000);
// display.print(".");
oled.print(".");
if ((elapsedMillis % 1000) < 10){
//display.print("0");
oled.print("0");
}
// display.print(elapsedMillis % 1000 / 10);
oled.print(elapsedMillis % 1000 / 10);
// display.print(" s");
oled.print(" s");
} else {
unsigned int minutes = (elapsedMillis / 1000) / 60;
unsigned int seconds = (elapsedMillis / 1000) % 60;
unsigned int splitSeconds = (elapsedMillis % 1000) / 10;
if (minutes < 10) {
// display.print("0");
oled.print("0");
}
oled.setFont(FONT6X8);
// display.print(minutes);
// display.print(":");
oled.print(":");
if (seconds < 10) {
// display.print("0");
oled.print("0");
}
// display.print(seconds);
oled.print(seconds);
// display.print(".");
oled.print(":");
if (splitSeconds < 10) {
// display.print("0");
oled.print("0");
}
// display.print(splitSeconds);
oled.print(splitSeconds);
}
}
if (digitalRead(resetButtonPin) == LOW) {
resetProgram();
}
}
//display.display();
oled.setFont(FONT6X8);
}
void startTimer() {
startMillis = millis() - elapsedMillis;
running = true;
// display.clearDisplay();
oled.clear();
}
void pauseTimer() {
running = false;
}
void resetProgram() {
running = false;
elapsedMillis = 0;
// display.clearDisplay();
oled.clear();
// display.setCursor(0,10);
oled.setCursor(0,10);
// display.print("Timer reset");
oled.print("Timer reset");
delay(1000);
// display.clearDisplay();
oled.clear();
//display.setCursor(0,10);
oled.setCursor(0,10);
//display.print("Push start");
oled.print("Push start");
}
Try commenting-out oled.switchRenderFrame()... and oled.switchFrame()... at the same time.
The video (and code) from post #2 uses graphics with no call to frame switching... (he does use "frame" but in the context of displaying graphics in 8 pixel grids)...
2. Terminate PB4-pin of ATtiny85 to GND via R1 (2k) pull-down resistor. 3. Connect OLED with the I2C Bus of Attiny85. 4. Upload the following sketch in ATtiny85. (Memory requirements: Flash: 5368 bytes, RAM : 135 bytes.)
GolamMostafa, that code works great! Once downloaded the stop watch automatically runs.
I thought I should go easy. Since the timer automatically runs once the power is on, I wanted to add just one push button that will pause the timer then, start it again. I tried doing this to the code but this error message comes out
" a function definition is not allowed here before '{' token".