Skip to content

Instantly share code, notes, and snippets.

@alex-charland
Last active Aug 22, 2021
Embed
What would you like to do?
/*Light-Up Timer Arduino Code
ATLS 3300 Final Project by Alexandra Charland, 5/28/2021
25-minute and 5-minute break timer with the intention of use alongside the Pomodoro study technique.
Press the button to start the timer.
Use the slider to change the color of the LEDs.
*/
//Libraries used: SevSeg, Adafruit_Neopixel
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(9, 8, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(9, 9, NEO_GRB + NEO_KHZ800);
// Notes from pitches.h for piezo buzzer
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
#include "SevSeg.h"
SevSeg sevseg;
int num = 25; //25-minute timer value
int slider = A0;
int button = 16;
int buzzer = 17;
int sliderColor = 0;//Current slider value for changing LED hue, initially set to 0
bool timerRunning = false;
bool timerDone = false;
bool timerBreakRunning = false;
bool timerBreakDone = false;
unsigned long prevMills = 60000;
unsigned long prevMillsBreak = 0;
const long minute = 60000;
int colorArray[] = {0,0,0};
// Melody: Shave and Haricut - Two Bits
int doneMelody[] = {
NOTE_C5, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_G4, 0, NOTE_B4, NOTE_C5
};
int startMelody[] = {
NOTE_C5, 0, NOTE_C5, 0, NOTE_C6
};
int doneNoteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
int startNoteDurations[] = {
4, 2, 4, 2, 4
};
void setup(){
Serial.begin(9600);
strip1.begin();
strip1.show();
strip2.begin();
strip2.show();
pinMode(slider,INPUT);
pinMode(buzzer,OUTPUT);
//SevSeg setup
byte numDigits = 2; //Use two digits for dual 7-segment display
byte digitPins[] = {21,5}; // Right digit is connected to pin 21, left digit to pin 5
byte segmentPins[] = {20,19,18,3,2,6,7,4}; // Pins for segments A,B,C,D,E,F,G,DP respectively
bool resistorsOnSegments = true;
byte hardwareConfig = COMMON_CATHODE;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(90);
}
void loop(){
int slide = analogRead(slider);
int buttonRead = digitalRead(button);
sliderColor = map(slide, 0, 1023, 0, 1530);
changeLEDs(sliderColor);
if(buttonRead == 1){
timerRunning = true;
playStartMelody();
}
if(timerRunning == true){
timer();
}
if(timerDone == true){
flashLights();
timerDone = false;
timerBreakRunning = true;
}
if(timerBreakRunning == true){
breakTimer();
}
if(timerBreakDone == true){
flashLights();
timerBreakDone = false;
}
}
int red[] = {255,0,0};
int orange[] = {255,150,0};
int yellow[] = {255,255,0};
int green[] = {0,255,0};
int blue[] = {0,0,255};
int purple[] = {127,0,255};
int startLight = 1; //Use to keep track of next LED color in the desired sequence
//Light up LEDs in the sequence of red, yellow, and green in sync with the buzzer start melody
void playStartMelody(){
for (int note = 0; note < 5; note++) {
int noteDuration = 1000 / (startNoteDurations[note]);
tone(buzzer, startMelody[note], noteDuration);
float pauseBetweenNotes = noteDuration * 1.30;
if(startLight == 1 && startMelody[note] != 0){
for(uint16_t i=0; i<3; i++){
strip1.setPixelColor(i,red[0],red[1],red[2]);
strip1.show();
strip2.setPixelColor(i,red[0],red[1],red[2]);
strip2.show();
}
delay(pauseBetweenNotes/2); //Turn the LED on and off in between note pauses
for(uint16_t i=0; i<3; i++){
strip1.setPixelColor(i,0,0,0);
strip1.show();
strip2.setPixelColor(i,0,0,0);
strip2.show();
}
delay(pauseBetweenNotes/2);
startLight = 2;
noTone(buzzer);
}
else if(startLight == 2 && startMelody[note] != 0){
for(uint16_t i=3; i<6; i++){
strip1.setPixelColor(i,yellow[0],yellow[1],yellow[2]);
strip1.show();
strip2.setPixelColor(i,yellow[0],yellow[1],yellow[2]);
strip2.show();
}
delay(pauseBetweenNotes/2);
for(uint16_t i=3; i<6; i++){
strip1.setPixelColor(i,0,0,0);
strip1.show();
strip2.setPixelColor(i,0,0,0);
strip2.show();
}
delay(pauseBetweenNotes/2);
startLight = 3;
noTone(buzzer);
}
else if(startLight == 3 && startMelody[note] != 0){
for(uint16_t i=6; i<9; i++){
strip1.setPixelColor(i,green[0],green[1],green[2]);
strip1.show();
strip2.setPixelColor(i,green[0],green[1],green[2]);
strip2.show();
}
delay(pauseBetweenNotes/2);
for(uint16_t i=6; i<9; i++){
strip1.setPixelColor(i,0,0,0);
strip1.show();
strip2.setPixelColor(i,0,0,0);
strip2.show();
}
delay(pauseBetweenNotes/2);
startLight = 1;
noTone(buzzer);
}
else{
delay(pauseBetweenNotes);
noTone(buzzer);
}
}
}
//Use millis() to keep track of time. Using delay() will not work
void timer(){
unsigned long currentMills = millis();
if(currentMills - prevMills >= minute){
prevMills = currentMills;
sevseg.setNumber(num);
if(num != 0){
num -= 1;
}
else{
timerRunning = false;
timerDone = true;
num = 25;
}
}
sevseg.refreshDisplay();
}
int breakNum = 5; //5-minute break timer value
void breakTimer(){
unsigned long currentMills = millis();
if(currentMills - prevMillsBreak >= minute){
prevMillsBreak = currentMills;
sevseg.setNumber(breakNum);
if(breakNum != 0){
breakNum -= 1;
}
else{
timerBreakRunning = false;
timerBreakDone = true;
breakNum = 5;
}
}
sevseg.refreshDisplay();
}
//Light the LEDs with the color indicated by the slider
void changeLEDs(int color){
hue(color);
for(uint16_t i=0; i<strip1.numPixels(); i++){
strip1.setPixelColor(i, colorArray[0], colorArray[1], colorArray[2]); //set to purple, takes RGB vals 0-255
strip1.show();
strip2.setPixelColor(i, colorArray[0], colorArray[1], colorArray[2]); //set to purple, takes RGB vals 0-255
strip2.show();
}
}
//Set the color based on the slider's value
void hue(int sliderPos){
if (sliderPos <= 255){
colorArray[0] = 255;
colorArray[1] = 0;
colorArray[2] = 0;
}
//Increment Blue until 255
if (sliderPos > 255 && sliderPos < 510){
colorArray[0] = 255;
colorArray[1] = 0;
colorArray[2] = sliderPos%255;
}
//Decrement Red until 0
if(sliderPos >= 510 && sliderPos < 765){
colorArray[0] = 255 - (sliderPos%255);
colorArray[1] = 0;
colorArray[2] = 255;
}
//Increment Green until 255
if(sliderPos >= 765 && sliderPos < 1020){
colorArray[0] = 0;
colorArray[1] = sliderPos%255;
colorArray[2] = 255;
}
//Decrement Blue until 0
if(sliderPos >= 1020 && sliderPos < 1275){
colorArray[0] = 0;
colorArray[1] = 255;
colorArray[2] = 255 - (sliderPos%255);
}
//Increment Red until 255
if(sliderPos >= 1275 && sliderPos < 1530){
colorArray[0] = sliderPos%255;
colorArray[1] = 255;
colorArray[2] = 0;
}
}
int currLight = 1;
//Flash LEDs in sync with a melody when the timer finishes
void flashLights(){
for (int note = 0; note < 8; note++) {
int noteDuration = 1000 / (doneNoteDurations[note]);
tone(buzzer, doneMelody[note], noteDuration);
float pauseBetweenNotes = noteDuration * 1.30;
if(currLight == 1){
for(uint16_t i=0; i<3; i++){
strip1.setPixelColor(i,red[0],red[1],red[2]);
strip1.show();
}
delay(pauseBetweenNotes/2);
for(uint16_t i=0; i<3; i++){
strip1.setPixelColor(i,0,0,0);
strip1.show();
}
delay(pauseBetweenNotes/2);
currLight = 2;
noTone(buzzer);
}
else if(currLight == 2){
for(uint16_t i=0; i<3; i++){
strip2.setPixelColor(i,green[0],green[1],green[2]);
strip2.show();
}
delay(pauseBetweenNotes/2);
for(uint16_t i=0; i<3; i++){
strip2.setPixelColor(i,0,0,0);
strip2.show();
}
delay(pauseBetweenNotes/2);
currLight = 3;
noTone(buzzer);
}
else if(currLight == 3){
for(uint16_t i=3; i<6; i++){
strip1.setPixelColor(i,blue[0],blue[1],blue[2]);
strip1.show();
}
delay(pauseBetweenNotes/2);
for(uint16_t i=3; i<6; i++){
strip1.setPixelColor(i,0,0,0);
strip1.show();
}
delay(pauseBetweenNotes/2);
currLight = 4;
noTone(buzzer);
}
else if(currLight == 4){
for(uint16_t i=3; i<6; i++){
strip2.setPixelColor(i,orange[0],orange[1],orange[2]);
strip2.show();
}
delay(pauseBetweenNotes/2);
for(uint16_t i=3; i<6; i++){
strip2.setPixelColor(i,0,0,0);
strip2.show();
}
delay(pauseBetweenNotes/2);
currLight = 5;
noTone(buzzer);
}
else if(currLight == 5){
for(uint16_t i=6; i<9; i++){
strip1.setPixelColor(i,yellow[0],yellow[1],yellow[2]);
strip1.show();
}
delay(pauseBetweenNotes/2);
for(uint16_t i=6; i<9; i++){
strip1.setPixelColor(i,0,0,0);
strip1.show();
}
delay(pauseBetweenNotes/2);
currLight = 6;
noTone(buzzer);
}
else{
for(uint16_t i=6; i<9; i++){
strip2.setPixelColor(i,purple[0],purple[1],purple[2]);
strip2.show();
}
delay(pauseBetweenNotes/2);
for(uint16_t i=6; i<9; i++){
strip2.setPixelColor(i,0,0,0);
strip2.show();
}
delay(pauseBetweenNotes/2);
currLight = 1;
noTone(buzzer);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment