-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirmware
More file actions
111 lines (99 loc) · 1.98 KB
/
Firmware
File metadata and controls
111 lines (99 loc) · 1.98 KB
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
//Created by Intelectron India, 2017
// FREE TO EDIT AND MODIFY
#include <SoftwareSerial.h>
#define FAN 1
#define LIGHT 2
int fanPin = 11;
int lightPin = 10;
int FANstate = 0;
int LIGHTstate = 0;
SoftwareSerial BT(9,8);
//comment the following line to disable debugging mode
#define __DEBUG_
String codes[] = {"", "FAN", "LIGHT"};
void setup() {
#ifdef __DEBUG_
Serial.begin(115200);
#endif
BT.begin(115200);
#ifdef __DEBUG_
Serial.println("DEBUG mode ON");
#endif
pinMode(fanPin, OUTPUT);
pinMode(lightPin, OUTPUT);
}
void loop() {
if (BT.available()) {
processSerial();
}
}
void processSerial() {
char _temp[20] = {};
int code = -1;
BT.readBytesUntil('#', _temp, 20);
String buffer = String(_temp);
//check which code was sent
switch (buffer[0]) {
case '$':
code = FAN;
break;
case '@':
code = LIGHT;
break;
}
buffer.remove(0, 1); //removes the identifier
processSerialData(buffer, code);
#ifdef __DEBUG_
Serial.println(codes[code]);
#endif
Serial.flush();
}
void processSerialData(String buffer, int code) {
switch (code) {
case FAN:
processFAN(buffer);
break;
case LIGHT:
processLIGHT(buffer);
break;
}
}
void processFAN(String buffer) {
char state = buffer[0];
if (state == '1') {
buffer.remove(0, 2);
FANstate = 1;
//TURN ON FAN
#ifdef __DEBUG_
Serial.println("FAN turned ON");
#endif
digitalWrite(fanPin, HIGH);
}
else if (state == '0') {
//turn off FAN
FANstate = 0;
digitalWrite(fanPin, LOW);
#ifdef __DEBUG_
Serial.println("FAN turned OFF");
#endif
}
}
void processLIGHT(String buffer) {
char state = buffer[0];
if (state == '1') {
//turn on LIGHT
LIGHTstate = 1;
#ifdef __DEBUG_
Serial.println("LIGHT turned ON");
#endif
digitalWrite(lightPin, HIGH);
}
else if (state == '0') {
//turn off LIGHT
LIGHTstate = 0;
#ifdef __DEBUG_
Serial.println("LIGHT turned OFF");
#endif
digitalWrite(lightPin, LOW);
}
}