bluevalve_v1
TRANSCRIPT
-
7/26/2019 blueValve_v1
1/9
Tuesday, September 15, 2015 8\Users\Mayor\Documents\Arduino\Arduino Resources and Projects\blueValve_v1\blueValve_v1.ino
1 /*
2 * Program ID : blueValve
3 * Version : 1.0
4 * Author : The MARANATHA team
5 * Date : September, 2015
6 * Notes : Initial release.
7 *
8 *
9 */
10
11
12 /*
13 * Load required libraries.
14 */
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
21
22
23 /*
24 * Manifest constants.
25 */
26 #define KBDADDR 0x38 // Keypad I2C address.
27 #define BTpower 7 // Logical pin used to switch Bluetooth module
on/off. (PHYSICAL PIN: 12)
28 #define BTprog 6 // Logical pin used to switch Bluetooth module's programming mod
on/off. (PHYSICAL PIN: 13)
29 #define valvePin 3 // Logical pin used to switch the valve
on/0ff. (PHYSICAL PIN: 5)
30 #define buzzer 4 // Logical pin used for the audio
feedback. (PHYSICAL PIN: 6)
3132 /*
33 * LOGICAL PINS: Pins that the program knows and addresses. This has a corresponding
PHYSICAL PIN in the ATMEGA328P-PU chip.
34 * Phisical pins are used in actual electrical connection.
35 */
36
37
38 LiquidCrystal lcd(10); // Declare the usage of an LCD.
39
40 AltSoftSerial blueValve; // Create a port for the Bluetooth module. Pin 8 is RX, Pin
is TX
4142
43 I2CDecodedKeypad kpd(KBDADDR,buzzer); // Declare the usage of a keypad.
44
45
46 /*
47 * Global variables.
48 */
49 char BluetoothData; // Holds the data given by the Bluetooth app installed in a smart
phone.
50 boolean valveOpen = false;
51 boolean updateLCD = true;
-1-
-
7/26/2019 blueValve_v1
2/9
Tuesday, September 15, 2015 8\Users\Mayor\Documents\Arduino\Arduino Resources and Projects\blueValve_v1\blueValve_v1.ino
52 String PIN; // Holds user provided PIN for security.
53
54
55 /*
56 * Codes inside setup() only run once every startup.
57 */
58 void setup() {
59 char aEEPROMBuffer[6];
60
61 lcd.begin(16, 2);
62 Wire.begin();
63 blueValve.begin(9600);
64 kpd.init();
65
66 lcd.clear();
67 lcd.home();
68
69
70 /*
71 * Read from EEPROM. Check if first time run. If first time, initialize.
72 * If not then simply read back EEPROM's content.
73 */
74 if (char(EEPROM.read(512) != 'E')){ // E is a marker denoting that the EEPROM has
been written to. Any indicator will do.
75 lcd.print("Initializing...");
76
77 PIN = "1234"; // Default PIN.
78 PIN.toCharArray(aEEPROMBuffer,6);
79 for (int nIndex = 0; nIndex < 4; nIndex++){
80 EEPROM.write(nIndex, byte(aEEPROMBuffer[nIndex]));
81 delay(100); // Give time for the EEPROM to store the Byte
82 }
83
84 /*** Mark the EEPROM to denote that initial data are available. ***/
85 EEPROM.write(512, byte('E'));86 delay(100); // Give time for the EEPROM to store the Byte
87 }
88 else {
89 lcd.print("Reading...");
90 for (int nIndex = 0; nIndex < 4; nIndex++){
91 PIN = PIN + char(EEPROM.read(nIndex));
92 delay(100); // Give time for the EEPROM to store the Byte
93 }
94 }
95
96
97 /*98 * Initialize pins.
99 */
100 pinMode(BTpower, OUTPUT);
101 pinMode(BTprog, OUTPUT);
102 pinMode(valvePin, OUTPUT);
103 pinMode(valvePin, OUTPUT);
104 pinMode(buzzer, OUTPUT);
105
106 digitalWrite(valvePin, LOW); // Start with the valve closed.
107 digitalWrite(BTprog, LOW); // Bluetooth setup mode disabled.
108 digitalWrite(BTpower, HIGH); // Bluetooth power supply turned on.
-2-
-
7/26/2019 blueValve_v1
3/9
Tuesday, September 15, 2015 8\Users\Mayor\Documents\Arduino\Arduino Resources and Projects\blueValve_v1\blueValve_v1.ino
109
110 lcd.clear();
111 lcd.print(" blueValve v1.0 ");
112 lcd.setCursor(0,1);
113 lcd.print(" By: MARANATHA ");
114
115 delay(3000);
116
117 beep(25,25,3);
118 }
119
120
121 /*
122 * Codes inside the loop() run as long as device has power.
123 */
124 void loop() {
125
126 if (updateLCD){
127 updateLCD = false;
128 lcd.clear();
129 lcd.home();
130 lcd.print(" blueValve v1.0 ");
131 lcd.setCursor(0,1);
132 if (valveOpen){
133 lcd.print("Valve is opened.");
134 }
135 else{
136 lcd.print("Valve is closed.");
137 }
138
139 }
140
141 /*
142 * Monitors keypad.
143 *144 * This function will check if user pressed the "*", "#", and "A" buttons on the
keypad.
145 *
146 * Keypad functions:
147 *
148 * * - Turn the valve on.
149 * # - Turn the valve off.
150 * A - Change the keypad and Bluetooth PIN.
151 *
152 * PIN is required to complete any of the functions.
153 *
154 */155 keyPadCheck();
156
157
158 blueToothCheck(); // Monitors incoming Bluetooth data.
159
160 delay(100); // Slight delay to prepare for next data.
161 }
162
163
164 // -------------------------------------------- SUPPORT FUNCTIONS
-------------------------------------------- //
-3-
-
7/26/2019 blueValve_v1
4/9
Tuesday, September 15, 2015 8\Users\Mayor\Documents\Arduino\Arduino Resources and Projects\blueValve_v1\blueValve_v1.ino
165
166 /*
167 * Process keypad.
168 */
169 void keyPadCheck(){
170 char keyPressed;
171
172 keyPressed = kpd.getKeyStroke();
173 if (keyPressed) {
174 switch (keyPressed) {
175 case '*': // Turn valve on.
176 if (!valveOpen){
177 if (validPIN()) {
178 if (!valveOpen){
179 digitalWrite(valvePin, HIGH);
180 beep(75,50,3);
181 valveOpen = true;
182 updateLCD = true;
183 }
184 }
185 }
186 break;
187
188 case '#': // Turn valve off.
189 if (valveOpen){
190 if (validPIN()) {
191 if (valveOpen){
192 digitalWrite(valvePin, LOW);
193 beep(75,50,3);
194 valveOpen = false;
195 updateLCD = true;
196 }
197 }
198 }
199 break;200
201 case 'A': // Change PIN. Used in keypad and BT.
202 if (validPIN()) {
203 PINChange();
204 updateLCD = true;
205 }
206 break;
207
208 } // Case..
209 } // If..
210 }
211212
213 /*
214 * Process bluetooth.
215 */
216 void blueToothCheck(){
217 if (blueValve.available()){
218 BluetoothData=blueValve.read();
219
220 if (BluetoothData=='1' && !valveOpen){ // Open valve...
221 digitalWrite(valvePin, HIGH);
222 beep(75,50,3);
-4-
-
7/26/2019 blueValve_v1
5/9
Tuesday, September 15, 2015 8\Users\Mayor\Documents\Arduino\Arduino Resources and Projects\blueValve_v1\blueValve_v1.ino
223 valveOpen = true;
224 updateLCD = true;
225 }
226
227 if (BluetoothData=='0' && valveOpen){ // Close valve...
228 digitalWrite(valvePin, LOW);
229 beep(25,25,1);
230 valveOpen = false;
231 updateLCD = true;
232 }
233 }
234 }
235
236
237 /*
238 * PIN Change Function.
239 *
240 * When "A" is pressed, this function is called. The old PIN is required to change PIN
241 * Power must not be interrupted during this process or else the EEPROM and/or
Bluetooth is going to be corrupted.
242 * When the system is corrupted, flashing new firmware is required. The Bluetooth
module is going to be needing repair.
243 *
244 */
245 void PINChange(){
246 boolean BTOkay = false;
247 char keyPressed;
248 String thePIN, returnVal;
249 int keyStrokes=0;
250 char aEEPROMBuffer[6];
251 char ATCommandBuffer[14];
252
253 lcd.clear();
254 lcd.home();
255 lcd.print("Preparing system");256 lcd.setCursor(0,1);
257 lcd.print("Please wait... ");
258
259 /*
260 * Prepare BT module for programming.
261 */
262 digitalWrite(BTpower, LOW); // Shut down BT.
263 delay(1000);
264 digitalWrite(BTprog, HIGH); // Bluetooth setup mode enabled.
265 digitalWrite(BTpower, HIGH); // Start BT again in pin change mode.
266
267 blueValve.end();268 blueValve.begin(38400);
269
270 delay(2000); // New settings need sometime for it to take effect.
271
272
273 lcd.clear();
274 lcd.home();
275 lcd.print(" Enter new PIN: ");
276
277 /*
278 * Get new 4 digits PIN.
-5-
-
7/26/2019 blueValve_v1
6/9
Tuesday, September 15, 2015 8\Users\Mayor\Documents\Arduino\Arduino Resources and Projects\blueValve_v1\blueValve_v1.ino
279 */
280 while (keyPressed != '*'){
281 keyPressed = kpd.getKeyStroke();
282
283 /*
284 * Valid keys for PIN are 0 to 9.
285 * "*" - cancels PIN entry.
286 * Only for digits are accepted.
287 */
288 if (keyPressed) {
289
290 if (keyPressed == '*'){
291 break;
292 }
293
294 if (int(keyPressed) >= 48 && int(keyPressed)
-
7/26/2019 blueValve_v1
7/9
Tuesday, September 15, 2015 8\Users\Mayor\Documents\Arduino\Arduino Resources and Projects\blueValve_v1\blueValve_v1.ino
336 lcd.setCursor(0,1);
337 lcd.print("Restart system.");
338 while (1){} // System halt!
339 }
340 }
341 else {
342 lcd.clear();
343 lcd.home();
344 lcd.setCursor(0,1);
345 lcd.print(" PIN cancelled. ");
346 delay(500);
347 }
348
349 lcd.clear();
350 lcd.home();
351 lcd.print("Preparing system");
352 lcd.setCursor(0,1);
353 lcd.print("Please wait... ");
354 updateLCD = true;
355
356 /*
357 * Prepare BT module for programming.
358 */
359 digitalWrite(BTpower, LOW); // Shut down BT.
360 delay(1000);
361 digitalWrite(BTprog, LOW); // Bluetooth setup mode enabled.
362 digitalWrite(BTpower, HIGH); // Start BT again in pin change mode.
363
364 blueValve.end();
365 blueValve.begin(9600);
366
367 delay(2000); // New settings need sometime for it to take effect.
368 }
369
370371 /*
372 * Validate PIN.
373 */
374 boolean validPIN(){
375 char keyPressed;
376 String thePIN;
377 boolean PINOkay = false;
378 int keyStrokes = 0;
379
380 lcd.clear();
381 lcd.home();
382 lcd.print(" Enter PIN: ");383
384 while (keyPressed != '*'){
385 keyPressed = kpd.getKeyStroke();
386
387 /*
388 * Valid keys for PIN are 0 to 9.
389 * "*" - cancels PIN entry.
390 * Only for digits are accepted.
391 */
392 if (keyPressed) {
393
-7-
-
7/26/2019 blueValve_v1
8/9
Tuesday, September 15, 2015 8\Users\Mayor\Documents\Arduino\Arduino Resources and Projects\blueValve_v1\blueValve_v1.ino
394 if (keyPressed == '*') {
395 break;
396 }
397
398 if (int(keyPressed) >= 48 && int(keyPressed) 0)
447 {
448 digitalWrite(buzzer, HIGH);
449 delay(on);
450 digitalWrite(buzzer, LOW);
-8-
-
7/26/2019 blueValve_v1
9/9
Tuesday, September 15, 2015 8\Users\Mayor\Documents\Arduino\Arduino Resources and Projects\blueValve_v1\blueValve_v1.ino
451 delay(off);
452 reps--;
453 }
454 }
9