-
-
Save christophermoura/2c997bbd24e7a038ce71 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
POMODORO 0.2 | |
by @erikaheidi | |
January 2015 | |
LED STATES: | |
starts blue - idle. When the button is clicked, it starts a pomodoro working phase. LED will be a red "breathing" style. | |
After the time is done, it will change to GREEN (static) - indicating that the pomodoro was completed and now you should go to a PAUSE | |
When clicking the button, the PAUSE phase will start (GREEN "BREATHING"). It will turn to RED (static) when the pause is done, indicating that | |
a new WORKING phase should follow. When the button is clicked, starts over with the WORKING phase (RED breathing LED) :) | |
*/ | |
// To find about possible tones, check http://arduino.cc/en/Tutorial/Tone | |
#define TONE_START 3136 | |
#define TONE_END 2637 | |
// status for the timer. STATE_TIMER_RUNNING = when a timer is running, either in "working" or "break" phases of pomodoro | |
#define STATE_TIMER_IDLE 0 | |
#define STATE_TIMER_RUNNING 1 | |
// status pomodoro | |
#define STATE_TO_WORK 1 | |
#define STATE_WORKING 2 | |
#define STATE_TO_PAUSE 3 | |
#define STATE_PAUSED 4 | |
//IO | |
const int buttonPin = 2; | |
const int ledPin = 5; | |
const int buzzerPin = 6; | |
const int redPin = 11; | |
const int greenPin = 10; | |
const int bluePin = 9; | |
int timerState = STATE_TIMER_IDLE; // state from the timer - idle (0) or working (1) | |
int pomoState = STATE_TO_WORK; // state for how the led looks like | |
int currentColor = bluePin; | |
unsigned long time; | |
// time since current phase started - millis() output from when a phase has started | |
unsigned long pomoTimer; | |
//n*1000*60 | |
//const unsigned long POMO_DURATION_MS = 60000; // 1 minute | |
//const unsigned long POMO_PAUSE_MS = 60000; // 1 minute | |
const unsigned long POMO_DURATION_MS = 1500000; // 25 minutes | |
const unsigned long POMO_PAUSE_MS = 300000; // 5 minutes | |
void setup() { | |
Serial.begin(9600); | |
pinMode(ledPin, OUTPUT); | |
pinMode(redPin, OUTPUT); | |
pinMode(greenPin, OUTPUT); | |
pinMode(bluePin, OUTPUT); | |
pinMode(buttonPin, INPUT); | |
setColor(0, 0, 255); //blue is the default | |
pomoTimer = millis(); | |
} | |
void loop(){ | |
// verify state and change if its done. button wont work if timer is running | |
if (timerState == STATE_TIMER_RUNNING) { | |
time = millis() - pomoTimer; | |
Serial.print("Time elapsed: "); | |
Serial.println(time); | |
Serial.print("Target: "); | |
Serial.println(POMO_DURATION_MS); | |
if (pomoState == STATE_WORKING && time > POMO_DURATION_MS) { | |
changeStatus(STATE_TO_PAUSE); | |
} else if(pomoState == STATE_PAUSED && time > POMO_PAUSE_MS) { | |
changeStatus(STATE_TO_WORK); | |
} | |
if (timerState) { | |
pomoPulse(currentColor); //has a delay. only pulses when timer is running | |
} | |
} else { | |
// when the timer is idle, a button must be pushed to change state! | |
if (digitalRead(buttonPin) == LOW) { | |
buzz(TONE_START); | |
Serial.println("button pressed"); | |
if (pomoState == STATE_TO_WORK) { | |
changeStatus(STATE_WORKING); | |
} else if (pomoState == STATE_TO_PAUSE) { | |
changeStatus(STATE_PAUSED); | |
} | |
} | |
} | |
} | |
void buzz(int note) | |
{ | |
int noteDuration = 1000/8; | |
int pauseBetweenNotes = noteDuration * 1.30; | |
tone(buzzerPin, note, noteDuration); | |
delay(noteDuration*1.1); | |
tone(buzzerPin, note, noteDuration); | |
delay(noteDuration); | |
noTone(buzzerPin); | |
} | |
void changeStatus(int newStatus) | |
{ | |
Serial.println("Changing status"); | |
pomoState = newStatus; | |
if (pomoState == STATE_WORKING || pomoState == STATE_PAUSED) { | |
timerState = STATE_TIMER_RUNNING; | |
} else { | |
buzz(TONE_END); | |
timerState = STATE_TIMER_IDLE; | |
} | |
pomoTimer = millis(); //reset timer | |
updateStatusLed(); | |
} | |
void updateStatusLed() | |
{ | |
setColor(0,0,0); | |
if (pomoState == STATE_TO_WORK || pomoState == STATE_WORKING) { | |
currentColor = redPin; | |
} else if (pomoState == STATE_TO_PAUSE || pomoState == STATE_PAUSED) { | |
currentColor = greenPin; | |
} else { | |
currentColor = bluePin; | |
} | |
analogWrite(currentColor, 255); | |
} | |
void setColor(int red, int green, int blue) | |
{ | |
#ifdef COMMON_ANODE | |
red = 255 - red; | |
green = 255 - green; | |
blue = 255 - blue; | |
#endif | |
analogWrite(redPin, red); | |
analogWrite(greenPin, green); | |
analogWrite(bluePin, blue); | |
} | |
void pomoPulse(int pin) { | |
// fade in from min to max in increments of 5 points: | |
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { | |
// sets the value (range from 0 to 255): | |
analogWrite(pin, fadeValue); | |
// wait for 30 milliseconds to see the dimming effect | |
delay(30); | |
} | |
// fade out from max to min in increments of 5 points: | |
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { | |
// sets the value (range from 0 to 255): | |
analogWrite(pin, fadeValue); | |
// wait for 30 milliseconds to see the dimming effect | |
delay(30); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment