Pages: [1]   Go Down
Author Topic: Android/Arduino bluetooth control problem  (Read 928 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

Code:
/*
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");
  }
}

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please try posting your code again. All code, and only code, goes in code tags. But, before you post any code, use Tools + Auto Format.

The random
             indenting in
    your code makes
          it very hard to follow.

Putting each { on its own line would be helpful, too. Doing this, and Tools + Auto Format with your code makes loop like this:
Code:
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");
  }
}

As you can see, there is a useless block of code executed if there is serial data present to be read. Then, whether there is, or is not, anything to be read, you read one character. Oops. Fail!

Now, assuming that you fix that, please tell me which single key on the keyboard produces 0,1 on the screen. You are only reading one character, and expecting it to match three characters. That, on an Arduino, will NEVER happen.

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks,
   The code seems to appear fine in my window, my apologies if it is hard to read.  I don't see any point in reposting as the thread will get long and confusing.

If you read the instructions for the app in the top set of code I believe it states that when a touchscreen button (Set up by me) is pressed it outputs a value of, for example 1,0.  One being the number assigned to the button and the zero being the state that the button is in. Button two would output a value of 2,0 when in the off position (if I am understanding this correctly)

If you press the touchscreen button 1 it outputs a value of 1,1 and so on.

I guess I a trying to get the code to recognise the difference between a 1,1 output (ON) and a 1,0 output (OFF) through the serial monitor.

I may have this completely wrong, I was hoping someone could read the instructions and give me their own understanding of how it should work and how I could implement it.

Thanks in advance
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If you read the instructions for the app in the top set of code I believe it states that when a touchscreen button (Set up by me) is pressed it outputs a value of, for example 1,0.  One being the number assigned to the button and the zero being the state that the button is in.
Not quite. It outputs two or three bytes - not one value. The first byte is the key, which can be a pin number or an index into an array of pin numbers. The second, or second and third, are the value.

The code then requires 2 or 3 Serial.read() statements to read. The code, in the top code box in your initial post, following the "For simple projects..." statement shows how to read the simple (two byte) values being sent.

Quote
If you press the touchscreen button 1 it outputs a value of 1,1 and so on.
A set of values, not a value.

Quote
I guess I a trying to get the code to recognise the difference between a 1,1 output (ON) and a 1,0 output (OFF) through the serial monitor.
As I said just above, you posted code to do that.

Keep in mind that the key value you are sending is NOT a pin number. It could be, but you can't use pins 0 and 1 while doing Serial input or output.

You could put the pins you want to control, say 3,7,8, and 10, in an array. The key would then be the array index, and the pin number would be the value at that position in the array.

Quote
I may have this completely wrong, I was hoping someone could read the instructions and give me their own understanding of how it should work and how I could implement it.
It's easy to read code in a code box. It's hard to read other text in a code box. That's why I asked you to modify your post. But, I have read the instructions, and made comments above about them.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thankyou for your patience, as you can tell I am fairly new to this.

I think I understand what you mean about the bytes, so the app is sending two figures to the serial monitor via bluetooth,
one after another - 1 (button no) followed by a 0 (value). My problem is how I can interpret the different sets of data into
two variables, changing the pin state.  I know the is the bit that translates the bytes however I dont understand
what the digitalWrite (key,val); is doing.  Is this writing this figure to the arduino's memory?

void loop() {
if (Serial.available() >= 2) {
int key = Serial.read();
int val = Serial.read();

digitalWrite(key, val);

Sorry, but could you give me an example of how I can use the values to switch the pin state? it would
be very much appreciated.

My code as it currently stands is as follows. I have taken away the 'key value for now to try and simplify it.
  At the moment it is not working, however if I go to the terminal on the app and manually change the
val = 1 it does change the pin state and turn the LED on

Code:
/*
simple LED test
*/


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() >= 2)       // if data is available to read
  {;}
    int key = Serial.read();
    int val = Serial.read();
    digitalWrite (key,val);
 
if( val == '0' )               

  {
    digitalWrite(ledpin, LOW);    // turn Off pin 13 off
    delay(1000);                  // waits for a second 
    Serial.println("13 off");
  }

if( val == '1' )             
  {
    digitalWrite(ledpin, HIGH);  // turn ON pin 13 on
    delay(1000);                  // waits for a second
    Serial.println("13 on");
  }
}



Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is this writing this figure to the arduino's memory?
No. It is setting the pin HIGH or LOW.

Quote
Sorry, but could you give me an example of how I can use the values to switch the pin state? it would
be very much appreciated.
That's exactly what that code does.

Quote
My code as it currently stands is as follows.
Code:
    if( Serial.available() >= 2)       // if data is available to read
    {
      ;
    }
If there is serial data to be read, do nothing. That's what this block of code says.

It there is not, do nothing, either.

Then, regardless of whether there is, or is not, serial data available to be read:
Code:
    int key = Serial.read();
    int val = Serial.read();
    digitalWrite (key,val);
Read it.

Can you see that this is wrong?

It starts with the {;} bit. Put as simply as possible, GET RID OF THAT CRAP!

You should have, in loop():
Code:
   if(Serial.available() >= 2)
   {
      int key = Serial.read();
      int val = Serial.read();
      digitalWrite (key,val);
   }
That's it. Nothing more. Nothing less.

Oh, and this is not a good idea:
Code:
  pinMode(ledpin = 13, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
First, ledpin is not defined. Second, assigning a value this way is not a good idea. You should have:
const int ledpin = 13;
above setup(), and you should remove the = 13 bit from this statement.

You should also make sure that the sender sends 13,0 or 13,1 rather than 0,0 or 0,1.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul, thankyou.
     I understand it now, and thanks again for your patience.  The digitalWrite was the
bit that was throwing me but I see now that the key,val is defining the
pin number and setting the state.

I have fixed the other parts as well and it works perfectly

Cheers

Tim
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have fixed the other parts as well and it works perfectly
Excellent.
Logged

Pages: [1]   Go Up
Jump to: