-
Notifications
You must be signed in to change notification settings - Fork 1
/
uart_cmdmode.ino
125 lines (102 loc) · 2.99 KB
/
uart_cmdmode.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
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
112
113
114
115
116
117
118
119
120
121
122
123
#include <Arduino.h>
#include <SPI.h>
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
#include <SoftwareSerial.h>
#endif
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#include "BluefruitConfig.h"
#define FACTORYRESET_ENABLE 1
#define MINIMUM_FIRMWARE_VERSION "0.6.6"
#define MODE_LED_BEHAVIOUR "MODE"
/*=========================================================================*/
Adafruit_BluefruitLE_UART ble(Serial1, BLUEFRUIT_UART_MODE_PIN);
//initialize variables for where things are plugged in
int left = 12;
int right = 9;
// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}
bool analog =false;
void setup(void)
{
//while (!Serial); // required for Flora & Micro
delay(500);
Serial.begin(115200);
if ( !ble.begin(VERBOSE_MODE) )
{
error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
}
Serial.println( F("OK!") );
if ( FACTORYRESET_ENABLE )
{
/* Perform a factory reset to make sure everything is in a known state */
Serial.println(F("Performing a factory reset: "));
if ( ! ble.factoryReset() ) {
error(F("Couldn't factory reset"));
}
}
/* Disable command echo from Bluefruit */
ble.echo(false);
Serial.println("Requesting Bluefruit info:");
/* Print Bluefruit information */
ble.info();
Serial.println(F("Please use Adafruit Bluefruit LE app to connect in UART mode"));
Serial.println(F("Then Enter characters to send to Bluefruit"));
Serial.println();
ble.verbose(false); // debug info is a little annoying after this point!
/* Wait for connection */
while (! ble.isConnected()) {
delay(500);
}
// LED Activity command is only supported from 0.6.6
if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
{
// Change Mode LED Activity
Serial.println(F("******************************"));
Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
Serial.println(F("******************************"));
}
pinMode(left, OUTPUT);
pinMode(right, OUTPUT);
}
void loop(void)
{
//digitalWrite(left, HIGH);
//digitalWrite(right, HIGH);
ble.println("AT+BLEUARTRX");
ble.readline();
if (strcmp(ble.buffer, "OK") == 0) {
// no data
return;
}
// Some data was found, its in the buffer
Serial.print(F("[Recv] "));
String buf = ble.buffer;
char buzz[2];
buf.toCharArray(buzz, 2);
int leftMode = buf[0] - '0';
int rightMode = buf[1] - '0';
Serial.println(buf);
digitalWrite(left, leftMode);
digitalWrite(right, rightMode);
/**
for(int i=0; i <5; i++)
{
digitalWrite(left, leftMode);
digitalWrite(right, rightMode);
delay(1000);
digitalWrite(left, 0);
digitalWrite(right, 0);
delay(1000);
}
**/
ble.waitForOK();
// if (! ble.waitForOK() ) {
// Serial.println(F("Failed to send?"));
//}
}