toward RC CAR

Hello everybody. :slight_smile:
First of all sorry for my english :expressionless:
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 ..

ty

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.

Hello Paul

not quite understand it with the second if...

70,180 is the value of the angle of the servo value so far this value is what works best, since several methyl because you think it will be another?

I did some testing with the serial monitor that liked your opinion ... the wheels have focused on this potentiometer values??:

418---
433---
428---
420---
436---
418---
430---
424---
425---
435---
419---
434---
418---
432---
431---
421---
436---
418---
429---
426---
426---
433---
418---
433---
417---
431---
431---
421---
436---
418---
429---
426---
424---
434---
419---
432---
419---
430---
431---
420---
436---
418---

Everything turned left :

3---
19---
44---
32---
13---
0---
33---
35---
30---
10---
0---
39---
22---
19---
2---
20---
40---
18---
13---
0---
33---
37---
22---
10---
0---
41---
22---
19---
2---
18---
43---
32---
12---
0---
33---
36---
29---
10---
0---
40---
26---
22---

Everything turned right :

709---
735---
749---
726---
791---
748---
763---
749---
720---
703---
710---
750---
726---
712---
705---
719---
752---
719---
713---
708---
734---
749---
725---
792---
749---
761---
749---
720---
793---
754---
768---
738---
713---
706---
720---
753---
720---
710---
706---
732---
749---
725---

turned a little to the right and when they appear is the biggest bugs:

445---
445---
447---
449---
427---
432---
435---
449---
417---
431---
434---
438---
441---
425---
433---
437---
441---
455---
433---
437---
439---
434---
435---
473---
518---
490---
460---
450---
444---
441---
434---
492---
443---
437---
436---
438---
451---
438---
444---
447---
448---
498---

same problem with bugs but turned a little left:

303---
304---
312---
286---
286---
304---
314---
315---
287---
277---
300---
292---
310---
322---
280---
263---
327---
306---
286---
301---
302---
311---
289---
284---
303---
312---
315---
312---
290---
297---
287---
308---
323---
280---
265---
326---
308---
289---
301---
300---
310---
290---

What conclusions can be drawn from here?

What conclusions can be drawn from here?

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.

//center
if(joyh >= 410 || joyh <=450)
{
  servodireccao.write(115);  
}

This should be:

//center
if(joyh >= 410 && joyh <=450)
{
  servodireccao.write(115);  
}

The former will be true for ALL values of joyh, the latter will only be true when joyh is between 410 and 450.

PaulS:

What conclusions can be drawn from here?

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); 
}

jraskell:

//center

if(joyh >= 410 || joyh <=450)
{
  servodireccao.write(115); 
}




This should be:



//center
if(joyh >= 410 && joyh <=450)
{
  servodireccao.write(115); 
}




The former will be true for ALL values of joyh, the latter will only be true when joyh is between 410 and 450.

I've done this change but the problem stays the same

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.