-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpomoduino.ino
160 lines (132 loc) · 4.38 KB
/
pomoduino.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <Event.h>
#include <Timer.h>
#include <LED.h>
#include <FiniteStateMachine.h>
#include <Button.h>
#include "pitches.h"
/*
Pomoduino tiene 2 estados:
1. Inactivo - No se muestra nada en el display
2. Activo - Empieza la cuenta regresiva y actualiza el display cada segundo
*/
const byte NUMBER_OF_STATES = 2;
// La clase State tiene dos prototipos
// State(void updateFunction) y
// State(void enterFunction, void UpdateFuntion, void exitFunction)
State Off = State(pomoduinoOff);
State On = State(pomoduinoStart, pomoduinoOn, pomoduinoStop);
FSM pomoduinoStateMachine = FSM(Off);
Button button = Button(8, BUTTON_PULLUP);
LED led = LED(9);
const byte MELODY_SIZE = 47;
const byte BUZZER_PIN = 4;
int hayMusica = 1;
int melody[] = {
NOTE_E6, NOTE_E6, NOTE_0, NOTE_E6, NOTE_0, NOTE_C6, NOTE_E6, NOTE_0, NOTE_G6, NOTE_0,
NOTE_0, NOTE_0, NOTE_G5, NOTE_0, NOTE_0, NOTE_0, NOTE_C6, NOTE_0, NOTE_0, NOTE_G5,
NOTE_0, NOTE_0, NOTE_E5, NOTE_0, NOTE_0, NOTE_A5, NOTE_0, NOTE_B5, NOTE_0, NOTE_AS5,
NOTE_A5, NOTE_0, NOTE_G5, NOTE_E6, NOTE_G6, NOTE_A6, NOTE_0, NOTE_F6, NOTE_G6, NOTE_0,
NOTE_E6, NOTE_0, NOTE_C6, NOTE_D6, NOTE_B5, NOTE_0, NOTE_0
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 5, 5, 5, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7
};
// counter variable, hols number of button presses
byte buttonPresses = 0;
// 25 minutos x 60 segundos = 1500
int tiempoInicial = 5;
// El estado del timer
int segundosTranscurridos = 0;
Timer timer;
void setup() {
Serial.begin(9600);
Serial.println("Inicializando Pomoduino!");
timer.every(1000, pomoduinoUpdateDisplay);
}
//poor example, but then again; it's just an example
void loop() {
if (button.uniquePress()){
//increment buttonPresses and constrain it to [ 0, 1 ]
buttonPresses = ++buttonPresses % NUMBER_OF_STATES;
Serial.println("Detected button press");
switch (buttonPresses){
case 0: pomoduinoStateMachine.transitionTo(On); break;
case 1: pomoduinoStateMachine.transitionTo(Off); break;
}
}
pomoduinoStateMachine.update();
timer.update();
}
/*-------------------------------------------------------------------------------
Status = ON
-------------------------------------------------------------------------------*/
void pomoduinoStart() {
hayMusica = 0;
Serial.println("Prendido");
segundosTranscurridos = tiempoInicial;
noTone(BUZZER_PIN);
}
// Este codigo se ejecuta cada vez que se actualiza la state machine
// (practicamente todo el tiempo!)
void pomoduinoOn() {
led.on();
if (segundosTranscurridos == 0) {
pomoduinoStateMachine.immediateTransitionTo(Off);
}
}
void pomoduinoStop() {
// no hace nada por el momento ;)
mostrarTimerFormateado(segundosTranscurridos);
Serial.println("Stop!");
hayMusica = 1;
led.off();
doSound();
}
/*-------------------------------------------------------------------------------
Status = Off
-------------------------------------------------------------------------------*/
void pomoduinoOff() {
segundosTranscurridos = 0;
}
/*-------------------------------------------------------------------------------
Actualizacion del timer
-------------------------------------------------------------------------------*/
void pomoduinoUpdateDisplay() {
if (pomoduinoStateMachine.isInState(On)) {
mostrarTimerFormateado(segundosTranscurridos);
segundosTranscurridos -= 1;
}
}
void mostrarTimerFormateado(int seconds) {
byte minutes = seconds / 60;
byte remaining = seconds % 60;
Serial.print(minutes);Serial.print(':');
if (remaining < 10) {
Serial.print('0');
}
Serial.println(remaining);
}
void doSound() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < MELODY_SIZE; thisNote++) {
if (hayMusica == 0) {
break;
}
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(BUZZER_PIN, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(BUZZER_PIN);
}
}