#include <Adafruit_NeoPixel.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <TimeLib.h>
#include <WiFi.h>
#include "secrets.h"
struct colorData {
int red;
int grn;
int blu;
char * name;
};
const int LED_COUNT = 7;
const int NUM_BITS = 24;
const int SCREEN_WIDTH = 128;
const int SCREEN_HEIGHT = 64;
const int OLED_RESET = -1;
const int ONE_SECOND = 1000; // time interval
const int DL_OFFSET_SEC = 3600; // daylight savings offset
const int LED_PIN = 19;
const int BUZZ_PIN = 32;
const int DATA_PIN = 25;
const int CLOCK_PIN = 26;
const int LATCH_PIN = 27;
const int MODE_SW_PIN = 14; // 12h / 24h switch pin
const long GMT_OFFSET_SEC = (-5 * 60) * 60; // GMT offset (EST is -5)
const char* NTP_ADDR = "pool.ntp.org";
const char * dayOfWeek[] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
const colorData COLOR_DATA[] {
{0, 180, 255, "Evening"},
{240, 255, 0, "Morning"},
{0, 255, 0, "Noon"}
};
int oldVal = -1;
unsigned long lastTime = 0;
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void centerText(const char* text, int y) {
int16_t x1, y1;
uint16_t textWidth, textHeight;
oled.getTextBounds(text, 0, y, &x1, &y1, &textWidth, &textHeight);
int16_t x = (SCREEN_WIDTH - textWidth) / 2;
oled.setCursor(x, y);
oled.print(text);
}
void checkSwitches() {
int inVal = 0;
// sample
digitalWrite(LATCH_PIN, LOW);
digitalWrite(LATCH_PIN, HIGH);
// shift bits in, LSB (D7) > MSB (D0)
for (int i = 0; i < NUM_BITS; i++) {
int bit = digitalRead(DATA_PIN);
bitWrite(inVal, i, bit);
digitalWrite(CLOCK_PIN, HIGH); // Shift out the next bit
digitalWrite(CLOCK_PIN, LOW);
}
// display if data changed
if (inVal != oldVal) {
oldVal = inVal;
// button number
if (inVal == 0) {
strip.clear();
strip.show();
Serial.println("No button pressed");
} else {
// number buttons left to right
for (int i = NUM_BITS; i >= 0; i--) {
if (bitRead(inVal, i)) {
//Serial.print("Button ");
//Serial.print(NUM_BITS - i);
//Serial.println(" pressed");
lightPixel(NUM_BITS - i);
}
}
}
delay(50); // debounce
}
}
void connectWiFi() {
static bool symbol = false;
char buffer[24];
strncpy(buffer, "Connecting to", 24);
centerText(buffer, 10);
Serial.print(buffer);
Serial.print(" ");
centerText(ssid, 30);
Serial.println(ssid);
oled.display();
WiFi.begin(ssid, pass, 6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
//Serial.print(WiFi.status());
symbol = !symbol;
oled.setCursor(63, 50);
oled.print(symbol ? "X" : "+");
oled.display();
}
Serial.println("");
oled.clearDisplay();
strncpy(buffer, "WiFi connected.", 24);
centerText(buffer, 32);
Serial.println(buffer);
oled.display();
delay(1000);
oled.clearDisplay();
}
void lightPixel(int btnNum) {
int dayNum = 0;
int timeOfDay = 0;
for (int i = 0; i <= 21; i += 3) {
if (btnNum <= i) {
dayNum = (i / 3) - 1; // day of week index
timeOfDay = btnNum % 3; // time of day index
strip.setPixelColor(
dayNum,
COLOR_DATA[timeOfDay].red,
COLOR_DATA[timeOfDay].grn,
COLOR_DATA[timeOfDay].blu
);
strip.show();
Serial.print(dayOfWeek[dayNum]);
Serial.print(" ");
Serial.println(COLOR_DATA[timeOfDay].name);
return;
}
}
}
void printLocalTime(int mode24) {
char buffer[24];
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
if (!getLocalTime(timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
oled.setTextSize(2);
if (mode24) {
strftime (buffer, 24, " %H:%M:%S ", timeinfo);
} else {
strftime (buffer, 24, "%I:%M:%S%p", timeinfo);
if (buffer[0] == '0') buffer[0] = ' ';
}
//strftime (buffer, 24, "%X", timeinfo);
//Serial.print(buffer);
//Serial.print(" ");
centerText(buffer, 25);
oled.setTextSize(1);
strftime (buffer, 16, "%A", timeinfo);
centerText(buffer, 5);
//Serial.print("\t");
//Serial.print(buffer);
//Serial.print(", ");
strftime (buffer, 24, "%B %d, %Y", timeinfo);
//Serial.println(buffer);
centerText(buffer, 50);
oled.display();
}
void setup() {
Serial.begin(115200);
oled.setRotation(2);
oled.setTextSize(1); // Normal 1:1 pixel scale
oled.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
oled.clearDisplay();
oled.display();
pinMode(MODE_SW_PIN, INPUT_PULLUP);
pinMode(DATA_PIN, INPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
strip.begin();
strip.setBrightness(50);
strip.show();
connectWiFi();
configTime(GMT_OFFSET_SEC, DL_OFFSET_SEC, NTP_ADDR);
}
void loop() {
delay(10); // this speeds up the simulation
checkSwitches();
bool is24h = !digitalRead(MODE_SW_PIN);
if (millis() - lastTime >= ONE_SECOND) {
lastTime = millis();
printLocalTime(is24h);
}
}
Loading
ssd1306
ssd1306
S - M - T - W - T - F - S
Morning
Noon
Evening
12 / 24