#include <SPI.h>
#include <WiFi.h>
#include <Wire.h>
//#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int GMTOffset = -28800; //Replace with your GMT Offset in seconds
int daylightOffset = 0; //Replace with your daylight savings offset in seconds
// Interval between sensor readings. Learn more about ESP32 timers: https://RandomNerdTutorials.com/esp32-pir-motion-sensor-interrupts-timers/
//unsigned long previousMillis = 0;
//const long interval = 3000;
//Constants
const int screenPin = 4;
const int modePin = 2;
time_t rawtime = time(nullptr);
struct tm* timeinfo = localtime(&rawtime);
unsigned long last_time, now_time;
long count;
int screenState = 1;
int modeState = 1;
int OnOff = 0;
void setup();
void screenA();
void screenB();
void screenOn();
void screenOff();
void loop() ;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
configTime(GMTOffset, daylightOffset, "pool.ntp.org","time.nist.gov");
pinMode(screenPin, INPUT_PULLUP);
pinMode(modePin, INPUT_PULLUP);
last_time = micros();
//screenA();
//screenB();
//screenOff();
}
void screenA(){
now_time = micros();
if (now_time - last_time > 1000000){
Serial.print(now_time - last_time); // display the elapsed loop time in microseconds
Serial.print(" count "); Serial.println(count);
last_time = now_time;
count = 0;
//digitalWrite(modePin, HIGH);
display.clearDisplay();
int radius = 35;
display.drawCircle(display.width()/2, display.height()/2, 2, WHITE);
//draw clock
for( int i=0; i < 360;i= i + 30 ){
float angle = i ;
angle=(angle/57.29577951) ;
int x1=(64+(sin(angle)*radius));
int y1=(32-(cos(angle)*radius));
int x2=(64+(sin(angle)*(radius-5)));
int y2=(32-(cos(angle)*(radius-5)));
display.drawLine(x1,y1,x2,y2,WHITE);
}
//draw second hand
float angle = timeinfo->tm_sec*6 ;
angle=(angle/57.29577951) ;
int x2=(64+(sin(angle)*(radius)));
int y2=(32-(cos(angle)*(radius)));
display.drawLine(64,32,x2,y2,WHITE);
// draw minute hand
angle = timeinfo->tm_min * 6 ;
angle=(angle/57.29577951) ;
x2=(64+(sin(angle)*(radius-3)));
y2=(32-(cos(angle)*(radius-3)));
display.drawLine(64,32,x2,y2,WHITE);
// draw hour hand
angle = timeinfo->tm_hour * 30 + int((timeinfo->tm_min / 12) * 6 );
angle=(angle/57.29577951) ;
x2=(64+(sin(angle)*(radius-11)));
y2=(32-(cos(angle)*(radius-11)));
display.drawLine(64,32,x2,y2,WHITE);
display.display();
delay(100);
display.clearDisplay();
}
}
void screenB(){
//display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(30,32);
display.print("Screen B On");
display.display();
delay(5000);
display.clearDisplay();
}
void screenOff(){
display.ssd1306_command(SSD1306_DISPLAYOFF);
}
void screenOn(){
display.ssd1306_command(SSD1306_DISPLAYON);
}
void loop() {
rawtime = time(nullptr);
timeinfo = localtime(&rawtime);
int screenState = digitalRead(screenPin);
int modeVal = digitalRead(modePin);
//print out the value of the pushbutton
Serial.println(screenState);
Serial.println(modeVal);
if (screenState == HIGH && modeVal == HIGH){
screenA();
}
if (screenState == LOW && modeVal == HIGH) {
screenB();
}
if (modeVal == LOW) {
//display.ssd1306_command(SSD1306_DISPLAYOFF);
display.clearDisplay();
OnOff = 1;
//screenOff();
}
if (modeVal == LOW && OnOff == 1) {
//display.ssd1306_command(SSD1306_DISPLAYON);
display.display();
//screenOn();
OnOff = 0;
}
//delay(1);
}