Servo function not working

Hi all

Im new to the forums and new to arduino and programming in general, I am studying electronics and thought I would pick up some projects for fun and to hopefully learn a thing or two.

What im trying to make is a little battle bot, my brother is making a similar one, so we can duke it out, it’s a tracked chassis i bought off banggood, it’s going to have a turret, im thinking of putting a bb gun or something on it.

So far I got the track movement and bluetooth control working perfect, but when I try to include the servo things get iffy, my turretControl() is completely unresponsive, it doesn’t seem to do anything, I have rewritten that part a few times, but then it just gets stuck down there and never returns to the main loop()

So, the turretControl() does nothing, but another thing is, when I send it a “w”, “s”, “q”, “e” and the motors for the tracks starts doing what I want them to, the turretServo is moving on it’s own, depending on which input i sent to the motors, it like jerks back and forth, making weird movement, so my motorControl() seems to somehow affect the servo…

What i’d want it to do is make smooth movement either clockwise or counter clockwise, depending on the input, I chose the values “a” and “d” for turret movement…

I’ve attached my code, hopefully someone could help me out or if there’s better way to do things im also open to them, im new and will take anything you can give me!! :slight_smile:

(I can upload to pastebin or something if that’s easier for you guys, just let me know)

Best Regards

/***********************************************************************************/
//#include <Wire.h>
#include <Servo.h>
#define motorRightAlpha 5
#define motorRightBravo 3
#define motorLeftAlpha 6 
#define motorLeftBravo 11
Servo turretServo;
/***********************************************************************************/
char btJunk;
String btRecieved = "";
String btCommand = "";
int directionVal;
int directionValOld;
int turretDirection;
int turretDirectionOld;
int turretServoPos;
int turretServoPosOld;
/***********************************************************************************/
void setup() {             /**** RUN ONCE ****/
  Serial.begin(9600);     // Default baud rate of BT module, reprogram module later for higher baud rates 
  pinMode(motorLeftAlpha, OUTPUT);
  pinMode(motorLeftBravo, OUTPUT);
  pinMode(motorRightAlpha, OUTPUT);
  pinMode(motorRightBravo, OUTPUT);
  turretServo.attach(19);
}
/***********************************************************************************/
void loop() {            /**** MAIN LOOP ****/
  motorControl();
  bluetoothLoop();
  turretControl();
  //Serial.println(btCommand);
  //Serial.println(pos);
 }
/***********************************************************************************/
                         /**** MOTOR CONTROL ****/
void motorControl(){
  btCommandUpdate();    //See BT section
  if (btCommand == "x"){
    directionVal = 0;    //Stop
  }
  if (btCommand == "w"){
    directionVal = 1;    //Forward
  }
  if (btCommand == "s"){ 
    directionVal = 2;    //Reverse
  }
  if (btCommand == "q"){ 
    directionVal = 3;    //Left
  }
  if (btCommand == "e"){ 
    directionVal = 4;    //Right
  }
  if (directionValOld != directionVal){
    delay(100);
    switch(directionVal){
      case 0:{  //STOP
        analogWrite(motorLeftAlpha, 0);
        analogWrite(motorLeftBravo, 0);
        analogWrite(motorRightAlpha, 0);
        analogWrite(motorRightBravo, 0);
        directionValOld = directionVal;
      }
    break;
    case 1:{   //FORWARD
        analogWrite(motorLeftAlpha, 255);
        analogWrite(motorLeftBravo, 0);
        analogWrite(motorRightAlpha, 0);
        analogWrite(motorRightBravo, 255);
        directionValOld = directionVal;
    }
    break;
    case 2:{   //REVERSE
        analogWrite(motorLeftAlpha, 0);
        analogWrite(motorLeftBravo, 220);
        analogWrite(motorRightAlpha, 200);
        analogWrite(motorRightBravo, 0);
        directionValOld = directionVal;
    }
    break;
    case 3:{   //LEFT
        analogWrite(motorLeftAlpha, 0);
        analogWrite(motorLeftBravo, 200);
        analogWrite(motorRightAlpha, 0);
        analogWrite(motorRightBravo, 180);
        directionValOld = directionVal;      
    }
    break;
    case 4:{   //RIGHT
        analogWrite(motorLeftAlpha, 200);
        analogWrite(motorLeftBravo, 0);
        analogWrite(motorRightAlpha, 180);
        analogWrite(motorRightBravo, 0);
        directionValOld = directionVal;
    }
     break;  
    } 
  }
}                         
/***********************************************************************************/
                         /**** BLUETOOTH ****/
void bluetoothLoop(){
    if (Serial.available()){
     while (Serial.available()) 
 {
     char inChar = (char)Serial.read();  //Read the input
     btRecieved += inChar;               //make a string of the characters being recieved on serial input
 }
   //Serial.println(inputString);
    while (Serial.available() > 0)
    { btJunk = Serial.read(); }          //Clear the serial buffer     
 }
}
void btCommandUpdate(){
   btCommand = btRecieved;
   btRecieved = "";
   }
/***********************************************************************************/
                         /**** LIGHTS & LASER CONTROL ****/

/***********************************************************************************/
                         /**** TURRET CONTROL ****/
void turretControl(){
//btCommandUpdate();
   if (btCommand == "a"){
    turretDirection = 1; // LEFT
   }
   if (btCommand == "d"){
    turretDirection = 2; // RIGHT
   }
   if (turretDirectionOld != turretDirection){
    switch (turretDirection){
      case 1:{
       for (turretServoPos = 0; turretServoPos <= 170; turretServoPos++){
         turretServo.write(turretServoPos);
         turretDirectionOld = turretDirection;
         delay(10);
       }
      }
       break;
       case 2:{
        for (turretServoPos = 170; turretServoPos >= 10; turretServoPos--){
          turretServo.write(turretServoPos);
          turretDirectionOld = turretDirection;
          delay(10); 
        }
       } 
        break;
    }
   }
}
/***********************************************************************************/

BattleBot_R02.ino (4.66 KB)

Please post your code.

Wops..

Yep, there, it's added :slight_smile:

Have you got all the servo grounds connected to the Arduino ground?
Do you have a separate supply for the servos?
I'm unclear why you're using String for single character commands.
You really need to get rid of the calls to delay()

Arduino and servo grounds are not connected.

Servo and motors have their own supply, currently a lab PSU.

Should these String’s be replaced with char?

Im not sure what the best way to replace delay() is

Arduino and servo grounds are not connected.

Why not?

I dont know, I felt it was "dangerous" to connect the lab PSU to the arduino and in turn my computer..

Im about to get an eye opener I think :slight_smile:

Well.. derp.

Connected the grounds and all the weird servo movement stopped, not sure I understand why, it worked just fine in the Servo -> Sweep sketch.

Code still doesn't work however.

Tried a different approach to the turretControl() function.

But no luck.

/***********************************************************************************/
//#include <Wire.h>
#include <Servo.h>
#define motorRightAlpha 5
#define motorRightBravo 3
#define motorLeftAlpha 6 
#define motorLeftBravo 11
Servo turretServo;
/***********************************************************************************/
char btJunk;
String btRecieved = "";
String btCommand = "";
int directionVal;
int directionValOld;
int turretDirection;
int turretDirectionOld;
int turretServoPos;
int turretServoPosOld;
/***********************************************************************************/
void setup() {             /**** RUN ONCE ****/
  Serial.begin(9600);     // Default baud rate of BT module, reprogram module later for higher baud rates 
  pinMode(motorLeftAlpha, OUTPUT);
  pinMode(motorLeftBravo, OUTPUT);
  pinMode(motorRightAlpha, OUTPUT);
  pinMode(motorRightBravo, OUTPUT);
  turretServo.attach(19);
}
/***********************************************************************************/
void loop() {            /**** MAIN LOOP ****/
  bluetoothLoop();
  btCommandUpdate();
  motorControl();
  turretControl();
  //  turretServo.write(170);
  //  delay(1000);
  //  turretServo.write(10);
  //  delay(1000);
  //Serial.println(btCommand);
  //Serial.println(pos);
 }
/***********************************************************************************/
                         /**** MOTOR CONTROL ****/
void motorControl(){
  if (btCommand == "x"){
    directionVal = 0;    //Stop
  }
  if (btCommand == "w"){
    directionVal = 1;    //Forward
  }
  if (btCommand == "s"){ 
    directionVal = 2;    //Reverse
  }
  if (btCommand == "q"){ 
    directionVal = 3;    //Left
  }
  if (btCommand == "e"){ 
    directionVal = 4;    //Right
  }
  if (directionValOld != directionVal){
    delay(100);
    switch(directionVal){
      case 0:{  //STOP
        analogWrite(motorLeftAlpha, 0);
        analogWrite(motorLeftBravo, 0);
        analogWrite(motorRightAlpha, 0);
        analogWrite(motorRightBravo, 0);
        directionValOld = directionVal;
      }
    break;
    case 1:{   //FORWARD
        analogWrite(motorLeftAlpha, 255);
        analogWrite(motorLeftBravo, 0);
        analogWrite(motorRightAlpha, 0);
        analogWrite(motorRightBravo, 255);
        directionValOld = directionVal;
    }
    break;
    case 2:{   //REVERSE
        analogWrite(motorLeftAlpha, 0);
        analogWrite(motorLeftBravo, 220);
        analogWrite(motorRightAlpha, 200);
        analogWrite(motorRightBravo, 0);
        directionValOld = directionVal;
    }
    break;
    case 3:{   //LEFT
        analogWrite(motorLeftAlpha, 0);
        analogWrite(motorLeftBravo, 200);
        analogWrite(motorRightAlpha, 0);
        analogWrite(motorRightBravo, 180);
        directionValOld = directionVal;      
    }
    break;
    case 4:{   //RIGHT
        analogWrite(motorLeftAlpha, 200);
        analogWrite(motorLeftBravo, 0);
        analogWrite(motorRightAlpha, 180);
        analogWrite(motorRightBravo, 0);
        directionValOld = directionVal;
    }
     break;  
    } 
  }
}                         
/***********************************************************************************/
                         /**** BLUETOOTH ****/
void bluetoothLoop(){
    if (Serial.available()){
     while (Serial.available()) 
 {
     char inChar = (char)Serial.read();  //Read the input
     btRecieved += inChar;               //make a string of the characters being recieved on serial input
 }
   //Serial.println(inputString);
    while (Serial.available() > 0)
    { btJunk = Serial.read(); }          //Clear the serial buffer     
 }
}
void btCommandUpdate(){
   btCommand = btRecieved;
   btRecieved = "";
   }
/***********************************************************************************/
                         /**** LIGHTS & LASER CONTROL ****/

/***********************************************************************************/
                         /**** TURRET CONTROL ****/
void turretControl(){
   if (btCommand == "a"){
    turretDirection = 1; // LEFT
   }
   if (btCommand == "d"){
    turretDirection = 2; // RIGHT
   }
   if (turretDirectionOld != turretDirection){
    switch (turretDirection){
      case 1:{
       if (turretServoPos >= 0 &&  turretServoPos <= 180){
        ++turretServoPos;
       }
        turretServo.write(turretServoPos);
        turretDirectionOld = turretDirection;
       }
      break;
      case 2:{
      if (turretServoPos <= 180 &&  turretServoPos >= 0){
        --turretServoPos;
      }
        turretServo.write(turretServoPos);
        turretDirectionOld = turretDirection;
      }
      break;
      }
     }
    } 
/***********************************************************************************/

But no luck.

All AVR processors are immunised against luck before leaving the factory.

Perhaps you could share your observations and expectations