Servo Steering control with dabble joystick (Bluetooth)

Hi all

I am trying to build a RC Car that uses a servo 9G motor for the steering.

I am using the dabble Bluetooth app to control the car. everything works better than I expected accept steering left.

the left position on the x axis is 0.00 to -7.00and i do not know how to read and write the float values to the servo

can some one please point me in right direction where i can learn to how to map float values to servo position.

thank you in advance.

here is a sample of my code where I'm stuck.

#define CUSTOM_SETTINGS
#define INCLUDE_GAMEPAD_MODULE
#include <Dabble.h>
#include <Servo.h>
Servo myservo;

//Motor
int r_en = 9;
int l_en = 12;
int Back = 10; //Back
int Forward = 11; //Forward



void setup() {

  myservo.attach(6);
  Serial.begin(9600);
  Dabble.begin(9600); 

  pinMode(r_en, OUTPUT);
  pinMode(l_en, OUTPUT);
  pinMode(Back, OUTPUT);
  pinMode(Forward, OUTPUT);
  pinMode(6,OUTPUT);//servo
}

void loop() {

  Dabble.processInput();

  Serial.print('\t');
  float c = GamePad.getXaxisData();
  Serial.print("x_axis: ");
  Serial.print(c);
  Serial.print('\t');

  
  if (GamePad.getXaxisData()>=0){
    int value = GamePad.getXaxisData();
    if (value >=0){
      Serial.println(value);
      int servo_p = map(value,0,7,90,120); // last min and max is servo position in degrees
      myservo.write(servo_p);
    }

  }
  if (GamePad.getXaxisData()>=-0){
    int value = GamePad.getXaxisData();
    if (value <=-0){
      Serial.println(value);
      int servo_p = map(value,-0,-7,90,60); // last min and max is servo position in degrees
      myservo.write(servo_p);
    }

  }
}

Shouldn't that be:

if (GamePad.getXaxisData()<0){

???

Also, there is no reason to use -0. You have already assigned GamePad.getXaxisData() to a variable. There is no reason to call it over and over again just use the variable.

Hi Todd

Thanks for your input it got me thinking, I did some research and have got it right finally.

void loop() {

 Dabble.processInput();
 float Joyx = GamePad.getXaxisData();
 Serial.print("x_axis: ");
 Serial.println(Joyx);
 Serial.print('\t');

  
  if (Joyx >= 0.00){
    float value = Joyx;
    if (value >=0.00){
      Serial.println(value);
      float servo_p = mapfloat(value,0.00,7.00,90,120); // last min and max is servo position in degrees
      myservo.write(servo_p);
    }

 
  }
    if (Joyx <= 0.00){
    float value1 = Joyx;
    if (value1 <=0.00){
      Serial.println(value1);
      float servo_p1 = mapfloat(value1,-7.00,0.00,60,90); // last min and max is servo position in degrees
      myservo.write(servo_p1);
    }

 
  }
}


float mapfloat(float x, float in_min, float in_max, long out_min, long out_max)
{
 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Glad you got it working! This:

  if (Joyx >= 0.00) {
    float value = Joyx;
    if (value >= 0.00) {
      Serial.println(value);
      float servo_p = mapfloat(value, 0.00, 7.00, 90, 120); // last min and max is servo position in degrees
      myservo.write(servo_p);
    }
  }

  if (Joyx <= 0.00) {
    float value1 = Joyx;
    if (value1 <= 0.00) {
      Serial.println(value1);
      float servo_p1 = mapfloat(value1, -7.00, 0.00, 60, 90); // last min and max is servo position in degrees
      myservo.write(servo_p1);
    }
  }

can be replaced by:

  float servo_p = mapfloat(Joyx, -7.00, 7.00, 60, 120); 
  myservo.write(servo_p);
1 Like

Epic it worked like a charm. you did me a solid thanks.

1 Like

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