-1

My robot car won’t respond to my command. The code below worked before however, this time there is no response. I’ve tried experimenting with the code in the first while loop. I’ve tried different code from other projects similar to this. I’ve played with the schematics however, nothing seems to work.

This is my code:

#include <SoftwareSerial.h> // TX RX software library for bluetooth
#include <Servo.h> // servo library 
Servo myservo1, myservo2, myservo3; // servo name
int bluetoothTx = 10; // bluetooth tx to 10 pin
int bluetoothRx = 11; // bluetooth rx to 11 pin
int motorOne = 4;
int motorOne2 = 5;
int motorTwo = 6;
int motorTwo2 = 7;
int enA = 3;
int enB = 2;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);


//initial motors pin

char command; 
char Value;

void setup()
{
  myservo1.attach(2); // attach servo signal wire to pin 9
  myservo2.attach(8);
  myservo3.attach(12);
   
  //Setup usb serial connection to computer
  Serial.begin(9600);
  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);
}
void loop()
{
  while (bluetooth.available() > 2) {
    Value = bluetooth.read();
    Serial.println(Value);
  }

  if ( Value == 'F') {
    // Robo Pet Run Forward
    digitalWrite(enA, 255);
    digitalWrite(enB, 255);
    digitalWrite(motorOne, HIGH);
    digitalWrite(motorOne2, LOW);
    digitalWrite(motorTwo, HIGH);
    digitalWrite(motorTwo2, LOW);
  } else if (Value == 'B') {
    //Robo Pet Run Backward
    digitalWrite(enA, 255);
    digitalWrite(enB, 255);
    digitalWrite(motorOne, LOW);
    digitalWrite(motorOne2, HIGH);
    digitalWrite(motorTwo, LOW);
    digitalWrite(motorTwo2, HIGH);
  } else if (Value == 'L') {
    //Robo Pet Turn Left
    digitalWrite(enA, 255);
    
    digitalWrite(motorOne, LOW);
    digitalWrite(motorOne2, LOW);
    digitalWrite(motorTwo, HIGH);
    digitalWrite(motorTwo2, LOW);
  } else if (Value == 'R') {
    //Robo Pet Turn Right
    
    digitalWrite(enB, 255);
    digitalWrite(motorOne, HIGH);
    digitalWrite(motorOne2, LOW);
    digitalWrite(motorTwo, LOW);
    digitalWrite(motorTwo2, LOW);
  } else if (Value == 'S') {
    //Robo Pet Stop
    digitalWrite(motorOne, LOW);
    digitalWrite(motorOne2, LOW);
    digitalWrite(motorTwo, LOW);
    digitalWrite(motorTwo2, LOW);
  }

  //Read from bluetooth and write to usb serial
  if(bluetooth.available()>= 2 )
  {
    unsigned int servopos = bluetooth.read();
    unsigned int servopos1 = bluetooth.read();
    unsigned int realservo = (servopos1 *256) + servopos;
    Serial.println(realservo);
    if (realservo >= 1000 && realservo <1180) {
      int servo1 = realservo;
      servo1 = map(servo1, 1000, 1180, 0, 180);
      myservo1.write(servo1);
      Serial.println("Servo 1 ON");
      delay(10);
    }
    if (realservo >= 2000 && realservo <2180) {
      int servo2 = realservo;
      servo2 = map(servo2, 2000, 2180, 0, 180);
      myservo2.write(servo2);
      Serial.println("Servo 2 ON");
      delay(10);
    }
    if (realservo >= 3000 && realservo <3180) {
      int servo3 = realservo;
      servo3 = map(servo3, 3000, 3180, 0, 180);
      myservo3.write(servo3);
      Serial.println("Servo 3 ON");
      delay(10);
    }
    
  }
}

I’m using this app: https://play.google.com/store/apps/details?id=com.giumig.apps.bluetoothserialmonitor&hl=en&gl=US

And here are the schematics: schematic

9
  • 2
    Is Serial.println(Value); printing what you expect?
    – timemage
    Sep 15 at 15:24
  • test if everything is wired correctly ... write simple code that tests each motor ... test servo
    – jsotola
    Sep 15 at 15:52
  • @timemage When it worked before, the app would print a series of numbers and then the letters. It only prints command if my Nano is connected to the computer. Sep 16 at 2:27
  • @jsotola I've tested the motors and servos individually and controlled them with potentiometers before moving on to Bluetooth. I've also tested the Bluetooth module with a simple blink command on the app and it works fine. Sep 16 at 2:27
  • 1
    @timemage Now nothing is printed on the Serial monitor even if the Arduino Nano is plugged to the computer. Sep 19 at 13:49

2 Answers 2

1

In short, among whatever other problems you might have, you've wired your TX and RX connections backwards according to your diagram and code. Swap them either in hardware or in software.


According to the documentation:

SoftwareSerial var_name(rxPin, txPin)
SS Operation
RX
TX

You've constructed your SoftwareSerial object as:

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
SS Operation Pin Variable
RX bluetoothTx
TX bluetoothRx

Defined your pin variables to these pins:

int bluetoothTx = 10; // bluetooth tx to 10 pin
int bluetoothRx = 11; // bluetooth rx to 11 pin
SS Operation Pin Variable Arduino Pin
RX bluetoothTx 10
TX bluetoothRx 11

Connected them as:

Serial connections

SS Operation Pin Variable Arduino Pin BT Operation
RX bluetoothTx 10 RX
TX bluetoothRx 11 TX

So, you have them backwards. Connecting their RX (inputs) which is is harmless but not helpful and connecting their TX (outputs) which may damage something.

In case this isn't clear, the RX and TX on the HC-05 are labeled from its perspective and not from the perspective of the Arduino. Except for some things like an old external modem, RX and TX on device are nearly always labeled from that device's perspective.


A variable name like bluetoothTx can be confusing if you're not sure which thing is doing the transmitting. Is this the pin number for transmitting to Bluetooth or the pin that will be connected to the Bluetooth transmitter (in other words, for receiving)?

In some cases I just describe the situation in the variable name:

static const int PIN_SOFTSERIAL_RX_TO_BLUETOOTH_TX = 11;
static const int PIN_SOFTSERIAL_TX_TO_BLUETOOTH_RX = 10;
/// ...
SoftwareSerial bluetooth(
  PIN_SOFTSERIAL_RX_TO_BLUETOOTH_TX,
  PIN_SOFTSERIAL_TX_TO_BLUETOOTH_RX
);

I dunno that I'd go with a name quite that long, but you get the idea.

I'm swapping them in software here, leaving your physical connections as they were in the diagram mostly because I don't feel like redrawing your diagram. Though, if it were my project I would swap the wires (and defined pin numbers above), because the convention is to have SoftwareSerial receive on 10 just like hardware receives on pin 0 and to have SoftwareSerial transmit on 11 just like hardware transmits on pin 1; in order words, keeping the relative order of the pin numbers.

So this is what you have with the above code:

SS Operation Pin Variable Arduino Pin BT Operation
RX PIN_SOFTSERIAL_RX_TO_BLUETOOTH_TX 11 TX
TX PIN_SOFTSERIAL_TX_TO_BLUETOOTH_RX 10 RX

Transmitters connected to receivers. If you get confused build your own table and make sure your variable names make sense.

This has nothing to do with our question really, but: Most Arduino users, new ones anyway, are better off with an Arduino that has connectivity that displaces the need for SoftwareSerial. A minimal change from the Nano for what you're doing is a Micro or Pro Micro board. That board has native USB support which it uses to send "Serial" to the computer. That leaves the one actual hardware serial peripheral on the (Pro) Micro available for you to to connect to HC-05 (or whatever). There are a pile of options, including ones that just do Bluetooth also (without need of HC-05). Anyway, there are lots of ways to not use SoftwareSerial.

0

@timemage

Your answer was part of the solution!

Thank you very much! In the first while loop I switched it from bluetooth.available > 2 to bluetooth.available > 0 and then switched the signs of the TX and RX. Now the robot car is responding and running. However it won't turn left even though both motors work fine.

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.