-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth.ino
75 lines (65 loc) · 1.63 KB
/
bluetooth.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
#define DEBUG true
#define DEBUG_SERIAL Serial
#define BLUETOOTH true
#define BLUETOOTH_SERIAL Serial3
#define START_MARKER '<'
#define END_MARKER '>'
// Befehle für serielle Kommunikation (NICHT VERÄNDERN):
#define debug(_str_); if(DEBUG){DEBUG_SERIAL.print(_str_);}
#define debugln(_str_); if(DEBUG){DEBUG_SERIAL.println(_str_);}
#define bluetooth(_str_); if(BLUETOOTH){BLUETOOTH_SERIAL.print(START_MARKER);BLUETOOTH_SERIAL.print(_str_);BLUETOOTH_SERIAL.print(END_MARKER);}
// ---
#define LED 13
#define BUTTON A0
String message;
String command;
bool isListening = false;
bool isButton = false;
bool lastButton = false;
bool isLed = false;
void setup() {
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
DEBUG_SERIAL.begin(9600);
debugln("RESTART");
BLUETOOTH_SERIAL.begin(38400);
bluetooth("IR:available");
}
void loop() {
digitalWrite(LED, isLed);
isButton = !digitalRead(BUTTON);
// bluetooth senden
if (isButton != lastButton) {
bluetooth("h");
}
// bluetooth auslesen
command = receiveBluetooth();
if (command != "") {
//debugln("[" + (String)millis() + "] " + (String)command);
if (command == "h") {
isLed = !isLed;
}
}
lastButton = isButton;
}
String receiveBluetooth() {
if (BLUETOOTH_SERIAL.available() > 0) {
char c = BLUETOOTH_SERIAL.read();
if ( isListening) {
if (c == START_MARKER) {
message = "";
} else if (c == END_MARKER) {
isListening = false;
return (message);
} else {
message += c;
}
} else {
if (c == START_MARKER) {
message = "";
isListening = true;
}
}
}
return ("");
}