Last active
May 11, 2022 10:59
-
-
Save t1m0thyj/a6d642a7da7c2a16adb0bb7fb4ddcb39 to your computer and use it in GitHub Desktop.
24hr Clock - requires Arduino Uno, ESP8266 ESP-01 Wi-Fi module, and Scroll pHAT HD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_IS31FL3731.h> | |
#include <ezTime.h> | |
#include <WiFiEsp.h> | |
#include "arduino_secrets.h" | |
// Emulate Serial1 on pins 6/7 if not present | |
#ifndef HAVE_HWSERIAL1 | |
#include "SoftwareSerial.h" | |
SoftwareSerial Serial1(6, 7); // RX, TX | |
#endif | |
char ssid[] = SECRET_SSID; // your network SSID (name) | |
char pass[] = SECRET_PASS; // your network password | |
int status = WL_IDLE_STATUS; // the Wifi radio's status | |
const int ldrPin = A0; | |
const char timezone[] = "America/New_York"; | |
const byte digits[][10] = { | |
{ B111, B001, B111, B111, B101, B111, B111, B111, B111, B111 }, | |
{ B101, B001, B001, B001, B101, B100, B100, B001, B101, B101 }, | |
{ B101, B001, B001, B001, B101, B100, B100, B001, B101, B101 }, | |
{ B101, B001, B111, B111, B111, B111, B111, B001, B111, B111 }, | |
{ B101, B001, B100, B001, B001, B001, B101, B001, B101, B001 }, | |
{ B101, B001, B100, B001, B001, B001, B101, B001, B101, B001 }, | |
{ B111, B001, B111, B111, B001, B111, B111, B001, B111, B111 } | |
}; | |
const uint8_t PROGMEM gamma8[] = { | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, | |
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, | |
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, | |
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, | |
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, | |
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, | |
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, | |
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50, | |
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68, | |
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89, | |
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114, | |
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142, | |
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175, | |
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213, | |
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 }; | |
Adafruit_IS31FL3731 matrix = Adafruit_IS31FL3731(17, 7); | |
Timezone myTz; | |
bool needsDraw = true; | |
bool showColon = false; | |
int currentPwm = 0; | |
int lastMs = 0; | |
int ldrValue = 0; | |
int pixel_addr(int x, int y) { | |
if (x > 8) { | |
x = x - 8; | |
y = 6 - (y + 8); | |
} else { | |
x = 8 - x; | |
} | |
return x * 16 + y; | |
} | |
int read_pwm() { | |
int tempLdrValue = analogRead(ldrPin) / 4; | |
if (abs(tempLdrValue - ldrValue) >= (ldrValue * 0.05)) { | |
ldrValue = tempLdrValue; | |
} | |
return max(1, pgm_read_byte(&gamma8[ldrValue])); | |
} | |
void draw_digit(int x, int y, int digit, int pwm) { | |
for (int y2 = 0; y2 < 7; y2++) { | |
for (int x2 = 0; x2 < 3; x2++) { | |
bool isOn = (digits[y2][digit] >> x2) & 1; | |
matrix.setLEDPWM(pixel_addr(x + 2 - x2, y + 6 - y2), pwm * isOn); | |
} | |
} | |
} | |
void setup() | |
{ | |
// turn off bright yellow light | |
pinMode(LED_BUILTIN, OUTPUT); | |
digitalWrite(LED_BUILTIN, LOW); | |
// initialize serial for debugging | |
Serial.begin(115200); | |
// initialize serial for ESP module | |
Serial1.begin(9600); | |
// initialize ESP module | |
WiFi.init(&Serial1); | |
// check for the presence of the shield | |
if (WiFi.status() == WL_NO_SHIELD) { | |
digitalWrite(LED_BUILTIN, HIGH); | |
Serial.println("WiFi shield not present"); | |
// don't continue | |
while (true); | |
} | |
// attempt to connect to WiFi network | |
while ( status != WL_CONNECTED) { | |
Serial.print("Attempting to connect to WPA SSID: "); | |
Serial.println(ssid); | |
// Connect to WPA/WPA2 network | |
status = WiFi.begin(ssid, pass); | |
} | |
// you're connected now, so print out the data | |
Serial.println("You're connected to the network"); | |
if (!matrix.begin()) { | |
digitalWrite(LED_BUILTIN, HIGH); | |
while (true); | |
} | |
waitForSync(); | |
myTz.setLocation(timezone); | |
Serial.println("Current local time: " + myTz.dateTime()); | |
} | |
void loop() | |
{ | |
int msNow = (myTz.now() * 1000 + ms(LAST_READ)) % 1000; | |
if (needsDraw || (msNow != lastMs)) { | |
if (needsDraw || (msNow % 10 == 9)) { | |
int oldPwm = currentPwm; | |
currentPwm = read_pwm(); | |
if (currentPwm != oldPwm) { | |
needsDraw = true; | |
} | |
} | |
lastMs = msNow; | |
} | |
if (needsDraw || minuteChanged()) { | |
int hourNow = myTz.hour(); | |
int minuteNow = myTz.minute(); | |
draw_digit(0, 0, hourNow / 10, currentPwm); | |
draw_digit(4, 0, hourNow % 10, currentPwm); | |
draw_digit(10, 0, minuteNow / 10, currentPwm); | |
draw_digit(14, 0, minuteNow % 10, currentPwm); | |
} | |
bool tempShowColon = msNow < 500; | |
if (needsDraw || (tempShowColon != showColon)) { | |
showColon = tempShowColon; | |
matrix.setLEDPWM(pixel_addr(8, 1), currentPwm * showColon); | |
matrix.setLEDPWM(pixel_addr(8, 5), currentPwm * showColon); | |
} | |
needsDraw = false; | |
events(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment