0

I have 2 Arduinos, mounted with Xbee Shield and Xbee S1 module on each. One arduino is mounted on car chassis while another is connected with PC. Our AIM is to send signal from the arduino connected on PC and the motor will be controlled by the Arduino connected on car chassis. Here is the code of the arduino connected with PC:

char msg= "";
void setup(){
 Serial.begin(9600);}

void loop(){
 msg=Serial.read();
 serial.println(msg);
}

Here is the code of the Arduino on car chassis:

char msg = ' '; 
int motorA1 = 2;
int motorA2 = 3;
int motorB1 = 4;
int motorB2 =5;

void setup() {
  pinMode(motorA1, OUTPUT);
  pinMode(motorA2, OUTPUT);
  pinMode(motorB1, OUTPUT);
  pinMode(motorB2, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  while(Serial.available()>0){
  msg=Serial.read();
  switch(msg){
   case 'f':Serial.println("Forward");break;
   case 'b':Serial.println("Backward");break;
   case 'r':Serial.println("Right");break;
   case 'l':Serial.println("Left");break;
   case 's':Serial.println("Stop");break;
 }
if(msg=='f'){
  digitalWrite(motorA1,HIGH);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1,HIGH);
  digitalWrite(motorB2,LOW);
  Serial.println("Running Forward");
  }
if(msg=='b'){
  digitalWrite(motorA1,LOW);
  digitalWrite(motorA2,HIGH);
  digitalWrite(motorB1,LOW);
  digitalWrite(motorB2,HIGH);
  }
if(msg=='s'){
  digitalWrite(motorA1,LOW);
  digitalWrite(motorA2,LOW);
  digitalWrite(motorB1,LOW);
  digitalWrite(motorB2,LOW);
  }
if(msg=='r'){
  digitalWrite(motorA1,HIGH);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1,LOW);
  digitalWrite(motorB2,LOW);
  }
if(msg=='l'){
  digitalWrite(motorA1,LOW);
  digitalWrite(motorA2,LOW);
  digitalWrite(motorB1,HIGH);
  digitalWrite(motorB2,LOW);
  }
}
}

Now our next AIM is to send back a feedback from car to the Arduino terminal of PC, e.g. if we are giving 'f' as a input and we get a reply 'Running Forward' on the next line. What line of code should we incorporate for that?

  • Isn't your code doing that already in the switch statement? – mico May 11 '17 at 11:58
  • 2
    You need two serial connections on the pc Arduino. One that connects via xbee and one that connects to the pc/usb monitor. You need to describe what hardware you have because it can be complicated if you are using a shield with fixed serial pin assignments. – Visual Micro May 11 '17 at 14:28
0

//your code looks almost ok. The thing that i am feeling wrong is that arduino at the PC end. It should communicate with PC and 2nd arduino at the same time. So use arduino mega or "#include " library to avail extra Serial at this end if you are using arduin uno. Your code will be like this..

char msg= "";

void setup(){
Serial.begin(9600);
Serial1.begin(9600)//<----error
}

void loop(){
 msg=Serial.read();
 serial1.println(msg);//<- - -error
}
  • Please tell me how to add extra serial on the arduino at PC using software serial library as I have UNO. please provide me the final code – Ritam Das May 13 '17 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.