I am using an android phone as a remote control for the robot, I send commands via bluetooth using these codes:
char c = 'f';
int y = 2;
byte[] com = new byte[2];
com[0] = (byte) c;
com[2] = (byte) y;
The arduino clone then receives the command and reads them:
char c;
int y;
if(Serial.available() > 0){
c = Serial.read();
}
switch(c){
case 'f':
y = Serial.read();
// do anything
Serial.println(y);
break;
default:
break;
}
the problem is, it can't read separate values of the bytes being sent, I checked the serial monitor and it displays the value of the variable y and another negative value, can someone here help me? =(
char c now contains a byte of data, Serial.read is the method you used to retrieve the data. You can make y = c; or print c directly. You are currently doing a second Serial.read without a test.
Something to try, test each character received for a 1 or 0.
int ledPin = 13;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop()
{
char c = 0;
if (Serial.available() > 0)
{
c = Serial.read();
if (c == '1') // assuming you send character '1', not a byte with value 1
{
digitalWrite(ledPin, HIGH);
}
if (c == '0')
{
digitalWrite(ledPin, LOW);
}
}
}