Go Down

Topic: What is wrong with my code? (Read 239 times) previous topic - next topic

Medison

   I'm using Arduino Mega 2560 to develop Bluetooth Robot, but Bluetooth connected without Robot performing an action,



String readdata;



 int AIA =8; 
 int AIB = 9; 
 int BIA=10;
 int BIB=11;


 

void setup() {
 Serial1.begin(9600);



  pinMode(AIA, OUTPUT); // set pins to output
  pinMode(AIB, OUTPUT);
  pinMode(BIA, OUTPUT);
  pinMode(BIB, OUTPUT);
 
 

}

 

void loop() {

 

 while(Serial1.available())
{
delay(10);

char c=Serial1.read();
readdata+=c;
}
if(readdata.length()>0)
{
 Serial1.println(readdata);
 
if(readdata=="forward")
{

   digitalWrite(AIA, HIGH);;
   digitalWrite(AIB, LOW);
   digitalWrite(BIA, HIGH);
   digitalWrite(BIB, LOW);
   delay(1000);
 
}
else if(readdata=="right")
{
   
    digitalWrite(AIA, HIGH);
    digitalWrite(AIB, LOW);
    digitalWrite(BIA, LOW);
    digitalWrite(BIB, LOW);
    delay(1000);
}
else if(readdata=="left")
{
    digitalWrite(AIA, LOW);
    digitalWrite(AIB,LOW );
    digitalWrite(BIA, HIGH);
    digitalWrite(BIB, LOW);
    delay(1000);
 
}
else if(readdata=="reverse")
{
 
    digitalWrite(AIA, LOW);
    digitalWrite(AIB, HIGH);
    digitalWrite(BIA, LOW);
    digitalWrite(BIB, HIGH);
    delay(1000); 
 
}
else
{
    digitalWrite(AIA, LOW);
    digitalWrite(AIB, LOW);
    digitalWrite(BIA, LOW);
    digitalWrite(BIB, LOW);
    delay(1000);
}

readdata="";
}
 
}



 

larryd

Don't use String, just use a character array.

Look up:   strcmp




No technical PMs.
The last thing you did is where you should start looking.

jremington

#2
Jul 08, 2018, 03:49 am Last Edit: Jul 08, 2018, 03:49 am by jremington
It is much easier just to send single characters as commands: 'F' = forward, 'B'=back, 'R'=right, etc.

Then you don't have to use Strings (bad anyway on Arduino) or C-strings (character arrays).

nielyay

Quote
String readdata;



 int AIA =8;
 int AIB = 9;
 int BIA=10;
 int BIB=11;
*dont use string

Code: [Select]
array readdata;



 int AIA =8;
 int AIB = 9;
 int BIA=10;
 int BIB=11;

PaulS

Quote
*dont use string
Wrong. DO use strings. Do not use Strings. Case makes a BIG difference.
The art of getting good answers lies in asking good questions.

Go Up