Created
January 7, 2016 06:14
-
-
Save reefwing/eab05c12b615070732c3 to your computer and use it in GitHub Desktop.
Bluetooth Remote Control Sketch for Bluno
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* AVA BlueTooth Manual Overide (using iOS App) - Arduino Uno | |
* | |
* Bluno integrates a BT 4.0 (BLE) module into an Arduino Uno. | |
* Bluno uses the TI CC2540 BT 4.0 chip. | |
* | |
* Reading temperature or humidity takes about 250 milliseconds | |
* Sensor readings may also be up to 2 seconds old (its a very slow sensor) | |
* | |
* This microcontroller handles manual control of the robot via | |
* an iOS App running on an iPhone 5. It communicates with the | |
* Mega using serial comms. | |
* | |
* Reefwing Software | |
* Version: 2.4 | |
* Date: 30th December 2015 | |
*/ | |
#include "Arduino.h" | |
#include <SoftwareSerial.h> | |
#include "U8glib.h" | |
#include "TimerOne.h" | |
#include "blunoAccessory.h" | |
// Pin Definitions - Digital (14 Total) | |
// | |
// Pins 0 & 1 are used to communicate with the BLE module and USB Serial | |
// Pin: 2 DHT11 Temp/Humidity Sensor | |
// 3 RGB LED (blue) | |
// 6 OLED Reset | |
// 7 OLED DC | |
// 8 Buzzer | |
// 9 RGB LED (red) | |
// 10 RGB LED (green) | |
// 11 Relay | |
// 12 Spare | |
// 13 LED | |
// | |
// Analogue (6 Total) | |
// | |
// Pin: A0 Joystick | |
// A1 Knob | |
// A2 Spare | |
// A3 Spare | |
// A4 I2C Bus SDA | |
// A5 I2C Bus SCL | |
const char VERSION[] = "BLE Remote v2.4"; | |
const byte pin_MegaSerialRx = 4; | |
const byte pin_MegaSerialTx = 5; | |
const byte pin_LED = 13; | |
// Accessory Shield and OLED object definition | |
blunoAccessory accessoryShield; | |
U8GLIB_SSD1306_128X64 OLED(U8G_I2C_OPT_NONE); | |
bool commsDetected, remoteControlled; | |
float humidity, temp, red, blue, green; | |
enum task_t { statusReport, remoteControl, patrol, followIR, avoidIR, noTask }; | |
task_t task; | |
String taskDescription; | |
// Set up a new serial port for comms with the Mega 2560 | |
SoftwareSerial megaSerial(pin_MegaSerialRx, pin_MegaSerialTx); | |
void timerISR() { | |
// Toggle LED's | |
digitalWrite(pin_LED, digitalRead( pin_LED ) ^ 1); | |
if (commsDetected) {} | |
else if (blue == 0) { | |
blue = 25; | |
} else { | |
blue = 0; | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.println(VERSION); | |
pinMode(pin_LED, OUTPUT); | |
pinMode(pin_MegaSerialRx, INPUT); | |
pinMode(pin_MegaSerialTx, OUTPUT); | |
megaSerial.begin(9600); | |
digitalWrite(pin_LED, HIGH); | |
// Use a Hardware Interrupt to blink LED | |
// Set a timer of length 1000000 microseconds (or 1 sec) | |
Timer1.initialize(1000000); | |
Timer1.attachInterrupt(timerISR); | |
// Init Accessory Shield | |
task = noTask; | |
OLED.setColorIndex(1); // displayMode : u8g_MODE_BW | |
accessoryShield.begin(); | |
red = 0; | |
green = 0; | |
blue = 25; | |
commsDetected = false; | |
remoteControlled = false; | |
temp = accessoryShield.readTemperature(); | |
humidity = accessoryShield.readHumidity(); | |
accessoryShield.setRGBLed(red, green, blue); | |
} | |
// Display function | |
// See https://code.google.com/p/u8glib/wiki/tpictureloop | |
void draw (void) { | |
OLED.setFont(u8g_font_unifont); | |
OLED.drawStr(0, 16, VERSION); | |
OLED.setPrintPos(0, 32); | |
OLED.print("Temp : "); | |
OLED.print(temp); | |
OLED.print("C"); | |
OLED.setPrintPos(0, 48); | |
OLED.print("Humidity: "); | |
OLED.print(humidity); | |
OLED.print("%"); | |
OLED.setPrintPos(0, 64); | |
OLED.print(taskDescription); | |
} | |
void loop() { | |
unsigned long elapsedTime = 0; | |
unsigned long displayTime = 0; | |
// Update Sensors | |
if (millis() - elapsedTime >= 2000) { | |
humidity = accessoryShield.readHumidity(); | |
temp = accessoryShield.readTemperature(); | |
elapsedTime = millis(); | |
} | |
// Refresh OLED & RGB LED | |
if (millis() - displayTime >= 10000) { | |
if (remoteControlled) { | |
red = 25; | |
green = 0; | |
blue = 0; | |
} else { | |
red = 0; | |
if (green > 0) { | |
green = 0; | |
commsDetected = false; | |
} | |
if (commsDetected) { | |
green = 25; | |
blue = 0; | |
} | |
} | |
displayTime = millis(); | |
} | |
accessoryShield.setRGBLed(red, green, blue); | |
OLED.firstPage(); | |
do { | |
draw(); | |
} while ( OLED.nextPage() ); | |
// Handle any remote commands | |
delay(100); | |
if (Serial.available()) { | |
char data = Serial.read(); | |
commsDetected = true; | |
switch (data) { | |
case 's': | |
Serial.write("Ack - STOP"); | |
megaSerial.write(data); | |
break; | |
case 'f': | |
Serial.write("Ack - Forward"); | |
megaSerial.write(data); | |
break; | |
case 'b': | |
Serial.write("Ack - Back"); | |
megaSerial.write(data); | |
break; | |
case 'l': | |
Serial.write("Ack - Left"); | |
megaSerial.write(data); | |
break; | |
case 'r': | |
Serial.write("Ack - Right"); | |
megaSerial.write(data); | |
break; | |
case 'w': | |
Serial.write("Ack - F1"); | |
megaSerial.write(data); | |
break; | |
case 'x': | |
Serial.write("Ack - F2"); | |
megaSerial.write(data); | |
break; | |
case 'y': | |
Serial.write("Ack - F3"); | |
megaSerial.write(data); | |
break; | |
case 'z': | |
Serial.write("Ack - F4"); | |
megaSerial.write(data); | |
break; | |
case '0': | |
task = statusReport; | |
taskDescription = "T0: Status"; | |
Serial.print(taskDescription); | |
megaSerial.write(data); | |
remoteControlled = false; | |
break; | |
case '1': | |
task = remoteControl; | |
taskDescription = "T1: Remote"; | |
Serial.print(taskDescription); | |
megaSerial.write(data); | |
remoteControlled = true; | |
break; | |
case '2': | |
task = patrol; | |
taskDescription = "T2: Patrol"; | |
Serial.print(taskDescription); | |
megaSerial.write(data); | |
remoteControlled = false; | |
break; | |
case '3': | |
task = followIR; | |
taskDescription = "T3: Follow IR"; | |
Serial.print(taskDescription); | |
megaSerial.write(data); | |
remoteControlled = false; | |
break; | |
case '4': | |
task = avoidIR; | |
taskDescription = "T4: Avoid IR"; | |
Serial.print(taskDescription); | |
megaSerial.write(data); | |
remoteControlled = false; | |
break; | |
default: | |
char errorMsg[32]; | |
String error = "Unknown Command - "; | |
error += data; | |
error.toCharArray(errorMsg, 32); | |
Serial.write(errorMsg); | |
break; | |
} | |
Serial.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment