3. Timed control

Timer program.

  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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/***************************************************************************
   TEMPORIZADOR
 ***************************************************************************/
#include <Wire.h>
#include <PC42.h>
#include <EEPROM.h>

#define TONE_ERR     Si1         // 59 Hz Simón original
#define TONE_ALARM   Do6
#define MAX_SECONDS  9999
#define EEPROM_ADDR_SECONDS  4

/***************************************************************************
   DATOS GLOBALES
 ***************************************************************************/
typedef struct {
   int total_time;
   int countdown;
   int count_millis;
   long millis;
   long millis_old;
   struct {
     unsigned state:3;
     unsigned alarm:3;
   };
} tempo_t;

tempo_t tempo;

enum {INIT_STOP, STOP, INIT_COUNTDOWN, COUNTDOWN, INIT_ALARM, ALARM};
enum {OFF, ON };


/***************************************************************************
   SETUP Y BUCLE PRINCIPAL
 ***************************************************************************/

void setup() {
  pc.begin();
  tempo_init();
}


void loop() {
  
  // Dispatcher
  switch(tempo.state) {

  case INIT_STOP:  
    pc.ledBegin();
    pc.ledWrite(1, HIGH);
    power_off();
    tempo.state = STOP;
    break;

  case STOP:
    pc.dispNum(tempo.countdown);
    tempo_stop();
    break;

  case INIT_COUNTDOWN:
    if (tempo.countdown > 0) {
      pc.ledBegin();
      pc.ledWrite(4, HIGH);
      tempo.millis_old = millis();
      tempo.state = COUNTDOWN;
    }
    else {
      tempo.state = INIT_STOP;
    }
    break;

  case COUNTDOWN:  
    tempo_countdown();
    break;

  case INIT_ALARM:
    power_off();   
    pc.ledBegin();
    pc.ledWrite(3, HIGH);
    tempo.millis = millis();
    tempo.alarm = 0;
    tempo.state = ALARM;
    break;

  case ALARM:
    alarm();
    break;
    
  default:
     tempo.state = INIT_STOP;
  }
}


/***************************************************************************
   FUNCIONES
 ***************************************************************************/

//
// STOP
//
void tempo_stop(void) {
  char keys;
  pc.dispDots(0);

  keys = pc.keyCount(KEY_DOWN);
  tempo.countdown -= keys;
  if (tempo.countdown < 0)
    tempo.countdown += MAX_SECONDS + 1;

  keys = pc.keyCount(KEY_UP);
  tempo.countdown += keys;
  if (tempo.countdown > MAX_SECONDS)
    tempo.countdown -= MAX_SECONDS + 1;
 
  if (pc.keyPressed(KEY_UP) && pc.keyPressed(KEY_DOWN)) {
     tempo.countdown = tempo.total_time;
     tempo.count_millis = 0;
  }
   
  if (pc.keyEvents(KEY_RIGHT, KEY_PRESSED_TIME2)) {
    tempo.total_time = tempo.countdown;
    pc.dispWrite(0,0,0,0);
    eeprom_write16(EEPROM_ADDR_SECONDS, tempo.total_time);
    delay(100);
  }

  if (pc.keyEvents(KEY_ENTER, KEY_PRESSED_TIME1)) { 
    tempo.state = INIT_COUNTDOWN;
  }

  pc.dispNum(tempo.countdown);
}


//
// COUNTDOWN
//
void tempo_countdown(void) {
 
  // Activar la salida de potencia
  power_on();

  // Actualizar la cuenta atrás
  tempo.millis = millis();
  tempo.count_millis += tempo.millis - tempo.millis_old;
  tempo.millis_old = tempo.millis;

  while (tempo.count_millis >= 1000) {
    tempo.countdown--;
    pc.dispNum(tempo.countdown);
    tempo.count_millis -= 1000;
    if (tempo.countdown == 0) break;
  }

  if (tempo.count_millis < 500)
    pc.dispDots(0);
  else
    pc.dispDots(1);

  if (tempo.countdown == 0) {
    power_off();
    tempo.state = INIT_ALARM;
  }

  if (pc.keyEvents(KEY_ENTER, KEY_PRESSED_TIME1)) {
    tempo.state = INIT_STOP;
  }  
}


//
// ALARM
//
void alarm(void) {
  pc.dispWrite(DD_E, DD_n, DD_d, DD_SP);

  switch(tempo.alarm) {
  case 0:
    alarm_sound();
    tempo.alarm = 1;
    break;
  case 1:
    if (millis() > tempo.millis + 1000) {
      alarm_sound();
      tempo.millis += 1000;
      tempo.alarm = 2;
    }
    break;
  case 2:
    if (millis() > tempo.millis + 4000) {
      tempo.millis += 4000;
      pc.buzzPlay(TONE_ALARM, 16);
      pc.buzzPlay(0, 16);
    }
    break;
  }
  
  if (pc.keyEvents(KEY_ENTER, KEY_PRESSED_TIME1)) {
    pc.buzzBegin();
    tempo.countdown = tempo.total_time;
    tempo.count_millis = 0;
    tempo.state = INIT_STOP;
  }
}


//
// Read integer data from EEPROM
//
int eeprom_read16(int addr) {
  return (EEPROM.read(addr + 1) << 8) | EEPROM.read(addr);
}

//
// Write integer data to EEPROM
//
int eeprom_write16(int addr, int data) {
  EEPROM.write(EEPROM_ADDR_SECONDS, tempo.total_time);
  EEPROM.write(EEPROM_ADDR_SECONDS+1, tempo.total_time>>8);
}

//
// Inicializar datos del temporizador
//
void tempo_init(void) {
  tempo.total_time = eeprom_read16(EEPROM_ADDR_SECONDS);
  tempo.state = INIT_STOP;  
  tempo.alarm = OFF;
  
  tempo.millis = 0;
  tempo.millis_old = 0;

  tempo.countdown = tempo.total_time;
  tempo.count_millis = 0;
}

//
// Sound Alarm
//
void alarm_sound(void) {
  pc.buzzPlay(TONE_ALARM, 62);
  pc.buzzPlay(0, 62);
  pc.buzzPlay(TONE_ALARM, 62);
  pc.buzzPlay(0, 62);
  pc.buzzPlay(TONE_ALARM, 62);
  pc.buzzPlay(0, 62);
  pc.buzzPlay(TONE_ALARM, 62);
  pc.buzzPlay(0, 62);
}


//
// Encender la fuente de alimentación
//
void power_on(void) {
  // Desactivar la salida de potencia
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
}


//
// Apagar la fuente de alimentación
//
void power_off(void) {
  // Desactivar la salida de potencia
  digitalWrite(10, LOW);
  pinMode(10, INPUT);
}