Last active
February 23, 2022 10:54
-
-
Save ralphcrutzen/57aba7e1edaf56258a7a45ca65677162 to your computer and use it in GitHub Desktop.
Arduino Mega, LCD shield, RTC, led strip Wakeup Light
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
/******************************************************* | |
Wakeup Light | |
by | |
Ralph Crützen | |
Hardware: | |
- Arduino Mega | |
- LCD + button shield | |
- DS3231 Real time clock (http://www.rinkydinkelectronics.com/library.php?id=73) | |
- RGB led strip (https://blog.thesen.eu/aldi-rgb-led-strip-iclite-auf-tms1829-basis-mit-dem-arduino-steuern/) | |
LCD and button code: | |
based on a sketch by Mark Bramwell, July 2010 | |
https://www.dfrobot.com/wiki/index.php/Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009) | |
********************************************************/ | |
#include <LiquidCrystal.h> | |
#include <Aldi_NeoPixel.h> | |
#include <DS3231.h> | |
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); | |
Aldi_NeoPixel strip = Aldi_NeoPixel(50, 36, NEO_BRG + NEO_KHZ800); | |
DS3231 rtc(SDA, SCL); | |
#define backlightPin 10 | |
// LCD variables and constants | |
int lcdKey = 0; | |
int adcKeyIn = 0; | |
#define btnRIGHT 0 | |
#define btnUP 1 | |
#define btnDOWN 2 | |
#define btnLEFT 3 | |
#define btnSELECT 4 | |
#define btnNONE 5 | |
// Color modes | |
#define cmStatic 0 | |
#define cmAnimation 1 | |
Time t; | |
int alarmh = 0; | |
int alarmm = 0; | |
boolean alarmSet = false; | |
int r = 128; | |
int g = 128; | |
int b = 128; | |
int colorMode = cmAnimation; | |
boolean lightOn = true; | |
boolean toMainMenu = false; | |
uint16_t j = 0; | |
void setup() { | |
lcd.begin(16, 2); | |
strip.begin(); | |
rtc.begin(); | |
Serial.begin(9600); | |
pinMode(backlightPin, OUTPUT); | |
digitalWrite(backlightPin, HIGH); | |
for(uint16_t i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, strip.Color(r, g, b)); | |
} | |
strip.show(); | |
//rtc.setDOW(SUNDAY); // Set Day-of-Week to SUNDAY | |
//rtc.setTime(10, 35, 0); // Set the time to 10:35:00 (24hr format) | |
//rtc.setDate(6, 11, 2016); // Set the date to November 6th, 2016 | |
} | |
void loop() { | |
lcdKey = readLCDButtons(); | |
if (lcdKey == btnSELECT) { | |
menuSetAlarm(); | |
toMainMenu = false; | |
delay(300); | |
} | |
else if (lcdKey != btnNONE) { | |
lightOn = !lightOn; | |
delay(300); | |
} | |
if (colorMode == cmAnimation && !alarmSet && lightOn) { | |
for(uint16_t i=0; i < strip.numPixels(); i++) { | |
strip.setPixelColor(i, Wheel((i + j) & 255)); | |
} | |
strip.show(); | |
j = (j + 1) % 256; | |
} | |
else if (colorMode == cmStatic && !alarmSet && lightOn) { | |
for(uint16_t i=0; i < strip.numPixels(); i++) { | |
strip.setPixelColor(i, strip.Color(r, g, b)); | |
} | |
strip.show(); | |
} | |
else { | |
for(uint16_t i=0; i < strip.numPixels(); i++) { | |
strip.setPixelColor(i, 0); | |
} | |
strip.show(); | |
} | |
if (lightOn && !alarmSet) | |
digitalWrite(backlightPin, HIGH); | |
else | |
digitalWrite(backlightPin, LOW); | |
lcd.setCursor(0,0); | |
lcd.print("Het is "); // "It's" | |
lcd.print(rtc.getTimeStr()); | |
lcd.setCursor(0,1); | |
lcd.print("Wekker "); // "Alarm" | |
if (alarmSet) { | |
if (alarmh < 10) lcd.print(0); lcd.print(alarmh); lcd.print(":"); | |
if (alarmm < 10) lcd.print(0); lcd.print(alarmm); | |
t = rtc.getTime(); | |
if (alarmh == t.hour && alarmm == t.min) { | |
wakeyWakey(); | |
alarmSet = false; | |
} | |
} | |
else lcd.print("Uit"); // "Off" | |
delay(100); | |
} | |
//**************************** | |
//** Alarm settings | |
//**************************** | |
void wakeyWakey() { | |
digitalWrite(backlightPin, HIGH); | |
lcd.clear(); | |
lcd.print("Wakker worden!"); // "Wake up!" | |
delay(500); | |
for (int c = 0; c < 256; c++) { | |
lcd.setCursor(0,1); | |
lcd.print(255 - c); lcd.print(" "); | |
for(uint16_t i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, strip.Color(c, 0, c/10)); | |
strip.show(); | |
delay(25); | |
lcdKey = readLCDButtons(); | |
if (lcdKey != btnNONE) return; | |
} | |
} | |
} | |
void menuSetAlarm() { | |
lcd.clear(); | |
lcd.print("Wekker instellen"); // "Set alarm" | |
delay(500); | |
while (true) { | |
lcdKey = readLCDButtons(); | |
while (lcdKey == btnNONE) { | |
lcdKey = readLCDButtons(); | |
} | |
if (lcdKey == btnSELECT) setAlarmOnOff(); | |
if (lcdKey == btnUP) menuClock(); | |
if (lcdKey == btnDOWN) menuSetTime(); | |
if (toMainMenu) { | |
return; | |
} | |
} | |
} | |
void setAlarmOnOff() { | |
lcd.clear(); | |
if (alarmSet) { | |
lcd.print("> Wekker aan"); // "Alarm on" | |
lcd.setCursor(0,1); | |
lcd.print(" Wekker uit"); // "Alarm off" | |
} | |
else { | |
lcd.print(" Wekker aan"); | |
lcd.setCursor(0,1); | |
lcd.print("> Wekker uit"); | |
} | |
delay(500); | |
while (true) { | |
lcdKey = readLCDButtons(); | |
while (lcdKey == btnNONE) { | |
lcdKey = readLCDButtons(); | |
} | |
if (lcdKey == btnSELECT) { | |
if (alarmSet) setAlarm(); | |
else { | |
toMainMenu = true; | |
lcd.clear(); | |
return; | |
} | |
} | |
if (lcdKey == btnUP || lcdKey == btnDOWN) { | |
if (alarmSet) { | |
lcd.clear(); | |
lcd.print(" Wekker aan"); | |
lcd.setCursor(0,1); | |
lcd.print("> Wekker uit"); | |
alarmSet = false; | |
delay(500); | |
} | |
else { | |
lcd.clear(); | |
lcd.print("> Wekker aan"); | |
lcd.setCursor(0,1); | |
lcd.print(" Wekker uit"); | |
alarmSet = true; | |
delay(500); | |
} | |
} | |
if (toMainMenu) { | |
return; | |
} | |
} | |
} | |
void setAlarm() { | |
lcd.clear(); | |
lcd.print("Wekker instellen"); // "Set alarm" | |
lcd.setCursor(0,1); | |
lcd.print(alarmh/10); lcd.print(alarmh%10); lcd.print(":"); | |
lcd.print(alarmm/10); lcd.print(alarmm%10); | |
int hms[] = {alarmh/10, alarmh%10, alarmm/10, alarmm%10}; | |
int maxhms[] = {3, 10, 6, 10}; | |
delay(500); | |
int digit = 0; | |
int cursorPos; | |
lcd.cursor(); | |
while (true) { | |
lcd.setCursor(0,1); | |
lcd.print(hms[0]); lcd.print(hms[1]); lcd.print(":"); | |
lcd.print(hms[2]); lcd.print(hms[3]); | |
if (digit < 2) cursorPos = digit; | |
else cursorPos = digit + 1; | |
lcd.setCursor(cursorPos,1); | |
delay(300); | |
lcdKey = readLCDButtons(); | |
while (lcdKey == btnNONE) { | |
lcdKey = readLCDButtons(); | |
} | |
if (lcdKey == btnSELECT) { | |
toMainMenu = true; | |
alarmh = hms[0]*10 + hms[1]; | |
alarmm = hms[2]*10 + hms[3]; | |
lcd.noCursor(); | |
alarmSet = true; | |
return; | |
} | |
if (lcdKey == btnUP) { | |
hms[digit]++; | |
if (digit == 1 && hms[0] == 2 && hms[1] == 4) hms[1] = 0; | |
else | |
if (hms[digit] == maxhms[digit]) hms[digit] = 0; | |
} | |
if (lcdKey == btnDOWN) { | |
hms[digit]--; | |
if (digit == 1 && hms[0] == 2 && hms[1] == -1) hms[1] = 3; | |
else | |
if (hms[digit] == -1) hms[digit] = maxhms[digit] - 1; | |
} | |
if (lcdKey == btnRIGHT) { | |
digit++; | |
if (digit == 4) digit = 0; | |
} | |
if (lcdKey == btnLEFT) { | |
digit--; | |
if (digit == -1) digit = 3; | |
} | |
} | |
} | |
//**************************** | |
//** Time settings | |
//**************************** | |
void menuSetTime() { | |
lcd.clear(); | |
lcd.print("Tijd instellen"); // "Set time" | |
delay(500); | |
while (true) { | |
lcdKey = readLCDButtons(); | |
while (lcdKey == btnNONE) { | |
lcdKey = readLCDButtons(); | |
} | |
if (lcdKey == btnSELECT) setTime(); | |
if (lcdKey == btnUP) menuSetAlarm(); | |
if (lcdKey == btnDOWN) menuSetColor(); | |
if (toMainMenu) { | |
return; | |
} | |
} | |
} | |
void setTime() { | |
lcd.setCursor(0,1); | |
lcd.print(rtc.getTimeStr()); | |
int h = t.hour; int m = t.min; int s = t.sec; | |
int h1 = h/10; | |
int h2 = h - h1*10; | |
int m1 = m/10; | |
int m2 = m - m1*10; | |
int s1 = s/10; | |
int s2 = s - s1*10; | |
int hms[] = {h1, h2, m1, m2, s1, s2}; | |
int maxhms[] = {3, 10, 6, 10, 6, 10}; | |
delay(500); | |
int digit = 0; | |
int cursorPos; | |
lcd.cursor(); | |
while (true) { | |
lcd.setCursor(0,1); | |
lcd.print(hms[0]); lcd.print(hms[1]); lcd.print(":"); | |
lcd.print(hms[2]); lcd.print(hms[3]); lcd.print(":"); | |
lcd.print(hms[4]); lcd.print(hms[5]); | |
if (digit < 2) cursorPos = digit; | |
else if (digit < 4) cursorPos = digit + 1; | |
else cursorPos = digit + 2; | |
lcd.setCursor(cursorPos,1); | |
delay(300); | |
lcdKey = readLCDButtons(); | |
while (lcdKey == btnNONE) { | |
lcdKey = readLCDButtons(); | |
} | |
if (lcdKey == btnSELECT) { | |
toMainMenu = true; | |
rtc.setTime(hms[0]*10 + hms[1], hms[2]*10 + hms[3], hms[4]*10 + hms[5]); | |
lcd.noCursor(); | |
return; | |
} | |
if (lcdKey == btnUP) { | |
hms[digit]++; | |
if (digit == 1 && hms[0] == 2 && hms[1] == 4) hms[1] = 0; | |
else | |
if (hms[digit] == maxhms[digit]) hms[digit] = 0; | |
} | |
if (lcdKey == btnDOWN) { | |
hms[digit]--; | |
if (digit == 1 && hms[0] == 2 && hms[1] == -1) hms[1] = 3; | |
else | |
if (hms[digit] == -1) hms[digit] = maxhms[digit] - 1; | |
} | |
if (lcdKey == btnRIGHT) { | |
digit++; | |
if (digit == 6) digit = 0; | |
} | |
if (lcdKey == btnLEFT) { | |
digit--; | |
if (digit == -1) digit = 5; | |
} | |
} | |
} | |
//**************************** | |
//** Color settings | |
//**************************** | |
void menuSetColor() { | |
lcd.clear(); | |
lcd.print("Kleur instellen"); | |
delay(500); | |
while (true) { | |
lcdKey = readLCDButtons(); | |
while (lcdKey == btnNONE) { | |
lcdKey = readLCDButtons(); | |
} | |
if (lcdKey == btnSELECT) setColorMode(); | |
if (lcdKey == btnUP) menuSetTime(); | |
if (lcdKey == btnDOWN) menuClock(); | |
if (toMainMenu) { | |
return; | |
} | |
} | |
} | |
void setColorMode() { | |
lcd.clear(); | |
if (colorMode == cmStatic) { | |
lcd.print("> Een kleur"); // "One color" | |
lcd.setCursor(0,1); | |
lcd.print(" Regenboog"); // "Rainbow" | |
} | |
else { | |
lcd.print(" Een kleur"); | |
lcd.setCursor(0,1); | |
lcd.print("> Regenboog"); | |
} | |
delay(500); | |
while (true) { | |
lcdKey = readLCDButtons(); | |
while (lcdKey == btnNONE) { | |
lcdKey = readLCDButtons(); | |
} | |
if (lcdKey == btnSELECT) { | |
if (colorMode == cmStatic) setColor(); | |
else { | |
toMainMenu = true; | |
return; | |
} | |
} | |
if (lcdKey == btnUP || lcdKey == btnDOWN) { | |
if (colorMode == cmStatic) { | |
lcd.clear(); | |
lcd.print(" Een kleur"); | |
lcd.setCursor(0,1); | |
lcd.print("> Regenboog"); | |
colorMode = cmAnimation; | |
delay(500); | |
} | |
else { | |
lcd.clear(); | |
lcd.print("> Een kleur"); | |
lcd.setCursor(0,1); | |
lcd.print(" Regenboog"); | |
colorMode = cmStatic; | |
delay(500); | |
} | |
} | |
if (toMainMenu) { | |
return; | |
} | |
} | |
} | |
void setColor() { | |
for(uint16_t i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, strip.Color(r, g, b)); | |
} | |
strip.show(); | |
lcd.clear(); | |
lcd.print("Rood Groen Blauw"); // "Red Green Blue" | |
lcd.setCursor(0,1); lcd.print(r); | |
lcd.setCursor(5,1); lcd.print(g); | |
lcd.setCursor(11,1); lcd.print(b); | |
int rgb[] = {r/100, (r%100)/10, (r%100) % 10, | |
g/100, (g%100)/10, (g%100) % 10, | |
b/100, (b%100)/10, (b%100) % 10 }; | |
delay(500); | |
int digit = 0; | |
int cursorPos; | |
lcd.cursor(); | |
while (true) { | |
lcd.setCursor(0,1); lcd.print(rgb[0]); lcd.print(rgb[1]); lcd.print(rgb[2]); | |
lcd.setCursor(5,1); lcd.print(rgb[3]); lcd.print(rgb[4]); lcd.print(rgb[5]); | |
lcd.setCursor(11,1); lcd.print(rgb[6]); lcd.print(rgb[7]); lcd.print(rgb[8]); | |
if (digit < 3) cursorPos = digit; | |
else if (digit < 6) cursorPos = digit + 2; | |
else cursorPos = digit + 5; | |
lcd.setCursor(cursorPos,1); | |
delay(300); | |
lcdKey = readLCDButtons(); | |
while (lcdKey == btnNONE) { | |
lcdKey = readLCDButtons(); | |
} | |
if (lcdKey == btnSELECT) { | |
toMainMenu = true; | |
lcd.noCursor(); | |
lcd.clear(); | |
return; | |
} | |
if (lcdKey == btnUP) { | |
for (int i = 0; i <= 6; i = i + 3) { | |
if (digit == i && ((rgb[i] + 1)*100 + rgb[i+1]*10 + rgb[i+2]) < 256 && rgb[digit] < 9) | |
rgb[i]++; | |
if (digit == i+1 && (rgb[i]*100 + (rgb[i+1] + 1)*10 + rgb[i+2]) < 256 && rgb[digit] < 9) | |
rgb[i+1]++; | |
if (digit == i+2 && (rgb[i]*100 + rgb[i+1]*10 + (rgb[i+2] + 1)) < 256 && rgb[digit] < 9) | |
rgb[i+2]++; | |
} | |
r = rgb[0]*100 + rgb[1]*10 + rgb[2]; | |
g = rgb[3]*100 + rgb[4]*10 + rgb[5]; | |
b = rgb[6]*100 + rgb[7]*10 + rgb[8]; | |
for(uint16_t i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, strip.Color(r, g, b)); | |
} | |
strip.show(); | |
} | |
if (lcdKey == btnDOWN) { | |
rgb[digit]--; | |
if (rgb[digit] == -1) rgb[digit] = 0; | |
r = rgb[0]*100 + rgb[1]*10 + rgb[2]; | |
g = rgb[3]*100 + rgb[4]*10 + rgb[5]; | |
b = rgb[6]*100 + rgb[7]*10 + rgb[8]; | |
for(uint16_t i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, strip.Color(r, g, b)); | |
} | |
strip.show(); | |
} | |
if (lcdKey == btnRIGHT) { | |
digit++; | |
if (digit == 9) digit = 0; | |
} | |
if (lcdKey == btnLEFT) { | |
digit--; | |
if (digit == -1) digit = 8; | |
} | |
} | |
} | |
//**************************** | |
//** Menu back to clock | |
//**************************** | |
void menuClock() { | |
lcd.clear(); | |
lcd.print("Toon klok"); // "Show clock" | |
delay(500); | |
while (true) { | |
lcdKey = readLCDButtons(); | |
while (lcdKey == btnNONE) { | |
lcdKey = readLCDButtons(); | |
} | |
if (lcdKey == btnSELECT) { | |
toMainMenu = true; | |
return; | |
} | |
if (lcdKey == btnUP) menuSetColor(); | |
if (lcdKey == btnDOWN) menuSetAlarm(); | |
} | |
} | |
int readLCDButtons() { | |
adcKeyIn = analogRead(0); | |
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741 | |
// we add approx 50 to those values and check to see if we are close | |
if (adcKeyIn > 1000) | |
return btnNONE; | |
else { | |
digitalWrite(backlightPin, HIGH); | |
} | |
if (adcKeyIn < 50) return btnRIGHT; | |
if (adcKeyIn < 250) return btnUP; | |
if (adcKeyIn < 450) return btnDOWN; | |
if (adcKeyIn < 650) return btnLEFT; | |
if (adcKeyIn < 850) return btnSELECT; | |
return btnNONE; // when all others fail, return this... | |
} | |
// Input a value 0 to 255 to get a color value. | |
// The colours are a transition r - g - b - back to r. | |
uint32_t Wheel(byte WheelPos) { | |
if(WheelPos < 85) { | |
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); | |
} else if(WheelPos < 170) { | |
WheelPos -= 85; | |
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); | |
} else { | |
WheelPos -= 170; | |
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment