Help me with a Bluetooth project! (please )

Hi all! I'm working on a project where I can use my phone to control a small r/c car with Bluetooth. On the Phone side of things, I'm using MIT's App Inventor 2 to create an app to control the car. There is a picture of my interface, as well as my 'code.' On the car side of things, I'm using the HC06 Bluetooth module, and an Arduino Uno. I plan to use the app to control a motor for the forward and backward movement of the car, and also to control either a servo or geared motor to control steering. Right now, I am simply using two LED's to represent these two parts of the car (LED1 is throttle, LED2 is steering). Here is where my problem arises. Using my app, I can currently turn one LED on or off, but I cannot figure out how to also control the second one! :astonished: I was hoping that y'all could help me here. For current purposes, I want to be able to control the two LED's with my app.

NOTE: in the app, the 'Send1ByteNumber: 3' in the imageSprite 3 has no purpose, it is simply a placeholder.

My code:

//Bluetooth App - Control an RC car with bluetooth

int led1 = 13;
int led2 = 12;
int state;

void setup() {
// initialize the digital pin as an output.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {

if(Serial.available() > 0){
state = Serial.read();
}

if (state == '1') {
digitalWrite(led1, HIGH);
}

delay(100); // wait for 100ms

}

You're sending/getting 'state' and turning on LED1 based on that (but how does it turn off?)
By the same token, you need to send, say, "state2"
So, definitely implement a contingency for "state2".
Send a control byte followed by a state byte("S1", 0 - LED1 off; "S2", 1 - LED on, etc).

Later, maybe you could combine everything as one byte, send that, and then mask off the bits to determine what action to take then?

You need to design the system of codes your phone will send and your Arduino will receive. For example you might decide that each code would have two bytes - perhaps M, 64 meaning motor at speed step 64 meaning half forward speed. M, 127 would be stop and M, 192 would be half speed backwards. You could use a similar 2 byte code for steering.

Then the Arduino would expect to receive two bytes with code something like this

if (Serial.available >= 2) {
   codeByte = Serial.read();
   valueByte = Serial.read();
}

if (codeByte == 'M') {
   speedValue = valueByte;
}

if (codeByte == 'S') {
   steeringValue = valueByte;
}

You could (and probably should) add some extra bytes to reduce the possibility of errors. The code in this demo may give you some ideas.

...R

hmm, I think the major issue here is that I don't understand the 'bytes' part of this project very well. Could someone explain how they work to me? Thanks!

When a character (a number, a letter, a value) is sent, it's sent as a byte.

Anyway, in re. your posted sketch, does the LED turn off, too?
[From what's there I think that it can be turned on but not off.]

A serial connection, whether using bluetooth or a cable sends all the data as a series of bytes. A byte is 8 binary digits (bits) so it can have a maximum value is 2^8 = 256 different values, and because 0 counts as the first number the actual highest number is 255.

On the Arduino an unsigned int is made up of a pair of bytes so its maximum value = 2^16 - 1 = 65535.

The printable characters are each represented by a byte value - for example 'A' is 65 and 'a' is 97 and '0' is 48.

You could send the the number 103 as three characters using 3 bytes '1' '0' '3' or you could send it as a single byte with a value of 103 which also happens to be the code for 'g'.

If speed is not important and simple testing using the Arduino Serial Monitor is needed it is usually easier to send numbers as characters because they can be typed into the Serial Monitor, whereas there is no easy way to type characters representing, for example, the byte values of 4 or 140.

However when speed is important it makes sense to send byte values rather than characters. And this also makes it easier (quicker) for the Arduino to use the numbers.

...R

Runaway pancake... I must not have copied it, but there was a price of code where if another byte number was received, it would shut off. It was else statement, so no exact number was needed. And I had a second button that sent a random number (I think it was 53 or something).

Another question- when I send the byte '49', why does the arduino receive it as 1?is 49 the byte for the numeral 1? If I wanted the arduino to receive '2', what would I send from my phone? Lol sorry in not very advanced.

If you look up ASCII codes using Google you will get a list of all the numbers that correspnd with the printable characters. For exanple the numeral 0 is 48. 'A' is 65 and 'a' is 97.

A byte is the smallest piece of memory that can be used in an Arduino. It can store a value from 0 to 255.
A char is the same size as a byte but it can hold values from -128 to + 127.

Generally when you ask the Arduino to print a char it assumes you want to see the printable character and when you ask it to print a byte it assumes you want to see the number.

...R