Hi,
I am using an Arduino UNO with a BlueSmirf Silver Bluetooth shield.
I have succesfully connected to the BT module (Baud 115200) using an Android phone using various Apps
(Arduino Commander, Microcontroller BT).
With an LED connected to Pin 13 I can succesfully turn it on and off using the Arduino Commander App and the
StandardFirmata sketch so I know I have no connection issues/problems with the boards etc.
I want to be able to turn pins on and off using the Microcontroller BT app but I am slightly confused by the instructions on
the app web page (shown in code below) I am trying to work out how to code for this.
My understanding is that when you set up a button in the app and press it, it sends a
number 1,0 (button 1, state 0)
1,1 (button 1,state 1) through the BT module to the serial port on the Arduino.
I then need to somehow in the code monitor the serial input to look for certain states
and change the pin values accordingly.
Can someone explain how I need to set this up in the code. so far I cant seem to get it to work (see code at the end)
App instructions followed by my best guess in the second set of code:
Add a highly customizable programmable Bluetooth control panel to your electronics projects with microController BT. Drag, drop, resize and edit widgets to build the perfect controller for your microcontroller.
While designed with the Arduino microcontroller in mind, using low cost Bluetooth transceivers, microController BT should work with most projects that support serial communication and most Bluetooth transceivers that support SPP.
microController BT outputs standard 2 byte messages. The messages contain a key (0-255) and a value (0-255). For monitoring output, it listens for a standard 3 byte message. This message should contain a key (0-255), a highByte and a lowByte representing a value (0-1023).
outgoing: (key, value)
incoming: (key, highByte(value), lowByte(value))
For simple projects, you can directly plug the standard 2 byte messages into the digitalWrite/analogWrite functions, as shown below. For more advanced projects, you may choose to listen for particular keys and route accordingly.
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT); //set for each pin
}
void loop() {
if (Serial.available() >= 2) {
int key = Serial.read();
int val = Serial.read();
digitalWrite(key, val);
}
}
To send values to microController BT for monitoring, simply write the standard 3 byte message to the serial port as shown below.
Serial.write(key);
Serial.write(highByte(val));
Serial.write(lowByte(val));
microController BT also outputs standard 2 byte messages when certain application events occur. These messages are useful for projects that change based on application state.
device connected: (0, 255)
start edit mode: (0, 253)
end edit mode: (0, 254)
My Code, adapted from an INSTRUCTABLE
/*
simple LED test
*/
char val; // variable to receive data from the serial port (bluetooth module)
int ledpin = 2; // LED connected to pin 2 (on-board LED)
void setup()
{
pinMode(ledpin = 13, OUTPUT); // pin 13 (on-board LED) as OUTPUT
Serial.begin(115200); // start serial communication at 115200bps
}
void loop() {
if( Serial.available() ) // if data is available to read
{;}
val = Serial.read(); // read it and store it in 'val'
if( val == '1,0' ) // if 1,0' value recieved from bluetooth module
{
digitalWrite(ledpin, LOW); // turn Off pin 13 off
delay(1000); // waits for a second
Serial.println("13 off");
}
if( val == '1,1' ) // if '1,1' value recieved from bluetooth
{
digitalWrite(ledpin = 13, HIGH); // turn ON pin 13 on
delay(1000); // waits for a second
Serial.println("13 on");
}
}