-
Notifications
You must be signed in to change notification settings - Fork 0
/
XBee_config.ino
85 lines (78 loc) · 2.11 KB
/
XBee_config.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
#include <SoftwareSerial.h>
SoftwareSerial XBee(2, 3); // Arduino RX, TX (XBee Dout, Din)
void setup()
{
XBee.begin(9600);
XBee.println(F("Awwwww YEA we WORKIN!"));
}
void loop()
{
if (XBee.available())
{
char c = XBee.read();
switch (c)
{
case 'w': // If received 'w'
case 'W': // or 'W'
writeAPin(); // Write analog pin
break;
case 'd': // If received 'd'
case 'D': // or 'D'
writeDPin(); // Write digital pin
break;
case 'r': // If received 'r'
case 'R': // or 'R'
readDPin(); // Read digital pin
break;
case 'a': // If received 'a'
case 'A': // or 'A'
readAPin(); // Read analog pin
break;
}
}
}
// Write Digital Pin
// Send a 'd' or 'D' to enter.
// Then send a pin #
// Use numbers for 0-9, and hex (a, b, c, or d) for 10-13
// Then send a value for high or low
// Use h, H, or 1 for HIGH. Use l, L, or 0 for LOW
void writeDPin()
{
while (XBee.available() < 2)
; // Wait for pin and value to become available
char pin = XBee.read();
char hl = ASCIItoHL(XBee.read());
// Print a message to let the control know of our intentions:
XBee.print("Setting pin ");
XBee.print(pin);
XBee.print(" to ");
XBee.println(hl ? "HIGH" : "LOW");
pin = ASCIItoInt(pin); // Convert ASCCI to a 0-13 value
pinMode(pin, OUTPUT); // Set pin as an OUTPUT
digitalWrite(pin, hl); // Write pin accordingly
}
// Turn an ASCII value into either HIGH or LOW
int ASCIItoHL(char c)
{
// If received 0, byte value 0, L, or l: return LOW
// If received 1, byte value 1, H, or h: return HIGH
if ((c == '0') || (c == 0) || (c == 'L') || (c == 'l'))
return LOW;
else if ((c == '1') || (c == 1) || (c == 'H') || (c == 'h'))
return HIGH;
else
return -1;
}
// Turn an ASCII hex value into a 0-15 byte val
int ASCIItoInt(char c)
{
if ((c >= '0') && (c <= '9'))
return c - 0x30; // Minus 0x30
else if ((c >= 'A') && (c <= 'F'))
return c - 0x37; // Minus 0x41 plus 0x0A
else if ((c >= 'a') && (c <= 'f'))
return c - 0x57; // Minus 0x61 plus 0x0A
else
return -1;
}