#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SW1 26
#define SW2 27
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
const int potPin = 34;
const int potPin2 = 39;
int ldr = 0;
int pot = 0;
int potValue = 0;
int potValue2 = 0;
int secTime = 0;
int minTime = 0;
int avai = 0;
char temp[100];
unsigned long last_time = 0;
unsigned long last_time_500 = 0;
bool modeclock = false;
bool toogle = true;
void IRAM_ATTR swit1() {
int stateSW1 = digitalRead(SW1);
int stateSW2 = digitalRead(SW2);
if (stateSW1 == LOW && stateSW2 == HIGH) {
modeclock = true;
} else if (stateSW1 == HIGH && stateSW2 == LOW) {
modeclock = false;
} else if (stateSW1 == HIGH && stateSW2 == HIGH) {
modeclock = false;
} else if (stateSW1 == LOW && stateSW2 == LOW) {
modeclock = true;
}
}
void IRAM_ATTR swit2() {
int stateSW1 = digitalRead(SW1);
int stateSW2 = digitalRead(SW2);
if (stateSW1 == LOW && stateSW2 == HIGH) {
modeclock = true;
} else if (stateSW1 == HIGH && stateSW2 == LOW) {
modeclock = false;
} else if (stateSW1 == HIGH && stateSW2 == HIGH) {
modeclock = true;
} else if (stateSW1 == LOW && stateSW2 == LOW) {
modeclock = false;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) {
Serial.println(F("SSD1306 allocation failed"));
}
pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
attachInterrupt(SW1, swit1, CHANGE);
attachInterrupt(SW2, swit2, CHANGE);
display.display();
delay(2000);
display.clearDisplay();
// Test Draw Char
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println(F("Computer "));
display.setCursor(0, 15);
display.println(F("Engineer "));
display.display();
delay(2000);
fCheck();
}
void loop() {
if (Serial.available()) {
avai = Serial.parseInt();
if (avai == 1) {
modeclock = true;
} else if (avai == 2) {
modeclock = false;
}
}
if ( millis() - last_time >= 1000) {
last_time = millis();
if (modeclock == true) {
secTime++;
if (secTime >= 60) {
secTime = 0;
minTime++;
if (minTime >= 60) {
minTime = 0;
}
}
}
}
if ( millis() - last_time_500 >= 500) {
last_time_500 = millis();
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
ldr = analogRead(potPin2);
pot = analogRead(potPin);
if (toogle == true) {
sprintf(temp, "Time %02d:%02d", minTime, secTime);
display.print(temp);
display.setCursor(0, 10);
sprintf(temp, "LDR DEC:%04d VOLT:%.1f", ldr, ((float)ldr * 3.3 / 4095));
display.print(temp);
display.setCursor(0, 20);
sprintf(temp, "POT DEC:%04d VOLT:%.1f", pot, ((float)pot * 3.3 / 4095));
display.print(temp);
display.display();
}
else {
sprintf(temp, "Time %02d %02d", minTime, secTime);
display.print(temp);
display.setCursor(0, 10);
sprintf(temp, "LDR DEC:%04d VOLT:%.1f", ldr, ((float)ldr * 3.3 / 4095));
display.print(temp);
display.setCursor(0, 20);
sprintf(temp, "POT DEC:%04d VOLT:%.1f", pot, ((float)pot * 3.3 / 4095));
display.print(temp);
display.display();
}
toogle = !toogle;
}
}