1
1
#include < SoftwareSerial.h>
2
- #define PIN A5
2
+
3
+ // We're using pin 10 and 11 in RX and TX mode
3
4
SoftwareSerial BT (10 ,11 );
5
+ // String that will store the command from mobile device
4
6
String readdata;
5
- char data = 0 ; // Variable for storing received data
7
+ char data = 0 ;
6
8
bool ledOn = false ;
7
- int startDelay, stopAfter;
9
+ // Variables used for implementing delay mechanism
10
+ int startDelay, stopAfter;
11
+ // Used for reading data as it is received from the Bluetooth input stream
8
12
char c;
9
- int bulb_1 = 0 , bulb_2 = 0 ;
13
+ // The two vaiables represent the two output pins to which bulbs are connected
14
+ int bulb_1 = 0 , bulb_2 = 0 ;
10
15
11
16
void setup ()
12
17
{
13
18
BT.begin (38400 );
14
- Serial.begin (9600 ); // Sets the baud for serial data transmission
15
- pinMode (3 , OUTPUT); // Sets digital pin 13 as output pin
16
- pinMode (2 , OUTPUT);
19
+ Serial.begin (9600 ); // Sets the baud for serial data transmission
20
+ pinMode (3 , OUTPUT); // Sets digital pin 3 as output pin
21
+ pinMode (2 , OUTPUT); // Sets digital pin 2 as output pin
17
22
}
18
23
19
24
void loop ()
20
25
{
21
- while (BT.available ()) // Send data only when you receive data:
26
+ while (BT.available ()) // Receive data as long as it is available
22
27
{
23
28
delay (10 );
24
29
data = BT.read ();
25
30
readdata+=data;
26
31
}
27
32
28
- // Read the incoming data & store into data
33
+ // Read the incoming data & store into readdata
29
34
if (readdata.length ()>0 )
30
35
{
31
- Serial.print (readdata); // Print Value inside data in Serial monitor
32
- Serial.print (" \n " );
36
+ Serial.print (readdata); // Print value inside data in Serial monitor
37
+ Serial.print (" \n " );
33
38
34
39
if (readdata == " 1" ) {
40
+ // If bulb 1 is selected, set bulb_1 to pin 3
35
41
bulb_1 = 3 ;
42
+ // Keep the bulb off initially
36
43
digitalWrite (bulb_1, LOW);
44
+ // Make readdata string empty
37
45
readdata = " " ;
38
46
}
39
47
40
48
else if (readdata == " 2" ) {
49
+ // If bulb 2 is selected, set bulb_2 to pin 2
41
50
bulb_2 = 2 ;
51
+ // Keep the bulb off initially
42
52
digitalWrite (bulb_2, LOW);
43
53
readdata = " " ;
44
54
}
45
55
46
56
else if (readdata == " 01" ) {
57
+ // If bulb 1 has been unchecked, make bulb_1 as 0 to disselect it
47
58
bulb_1 = 0 ;
48
- digitalWrite (bulb_1, LOW);
49
59
readdata = " " ;
50
60
}
51
61
52
62
else if (readdata == " 02" ) {
63
+ // If bulb 1 has been unchecked, make bulb_1 as 0 to disselect it
53
64
bulb_2 = 0 ;
54
- digitalWrite (bulb_2, LOW);
55
65
readdata = " " ;
56
66
}
57
67
58
68
if (readdata == " STOP" ) {
69
+ // This signal is sent by pressing disconnect button on the app
70
+ // It makes both the bulbs non-funtional
59
71
bulb_1 = 0 ;
60
72
bulb_2 = 0 ;
61
73
readdata = " " ;
62
- digitalWrite (bulb_1, LOW);
63
- digitalWrite (bulb_2, LOW);
64
74
}
65
75
66
76
if (readdata == " ON" ) {
77
+ // This signal is sent by tapping the bulb when it is off
78
+ // Used to switch on the selected bulb(s)
67
79
digitalWrite (bulb_2, HIGH);
68
80
digitalWrite (bulb_1, HIGH);
69
81
readdata = " " ;
70
82
ledOn = true ;
71
83
}
72
84
else if (readdata == " OFF" ) {
85
+ // This signal is sent by tapping the bulb when it is on
86
+ // Used to switch off the selected bulb(s)
73
87
digitalWrite (bulb_2, LOW);
74
88
digitalWrite (bulb_1, LOW);
75
89
readdata = " " ;
@@ -79,14 +93,14 @@ void loop()
79
93
// Get index of 'e', number following 'e' indicates the number of seconds for which the bulb(s) should remain on
80
94
c = readdata.indexOf (' e' );
81
95
96
+ // Get the start delay and convert it to int
82
97
startDelay = readdata.substring (1 , c-1 ).toInt ();
98
+ // Get the stop delay and convert it to int
83
99
stopAfter = readdata.substring (c+1 , readdata.length ()-1 ).toInt ();
84
100
85
- if (startDelay != 0 ) {
86
- delay (startDelay*10 );
87
- digitalWrite (bulb_1, HIGH);
88
- digitalWrite (bulb_2, HIGH);
89
- }
101
+ delay (startDelay*10 );
102
+ digitalWrite (bulb_1, HIGH);
103
+ digitalWrite (bulb_2, HIGH);
90
104
91
105
if (stopAfter != 0 ) {
92
106
delay (stopAfter*10 );
0 commit comments