-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtalking_clock_using_talking_measurements.ino
289 lines (277 loc) · 7.45 KB
/
talking_clock_using_talking_measurements.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*------------------------------------------------------------
Talking Clock with WT588D - U 32M audio module
Using audio file 'Talking Measurements'
Connections::
RTC and the OLED display are both I2C
they are connected in parallel
Vcc to Arduino 5 volts
Gnd to Arduino Gnd
SDA to Analog pin 4
SCL to Analog pin 5
(Be very careful with the OLED connections, they will burn out if not connected correctly)
WTD588D::
Upload the audio to this device before connecting into the circuit
Arduino pin 3 to Module pin 7 RESET
Arduino pin 4 to Module pin 21 BUSY
Arduino pin 5 to Module pin 18 PO1
Arduino pin 6 to Module pin 17 PO2
Arduino pin 7 to Module pin 16 PO3
Arduino Gnd to Module pin 14
Arduino 5v to Module pin 20
Push Button::
Connect N/O push switch between
Arduino pin 2
Arduino pin Gnd
(Note pin 2 is held HIGH by the internal pullup resistor)
By Chris Rouse
Feb 2016
------------------------------------------------------------*/
// incluide the Library
#include "WT588D.h" // audio module
#include "U8glib.h" // graphics
#include <SPI.h>
#include <Wire.h> // I2C
#include "RTClib.h" // real time clock
#include <SoftwareSerial.h>
//
#define am 37
#define pm 113
#define oh 105
#define oclock 104
#define hourOffset 0
#define tensMinutesOffset 0x12
boolean andPhrase = false;
boolean minus = false; // true if number is less then 0
boolean halfSecond = true; //used to flash colon
int hundredsOffset = 0x1b; // 1 less than its actual value
int tensOffset = 0x12; // 2 less than its actual value
int pad = 0;
unsigned long previousMillis = 0;
const long interval = 500;
//
// setup u8g object
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // I2C
//
// Setup RTC
RTC_DS1307 RTC;
String thisTime = "";
boolean amFlag = true; // false if pm
int minuteValue;
int hourValue;
int b;
//
// set the correct pin connections for the WT588D chip
#define WT588D_RST 3 //Module pin "REST"
#define WT588D_CS 6 //Module pin "P02"
#define WT588D_SCL 7 //Module pin "P03"
#define WT588D_SDA 5 //Module pin "P01"
#define WT588D_BUSY 4 //Module pin "LED/BUSY"
WT588D myWT588D(WT588D_RST, WT588D_CS, WT588D_SCL, WT588D_SDA, WT588D_BUSY);
#define talkPin 2 // pin used to request time
#define ledPin 13 // onboard LED
//
/*--------------------------------------------------------------*/
//
void setup() {
Serial.begin(9600);
pinMode(talkPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // turn off onboard LED
// draw splash screen
drawSplash();
//
// initialize the chip and port mappiing
myWT588D.begin();
//
// Setup RTC
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
// Get time
DateTime now = RTC.now();
hourValue = now.hour();
minuteValue = now.minute();
//
//
// play boot up sound
speakPhrase(116); // ready
speakPhrase(60); // digital clock
}
/*--------------------------------------------------------*/
// main loop
/*--------------------------------------------------------*/
void loop() {
// read the clock
DateTime now = RTC.now();
minuteValue = now.minute();
// picture loop for clock
u8g.firstPage();
do {
drawClock();
} while( u8g.nextPage() );
//
// see if the Talk Time button was pressed, this is attached to Arduino pin 2
if(digitalRead(talkPin) == 0 ){ // talk key pressed
speakTime();
}
// delay to flash colon
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
halfSecond = !halfSecond;
}
/*--------------------------------------------------------*/
} // end main loop
/*--------------------------------------------------------*/
//
void drawClock(void) {
// graphic commands to redraw the realtimeclock
u8g.setFont(u8g_font_profont15);
u8g.drawStr(13,10, "Real Time Clock");
if(amFlag){
u8g.drawStr(110,15, "am");
}
else u8g.drawStr(110,38, "pm");
u8g.setFont(u8g_font_profont29);
//
//get current time
DateTime now = RTC.now();
// convert to 12 hour mode
//
// convert to 12 hour clock
if(now.hour() >= 12){
amFlag = false;
hourValue = now.hour() - 12;
}
else amFlag = true;
if (now.hour() == 0) hourValue = 12; // midnight
// display time in 12 hour format
thisTime="";
if (halfSecond){
thisTime=String(hourValue) + ":";
}
else thisTime=String(hourValue) + ".";
if (now.minute() < 10){ thisTime=thisTime + "0";} // add leading zero if required
thisTime=thisTime + String(now.minute());
pad = thisTime.length(); // get the string length
pad = 8 - pad; // max length - actual length
pad = pad * 16; // number of pixels free
pad = (pad/2) + 1; // find the start point to ensure string is centred
const char* newTime = (const char*) thisTime.c_str();
u8g.drawStr(pad,50, newTime);
}
//
/*--------------------------------------------------------*/
//
void speakTime()
{
DateTime now = RTC.now();
Serial.print("The time is: ");
Serial.println(String(now.hour()) + ":" + String(now.minute()));
//
speakPhrase(0x88); // the time is
// speak hour
if(minuteValue == 0) speakPhrase(oclock);
else speakPhrase(hourValue + hourOffset);
//
// speak minutes
if(minuteValue > 0 && minuteValue < 21){
if (minuteValue < 10){
speakPhrase(oh);
}
speakPhrase(minuteValue);
}
if(minuteValue > 20){
int temp = minuteValue/10;
int temp2 = minuteValue;
speakPhrase(temp + tensMinutesOffset);
if((10* temp) != temp2){
// only speak unit minutes if not 20, 30, 40 or 50
speakPhrase(minuteValue - (10 * temp));
}
}
// add am or pm
if(amFlag){
speakPhrase(am);
}
else speakPhrase(pm);
} // end speak time
/*--------------------------------------------------------*/
void busy(int pause){
// waits for WT588D to finish sound
delay(100);
while (myWT588D.isBusy() ) {
}
delay(pause);
}
void speakPhrase(int phrase) {
myWT588D.playSound(phrase);
busy(0);
}
//
/*-----------------------------------------------------*/
void drawSplashScreen(){
u8g.setFont(u8g_font_profont15);
u8g.drawStr(36,23, "Talking");
u8g.drawStr(20,43, "Measurements");
u8g.drawFrame(2,2,124,60);
u8g.drawFrame(4,4,120,56);
}
void drawSplash(){
// picture loop for splash screen
u8g.firstPage();
do {
drawSplashScreen();
} while( u8g.nextPage() );
}
/*--------------------------------------------------------*/
void speakNumber(int number){
// will speak an integer from 1 to 9999
// speak a number routine
//
if(number > 99){
andPhrase = true;
}
else andPhrase = false;
//
if(minus) speakPhrase(0x5F); // minus
// positive number over 1000
if(number >= 1000){
b = number/1000;
speakPhrase(b);
speakPhrase(0x8B); // thousand
number = number - b*1000;
}
// positive number from 100 to 999
if(number >=100){
b = number/100;
speakPhrase(b+ hundredsOffset);
number = number - b*100;
if(number > 0){
speakPhrase(0x27); // short and
andPhrase = false;
}
}
// decade from 20 to 90
if(number>=20){
if(andPhrase ){
speakPhrase(0x27); // short and
andPhrase = false;
}
b = number/10;
speakPhrase(b + (tensOffset));
number = number - b*10;
}
// below 20
if(number>0){
if(andPhrase){
speakPhrase(0x27); // short and
}
speakPhrase(number);
}
}
/*--------------------------------------------------------*/