-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharduino-totem.ino
140 lines (120 loc) · 4.02 KB
/
arduino-totem.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/********
* |)|2\/\//-\|<`/
**
* Arduino - Pomodoro Totem Creature.
**
* @author Joaquin Lopez <[email protected]>
* @twitter @drwaky
* @bitbucket drwaky
* @github drwaky
* @copyright Copyright (c) 2014, DrWaky
* @version 0.4
* @date 2014/12/11
* @license GPLv3 - https://www.gnu.org/licenses/gpl-3.0.html
* @category Arduino
**
* Arduino Based Pomodoro Totem Creature to the SBA Team at MediaNet Software
**/
const int restCycle = 4; // Long rest cycle size
const int systemLed = 13; // Testing Led pin
const int restLed = 2; // Rest Led Arduino pin
const int firstWorkLed = 3; // First Wirk led Arduino pin
const int workLedQuantity = 3; // Quantity of Work Leds
boolean working; // Working mode On or Off
int currentWorkLedOn = workLedQuantity; // Quantity of Work Leds are on
int systemLedState = LOW; // Testing Led state.
int restLedState = LOW; // Rest Led state.
boolean cycleSetSw = false; // Switch variable to check or not the rest type.
unsigned int pomodorosCount = 0; // Pomodoros counter
unsigned long systemLedPreviousMillis = 0; // Testing Led time moment
unsigned long workRestLedPreviousMillis = 0; // Working or Resting Leds time moment
const long systemLedInterval = 500; // Testing Led time interval
const long shortRestInterval = 500; // Short rest time interval value in ms
const long longRestInterval = 1500; // Long rest time interval value in ms
const long workInterval = 3000; // Work time interval value in ms
long restInterval = 0; // Corrent Rest time interval in ms (can be long or short)
/**
* Returns the opposite state of a LED.
* @param {int} val - The current value of the led to toggle.
* @returns {int} The opposite state: LOW => HIGH, HIGH => LOW.
*/
int toggleLedState(int val){
int res;
if(val == HIGH){
res = LOW;
} else {
res = HIGH;
}
return res;
}
/**
* Turn on all the work LEDs.
* @param {int} init - The first pin for a work led.
* @param {int} number - The numbers of work led.
*/
void allWorkLedOn(int init, int number){
int pin;
for (int i = 0; i < number; i++){
pin = firstWorkLed + i;
digitalWrite(pin, HIGH);
}
}
/**
* Setup the environment.
*/
void setup(){
int pin;
for (int i = 0; i < workLedQuantity; i++){
pin = firstWorkLed + i;
pinMode(pin, OUTPUT);
}
pinMode(restLed, OUTPUT);
pinMode(systemLed, OUTPUT);
allWorkLedOn(firstWorkLed, workLedQuantity);
working = true;
}
/**
* Main infinite loop.
*/
void loop(){
unsigned long currentMillis = millis();
/* LED 13th blinking - Only for dev env */
if(currentMillis - systemLedPreviousMillis >= systemLedInterval){
systemLedPreviousMillis = currentMillis;
systemLedState = toggleLedState(systemLedState);
digitalWrite(systemLed, systemLedState);
}
/* --- */
if(working){
if((currentMillis - workRestLedPreviousMillis >= workInterval / workLedQuantity) && (currentWorkLedOn > 0)){
currentWorkLedOn--;
workRestLedPreviousMillis = currentMillis;
digitalWrite(firstWorkLed + currentWorkLedOn, LOW);
} else if(currentWorkLedOn == 0){
pomodorosCount++;
workRestLedPreviousMillis = currentMillis;
restLedState = toggleLedState(restLedState);
digitalWrite(restLed, restLedState);
working = false;
cycleSetSw = false;
}
} else {
if(!cycleSetSw){
if(pomodorosCount % restCycle == 0){
restInterval = longRestInterval;
cycleSetSw = true;
} else {
restInterval = shortRestInterval;
cycleSetSw = true;
}
}
if(currentMillis - workRestLedPreviousMillis >= restInterval){
workRestLedPreviousMillis = currentMillis;
allWorkLedOn(firstWorkLed, workLedQuantity);
currentWorkLedOn = workLedQuantity;
restLedState = toggleLedState(restLedState);
digitalWrite(restLed, restLedState);
working = true;
}
}
}