int motorPin = A3;
int buttonPin = D3;
//https://maker.ifttt.com/trigger/button_pressed/json/with/key/bcFhSXZcj5VFoJT1n0CjFo
int medSpeed = 100;
int lowSpeed = 50;
int hiSpeed = 180;
int motorSpeed = 100;
String statename[4] = {"WORK","QUICK BREAK","LONG BREAK","INACTIVE"};
String currentState = "NOT STARTED";
typedef struct {
uint32_t totalTime = 0;
uint8_t state = 3;
unsigned long timer = 0;
unsigned long totaltimer = 0;
bool active = false;
}Pomodoro;
uint32_t pomTime[3] = {25,5,15};
Pomodoro myPom;
Pomodoro *pPom;
long motorTimer = 0;
int motorDelayMillis = 5000;
int timeLeft = 0;
bool motorOn = false;
bool button_press = false;
long buttonTimer = 0;
long buttonLength;
int buttonState;
void initPom(){
pomTime[0] = min2milli(pomTime[0]);
pomTime[1] = min2milli(pomTime[1]);
pomTime[2] = min2milli(pomTime[2]);
pPom = &myPom;
}
int startPomSession(String command){
int mintime = command.toInt();
myPom.totalTime = min2milli(mintime);
myPom.state = 0;
myPom.active = true;
myPom.timer = millis();
myPom.totaltimer = millis();
motorControl(myPom.state);
return myPom.totalTime;
}
void pomCheck(){
currentState = statename[myPom.state];
timeLeft = milli2min(myPom.totalTime-millis()-myPom.totaltimer);
if (myPom.active == false || myPom.state == 3){
motorOn = false;
analogWrite(motorPin, 0);
return;
}
if (millis()-myPom.totaltimer>=myPom.totalTime){
myPom.active = false;
myPom.state = 3;
motorOn = false;
motorControl(4);
return;
}
else if (millis()-myPom.timer>=pomTime[myPom.state]){
//go to next state and restart timer
myPom.state ++;
if (myPom.state > 3) myPom.state = 0;
myPom.timer = millis();
//trigger appropriate flag wave
motorControl(myPom.state);
}
}
void setup() {
initPom();
pinMode(motorPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
Particle.function("StartPomSession", startPomSession);
Particle.variable("PomodoroState", ¤tState, STRING);
Particle.variable("PomLeft", &timeLeft, INT);
}
void loop() {
pomCheck();
motorTimerCheck();
buttonCheck();
}
void motorControl(int command)
{
// Set Activation Time and Speed
if (command == 0){
TimerAdj(8);
motorSpeed = hiSpeed;
}
else if (command == 1){
TimerAdj(3);
motorSpeed = hiSpeed;
}
else if (command == 2){
TimerAdj(12);
motorSpeed = medSpeed;
}
else if (command == 3){
TimerAdj(2);
motorSpeed = 0;
}
else if (command == 4){
//finish
TimerAdj(2);
motorSpeed = lowSpeed;
analogWrite(motorPin, motorSpeed);
delay(4000);
return;
}
analogWrite(motorPin, motorSpeed);
motorOn = true;
motorTimer = millis();
// done
}
void motorTimerCheck(){
// if timer has elapsed
if (motorOn && millis()-motorTimer>= motorDelayMillis){
motorSpeed = 0;
motorOn = false;
analogWrite(motorPin, motorSpeed);
}
if (motorOn == false) analogWrite(motorPin, 0);
}
void TimerAdj(int seconds){
// convert to seconds
motorDelayMillis = 1000* seconds;
}
int min2milli(int minutes){
return 1000*60*60*minutes;
}
int milli2min(int milli){
return milli/1000/60/60;
}
void buttonCheck(){
if (digitalRead(buttonPin)==LOW){
analogWrite(motorPin,0);
myPom.state = 3;
motorControl(3);
myPom.active = false;
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .