#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo1, myservo2, myservo3;
int bluetoothTx = 14;
int bluetoothRx = 15;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
myservo1.attach(9);
//Setup usb serial connection to computer
Serial.begin(9600);
//Setup Bluetooth serial connection to android
bluetooth.begin(9600);
}
void loop()
{
if(bluetooth.available()>0)
{
if(bluetooth.available()>=4)
{
char buffer[4];
for(int i=0;i<4;i++)
{
buffer[i]=bluetooth.read();
}
int incomingValue = atoi(buffer);
if(incomingValue>=1000 && incomingValue<=1360)
{
unsigned int servopos = bluetooth.read();
unsigned int servopos1 = bluetooth.read();
unsigned int realservo = (servopos1 * 256) + servopos;
if (realservo >= 1000 && realservo < 1360)
{
int servo1 = realservo;
servo1 = map(servo1, 1000, 1360, 0, 360);
myservo1.write(servo1);
Serial.println("servo 1 ON");
delay(10);
}
}
}
if(bluetooth.available()==2)
{
char inbit=bluetooth.read();
if(inbit=='A'||inbit=='D');
{
char myval= bluetooth.read();
if(myval== 'A')
{
myservo1.attach(9);
}
if(myval== 'D')
{
myservo1.detach();
}
}
}
}
}
1 Answer 1
There are so many things wrong with that program I can't begin to sort it out.
Here's some of the most glaringly obvious errors:
if(bluetooth.read() >=0 && bluetooth.read() <=360)
Read a character. Check it's at least 0. Read a second character. Check it's less than 360. Since characters are between 0 and 255 that will always succeed if there is one character of any form to read (if there is no characters to read you get -1).
else if(bluetooth.read()=='A'||'B'||'C'||'D'||'E'||'F');
Read another character and compare it to 'A'. This if is true if the character read is A. It also succeeds if B, or if C or if D, etc. That's not "if it's equal to C", just "if C". C is a character literal, so it's equivalent to a number. Any number that is not 0 is TRUE, therefore it's equivalent to:
if character is A or TRUE or TRUE or TRUE...
Which of course is just true, so it will always succeed regardless.
char myval= bluetooth.read();
if(myval== 'A')
Read yet another character from bluetooth (I sure hope there are enough there...) then compare it to a list of characters that are the same as you looked at above. You're comparing a new character this time, not the one you compared with the list a few lines beforehand.
-
Would the changes I made do any help?. I want my code to execute first if loop if the bluetooth sends a value between 1000 and 1360 and second if loop if it sends character A or D.@MajenkoAnita B– Anita B2016年04月18日 19:28:24 +00:00Commented Apr 18, 2016 at 19:28
-
Well, no. Because you can never read a character that is greater than 255 since such a thing doesn't exist. You need to read some tutorials on using serial on the Arduino.Majenko– Majenko2016年04月18日 19:31:48 +00:00Commented Apr 18, 2016 at 19:31
-
What if I add a buffer?Can I then send values more than 255?and then compare value in the buffer@MajenkoAnita B– Anita B2016年04月18日 20:02:23 +00:00Commented Apr 18, 2016 at 20:02
-
There are a number of ways of transferring data using serial. Simplest is with an ASCII representation. Then yes, read characters into a buffer and use something like
atoi
to convert that buffer into a number.Majenko– Majenko2016年04月18日 20:05:21 +00:00Commented Apr 18, 2016 at 20:05 -
Could you check this code please?@MajenkoAnita B– Anita B2016年04月18日 20:38:53 +00:00Commented Apr 18, 2016 at 20:38
bluetooth.read()
twice in anif
may not be that what you want.if
statement.