-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLExAR_control.ino
85 lines (80 loc) · 2.59 KB
/
BLExAR_control.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
/** Control Code for TinyBlueX and BLExAR iOS app
* -- This code allows the BLExAR iOS app to control the pins on
* -- the TinyBlueX combined ATtiny85 microcontroller and Bluetooth
* -- Low Energy module (CC254x chip also known as:
* -- HM-10, AT-09, MLT-BT05, JDY-08, etc.)
*
* by Joshua Hrisko, Maker Portal LLC (c) 2021
*
*/
#include <SoftwareSerial.h>
SoftwareSerial ble_device(0,1); // BLE TX-> ATtiny85 PB0, BLE RX-> ATtiny85 PB1
String strglob= "";
String pin_num_char = "";
int pin_num = 255;
bool analog_bool = false;
bool digital_bool = false;
void setup() {
ble_device.begin(9600); // start BLE device
delay(500); // wait until BLE device starts
// Uncommenting the code below will change thee name of the
// BLE module (the name change only needs to occur once)
// ble_device.println("AT+NAMETinyBlue"); // change device name
// delay(250); // wait for change
// ble_device.println("AT+RESET"); // reset module to enact name change
// delay(3000); // wait for reset
}
void loop() {
char ble_char = ble_device.read();
if (int(ble_char)!=-1){ // make sure it's a valid character
if (ble_char == '\n'){
if (analog_bool){
pinMode(pin_num,OUTPUT);
delay(50);
analogWrite(pin_num,strglob.toInt());
analog_bool = false;
pin_num = 255;
} else if (digital_bool){
pinMode(pin_num,OUTPUT);
delay(50);
if (strglob == "1"){
digitalWrite(pin_num,HIGH);
} else if (strglob == "0"){
digitalWrite(pin_num,LOW);
pinMode(pin_num,INPUT);
delay(50);
}
digital_bool = false;
pin_num = 255;
}
pin_num = 255;
strglob = "";
} else {
if (analog_bool == false and digital_bool == false) {
if (ble_char == 'D'){
digital_bool = true;
} else if (ble_char == 'A'){
analog_bool = true;
} else {
}
} else{
if (pin_num == 255){
if (ble_char != 'e'){
pin_num_char += ble_char;
} else if (ble_char == 'e'){
pin_num = (String(pin_num_char).toInt());
pin_num_char = "";
}
} else {
if (int(ble_char)!=-1 && ble_char!='.' && ble_char!='@' && ble_char!=' ' && ble_char!='\r' && ble_char!='\n' && ble_char!='\0'){
strglob += String(ble_char);
}
}
}
}
}
if ((ble_char)!=-1 && ble_char!=' ' && ble_char!='\n' && ble_char!='\r' && ble_char!='@' && ble_char!='.' && ble_char!='\0'){
delay(50);
// ble_device.println(ble_char); // uncomment to send back to BLExAR
}
}