#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Encoder.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
#define SCREEN_ADDRESS 0x3D
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Encoder encoder(3, 2);
const int BUTTON_PIN = 7;
enum Screen {
MAIN_SCREEN,
WARM_UP_SCREEN,
CUT_TIME_SCREEN
};
Screen currentScreen = MAIN_SCREEN;
Screen previousScreen = WARM_UP_SCREEN;
int warmUpTime = 4; // Initialize warm-up time variable
int cutTime = 22; // Initialize cut time variable
int encoderChange = 0; // Declare the encoderChange variable
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
// Clear the display
display.clearDisplay();
// Set text size and color
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
}
void loop() {
int encoderValue = encoder.read();
if (digitalRead(BUTTON_PIN) == LOW) {
delay(100); // Debounce
switch (currentScreen) {
case MAIN_SCREEN:
currentScreen = WARM_UP_SCREEN;
break;
case WARM_UP_SCREEN:
currentScreen = CUT_TIME_SCREEN;
break;
case CUT_TIME_SCREEN:
currentScreen = MAIN_SCREEN;
break;
}
encoderChange = 0;
}
encoderChange = encoder.read();
if (encoderChange != 0) {
switch (currentScreen) {
case WARM_UP_SCREEN: {
int newWarmUpTime = warmUpTime + encoderChange;
// Ensure the new warm-up time is within a reasonable range
if (newWarmUpTime >= 0 && newWarmUpTime <= 30) {
display.setCursor(60, 24);
display.setTextColor(SSD1306_BLACK);
display.print(warmUpTime);
display.println("s");
display.display();
display.setTextColor(SSD1306_WHITE);
warmUpTime = newWarmUpTime;
display.setCursor(60, 24);
display.print(warmUpTime);
display.println("s");
display.display();
}
break;
}
case CUT_TIME_SCREEN: {
int newCutTime = cutTime + encoderChange;
// Ensure the new cut time is within a reasonable range
if (newCutTime >= 0 && newCutTime <= 60) {
display.setCursor(45, 24);
display.setTextColor(SSD1306_BLACK);
display.print(cutTime);
display.println("s");
display.display();
display.setCursor(45, 24);
display.setTextColor(SSD1306_WHITE);
cutTime = newCutTime;
display.print(cutTime);
display.println("s");
display.display();
}
break;
}
}
encoder.write(0); // Reset the encoder value
}
if (currentScreen != previousScreen) {
// Update the display within the loop
display.clearDisplay();
display.setCursor(0, 0);
switch (currentScreen) {
case MAIN_SCREEN:
display.setTextSize(1);
display.print("Warm Up Time: ");
display.print(warmUpTime);
display.println("s");
display.setCursor(0, 16);
display.print("Cut Time: ");
display.print(cutTime);
display.println("s");
break;
case WARM_UP_SCREEN:
display.setTextSize(2);
display.print("Warm Up");
display.setCursor(0, 24);
display.print("Time:");
display.setTextSize(2);
display.setCursor(60, 24);
display.print(warmUpTime);
display.println("s");
break;
case CUT_TIME_SCREEN:
display.setTextSize(2);
display.print("Cut Time:");
display.setTextSize(2);
display.setCursor(45, 24);
display.print(cutTime);
display.println("s");
break;
}
display.display();
// Update the previous screen
previousScreen = currentScreen;
}
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
encoder1:CLK
encoder1:DT
encoder1:SW
encoder1:VCC
encoder1:GND