I have problem with controlling servo via bluetooth (hc06 to hc05). The controller has 2 joystick what controls 4 kinda different servos. Because I have to control these servos individually, I'm sending mapped servo positions like (servo1 = 100-280, servo2 = 200-380 ...) But the problem is, that when I turn joystick's arm to right on X-axel (analoginput 600-1023). That servo, which should move when I turn that joystick right, doesn't do that... But when I turn that same joystick's arm to left it works great....??? And Y-axle is working normally (both up and down works).
Controller code:
//Left Joystick
const int L_Y_pin = A1;
const int L_X_pin = A0;
int L_Y_value = 0;
int L_X_value = 0;
//Right Joystick
const int R_Y_pin = A2;
const int R_X_pin = A3;
int R_Y_value = 0;
int R_X_value = 0;
//Left button
const int L_Button_pin = 13;
int L_Button_state = 0;
//Right button
const int R_Button_pin = A7;
int R_Button_state = 0;
void setup()
{
Serial.begin(9600);
Serial.println("Controller is ready.");
pinMode(13, INPUT);
pinMode(A7, INPUT);
pinMode(A3, INPUT);
pinMode(A2, INPUT);
pinMode(A1, INPUT);
pinMode(A0, INPUT);
}
void loop()
{
//Read buttons
L_Button_state = digitalRead(13);
R_Button_state = analogRead(A7);
//Read Left Joystick
L_Y_value = analogRead(L_Y_pin);
int L_Y_valueMapped = map(L_Y_value, 0, 1023, 320, 500);
L_X_value = analogRead(L_X_pin);
int L_X_valueMapped = map(L_X_value, 0, 1023, 0, 180);
//Read Right Joystick
R_Y_value = analogRead(R_Y_pin);
int R_Y_valueMapped = map(R_Y_value, 0, 1023, 100, 190);
R_X_value = analogRead(R_X_pin);
int R_X_valueMapped = map(R_X_value, 0, 1023, 200, 310);
Serial.write(R_Y_valueMapped);
Serial.write(R_X_valueMapped);
delay(50);
}
Droid code:
#include <Servo.h>
//Servo Configurations
Servo Servo1; //Turn
Servo Servo2; //Head move forward/backward
Servo Servo3; //Head move sideways
int pos1 = 90;
int pos2 = 100;
int pos3 = 100;
//L298N Configuration
int IN1 = 2;
int IN2 = 3;
int IN3 = 4;
int IN4 = 5;
int ENA = 6;
int ENB = 10;
int data = 0;
void setup()
{
//Data rate for the SoftwareSerial port
Serial.begin(9600);
Serial.println("Drive-system is ready.");
//Motor-pins
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
//Define the servo digital pins
Servo1.attach (9);
Servo2.attach (8);
Servo3.attach (7);
Servo1.write(pos1);
Servo2.write(pos2);
Servo3.write(pos3);
}
void loop()
{
if(Serial.available())
{
data = Serial.read();
Serial.println(data);
if(data >= 100 && data <= 190)
{
Servo3.write(data - 35);
delay(10);
}
if(data >= 200 && data <= 310)
{
Servo2.write(data - 160);
delay(10);
}
}
}