App controls motors, not servos

Full description below.

Here is the code:

#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 = 3;
int motorOne2 = 4;
int motorTwo = 5;
int motorTwo2 = 6;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);


//initial motors pin

char command; 
char Value;

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

  if ( Value == 'F') {
    // Robo Pet Run Forward
    digitalWrite(motorOne, HIGH);
    digitalWrite(motorOne2, LOW);
    digitalWrite(motorTwo, HIGH);
    digitalWrite(motorTwo2, LOW);
  } else if (Value == 'B') {
    //Robo Pet Run Backward
    digitalWrite(motorOne, LOW);
    digitalWrite(motorOne2, HIGH);
    digitalWrite(motorTwo, LOW);
    digitalWrite(motorTwo2, HIGH);
  } else if (Value == 'L') {
    //Robo Pet Turn Left
    digitalWrite(motorOne, LOW);
    digitalWrite(motorOne2, LOW);
    digitalWrite(motorTwo, HIGH);
    digitalWrite(motorTwo2, LOW);
  } else if (Value == 'R') {
    //Robo Pet Turn Right
    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 gathered 2 code files from Youtube. One each for the servo and motor and I combined them into one file. I am also using 2 different apps because I could not find one that works for both motor and servo control.

The app used to control the servos:
https://drive.google.com/file/d/1CMS4IN4Hxd922cu4Q6afkjA03YsoZK6r/view

The app to control the motors:

When I try using the app for the motor, there are no errors and the motors work as intended. However, when I switch to using the app to control the servos, they don’t move at all. I tested the bluetooth servo control file and there is nothing wrong. I’m a beginner programmer and don’t know exactly what I am doing wrong. Should I find an app that controls both servos and motors? Is there another way to program the servos and motors?

Let me know if there is any more info you need.

Any tips and help would be very much appreciated thank you.

Neither of the links to the apps work.

None of the motor control pins are OUTPUTs. How does that work?

Having multiple places to read the serial data makes for a fragile reception method

If I were to write something like that I would send a small packet from the app that contained the action to be taken and the amount of movement. I would use something like the Bluetooth Serial Terminal as the app.

I would use techniques from the serial input basics tutorial to receive and parse one data packet that contains the appropriate message.

A message packet from the app could be something like: <F> to move the motors forward or <A,30,90> to move servo 1 to 30° and servo 2 to 90°. Those packets use < and > as start and end markers to frame the packet for more robust reception.

Hello ej_salga
Keep it simple and stupid firstly.
Run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.
Have a nice day and enjoy coding in C++.

1 Like

Get SIMPLE examples working first, then build up progressively.

I don’t know how to convert the messages from Serial input into commands as it is not explained in the forum post. I also don’t understand the use of start and end markers. I understand that it’s used to filter out the rest of the message and only read in between the markers. However, the commands I’ll be giving in the serial input won’t need any filtering.

I decided to ask chatGPT if it had a solution to converting “1 90” into a command for the servo. It came up with a simple solution:

void loop() {
  if (bluetooth.available() > 0) { // checks if data is available to read
    char command = bluetooth.read(); // reads the incoming command
        int servoNum = command - '0'; // convert the command character to a number
        int angle = bluetooth.parseInt(); // reads the second number as the angle

        // checks if the servo number is valid (between 1 and 3)
        if (servoNum >= 1 && servoNum <= 3) {
          // selects the servo based on the servo number and sets the angle
          switch (servoNum) {
            case 1:
              servo1.write(angle);
              break;
            case 2:
              servo2.write(angle);
              break;
            case 3:
              servo3.write(angle);
              break;
          }
          Serial.println("Servo " + String(servoNum) + " set to " + String(angle) + " degrees.");
        }
        else {
          Serial.println("Invalid servo number.");
        }
        break;
    }
  }
}

However, I still don't understand why this code works and my code didn't. Can you recommend a resource that could help me figure it out?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.