Hello everybody.
First of all sorry for my english
I'm new to the forum, and I'm doing a project for an RC car, and am currently working toward the car. But there is working as expected.
I'm using a joystick, arduino uno and servo.
With some research I could write this code:
#include <Servo.h>
Servo servodireccao;
const int joyH = 4;
int joyh = analogRead(joyH);
void setup()
{
servodireccao.attach(3);
Serial.begin(9600);
}
void loop()
{
outputJoystick();
int joyh = analogRead(joyH);
//center
if(joyh >= 410 || joyh <=450)
{
 servodireccao.write(115);Â
}
if(joyh < 410 || joyh > 450)
{
 joyh = map(joyh, 100, 800, 70, 180);
Â
 servodireccao.write(joyh);
 delay(15);
}
}
void outputJoystick()
{
 Serial.print(analogRead(joyh));
  Serial.println ("---");
}
but it has some bugs and do not know what else to do ... so far this code is what is working best. There are 2 problems:
When the wheels are centered without cause stir them up a bit.
Ligueiramente turning left or right ligueiramente has a bug and stir alone
I made a little video that shows the problem:
Anyone know how I can fix the problems? beginning to run out of what to do ..
The joystick appears to have a deadzone in the middle, from 410 to 450. You then map all values from 100 to 800, including those in the deadzone (since the second if is not an else if) to the range from 70 to 180. I don't see why you are doing this.
That your problem is in the mapping function. In addition to printing the joyh value before mapping, you should print it again after the mapping.
so they say:
int joyh = analogRead(A4);
if(joyh < 410 || joyh > 450)
{
 valor = map(joyh, 100, 800, 70, 180);
 int joyh = analogRead(A4);
 servodireccao.write(valor); // servo direction
 delay(15);
}
int joyh = analogRead(A4);
if(joyh < 410 || joyh > 450)
{
 valor = map(joyh, 100, 800, 70, 180);
 int joyh = analogRead(A4);
 servodireccao.write(valor); // servo direction
 delay(15);
}
Why are you creating a local variable in this if block/ The value assigned to it is never used.
It does not make sense to use one range of values in the call to map, for joyh between 0 and 409 AND for joyh between 451and 1023.