Need help with my RC CAR project

Good Afternoon All.
I su** at coding and i want to build an RC car with sensor that when it sees object in front it will stop but you can still drive backwards and drive away from object and continue forward as normal. I`m making this RC Car for my school project, but i can not get my DC motor work correctly.
Can someone help or point me the right direction.

And would like to know how to introduce a sensor in the code. I was thinking to put like Sonar sensor.
I got my servo working just fine with the same code and instructions i found.

But when I try to control motor its wants to run but at the same time does not, time to time if im slowly pushing to accelerate it starts to run but then again looses and its hard to understand whats going on. Times it is juggling like there is some problem in to the code. Direction of motor changes also and when trying backwards happens the same. Sometimes when the acceleration trigger is in middle position the motor starts to spin to the max, to stop it im just pressing to accelerate.

Using:
Arduino uno

H-Bridge L298N

Transmitter i`m using:
GoolRC TG3 2.4GHz 3CH Digital Radio Remote Control Transmitter with Receiver for RC Car Boat

Motor I`m using
maverick mm-22 electric motor

Is it difference that i`m using different motor/transmitter because everything else seems to be working fine.

Code I`m using as in video described.

#include <Servo.h>
Servo servo1;
#define rc1 9 // receiver chanel 1 on pin 9
#define rc2 10 // receiver chanel 2 on pin 10
#define spd 5 // PWM pin on L298N
#define pin1 4 // in 1 pin on L298N to pin 4 on ARDUINO
#define pin2 3 // in 2 pin on L298N to pin 3 on ARDUINO
int ch1; // value of chanel 1 of the receiver
int ch2; // value of chanel 2 of the receiver
int val1; // servo position
int val2; // motor speed

void setup() {
servo1.attach(11);
pinMode (rc1, INPUT);
pinMode (rc2, INPUT);
pinMode (spd, OUTPUT);
pinMode (pin1, OUTPUT);
pinMode (pin2, OUTPUT);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
Serial.begin(9600); // for testing
}

void loop() {
ch1 = pulseIn(rc1, HIGH);
ch2 = pulseIn(rc2, HIGH);
Serial.print(“ch1 “);
Serial.println(ch1);
Serial.print(“ch2 “);
Serial.println(ch2);
val1 = map(ch2, 1100, 1900, 10, 170);
servo1.write(val1);

if (ch1 < 1450)
{
val2 = map(ch1, 1450, 1900, 255, 25);
analogWrite(spd, val2);
digitalWrite(pin2, LOW);
digitalWrite(pin1, HIGH);
}
else if (ch1 > 1550)
{
val2 = map(ch1, 1550, 1100, 255, 25);
analogWrite(spd, val2);
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
}
else if (ch1 == 0)
{
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
}

delay(100);
}

Would like some support on this.

THX ahead.

Good Afternoon All.
I su** at coding and i want to build an RC car with sensor that when it sees object in front it will stop but you can still drive backwards and drive away from object and continue forward as normal. I`m making this RC Car for my school project, but i can not get my DC motor work correctly.
Can someone help or point me the right direction.

And would like to know how to introduce a sensor in the code. I was thinking to put like Sonar sensor.
I got my servo working just fine with the same code and instructions i found.

But when I try to control motor its wants to run but at the same time does not, time to time if im slowly pushing to accelerate it starts to run but then again looses and its hard to understand whats going on. Times it is juggling like there is some problem in to the code. Direction of motor changes also and when trying backwards happens the same. Sometimes when the acceleration trigger is in middle position the motor starts to spin to the max, to stop it im just pressing to accelerate.

Using:
Arduino uno

H-Bridge L298N

Transmitter i`m using:
GoolRC TG3 2.4GHz 3CH Digital Radio Remote Control Transmitter with Receiver for RC Car Boat

Motor I`m using
maverick mm-22 electric motor

Is it difference that i`m using different motor/transmitter because everything else seems to be working fine.

Code I`m using as in video described.

#include <Servo.h>
Servo servo1;
#define rc1 9 // receiver chanel 1 on pin 9
#define rc2 10 // receiver chanel 2 on pin 10
#define spd 5 // PWM pin on L298N
#define pin1 4 // in 1 pin on L298N to pin 4 on ARDUINO
#define pin2 3 // in 2 pin on L298N to pin 3 on ARDUINO
int ch1; // value of chanel 1 of the receiver
int ch2; // value of chanel 2 of the receiver
int val1; // servo position
int val2; // motor speed

void setup() {
servo1.attach(11);
pinMode (rc1, INPUT);
pinMode (rc2, INPUT);
pinMode (spd, OUTPUT);
pinMode (pin1, OUTPUT);
pinMode (pin2, OUTPUT);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
Serial.begin(9600); // for testing
}

void loop() {
ch1 = pulseIn(rc1, HIGH);
ch2 = pulseIn(rc2, HIGH);
Serial.print("ch1 ");
Serial.println(ch1);
Serial.print("ch2 ");
Serial.println(ch2);
val1 = map(ch2, 1100, 1900, 10, 170);
servo1.write(val1);

if (ch1 < 1450)
{
val2 = map(ch1, 1450, 1900, 255, 25);
analogWrite(spd, val2);
digitalWrite(pin2, LOW);
digitalWrite(pin1, HIGH);
}
else if (ch1 > 1550)
{
val2 = map(ch1, 1550, 1100, 255, 25);
analogWrite(spd, val2);
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
}
else if (ch1 == 0)
{
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
}

delay(100);
}

Would like some support on this.

THX ahead.

if (ch1 < 1450)
{
val2 = map(ch1, 1450, 1900, 255, 25);
}
else if (ch1 > 1550)
{
val2 = map(ch1, 1550, 1100, 255, 25);

Looks to me like you have your mapped ranges wrong. If ‘ch1’ is less than 1450 you are expecting it to be between 1450 and 1900?!? If ‘ch1’ is greater than 1550 upi are expecting it to be between 1550 and 1100?!?

Please provide links to the actual components you are using. E.g. there are loads of different Maverick motors with 22 in the name. Most of them are 540 size, intended to run with gearing and will take about 15-20A. An L293N can provide about a 1/10th of that maximum.

Your map()s are also very odd. What's the point in checking that a value is LESS THAN than 1450 and then try to map a range from 1450 to 1900? None of that range is less than 1450.

Steve

Cross post reported - https://forum.arduino.cc/index.php?topic=582350.0

Steve

Hello Steve. Yes sorry this is mm-22 540 size, so seems that this is not working do to motor being wrong, i`m starting to think.

About the code I tried to change values up down but nothing seems to be working properly, for instead couple of times managed to get it working with this code but then again it started to jerk the last direction which way i was trying to move it and not responding to my transmitter.

Janis

Sorry I did not know that i can not post it more than in one area. :(

Threads merged.

Are you trying to make your own motor controller?! This is insanity. Just use the original one that the car came with. RC car motor controllers act exactly like servos. Just put your arduino betwen the throttle pin on your receiver and the control input of your RC motor controller. Read the pulses coming in, if you're going to hit something, take action. Otherwise just pass the signals through. And your done.

-jim lee

Hello Jim Lee.

Yes i was trying to make the RC car from the scratch buying all the parts separately, but as i start to find out that looks like easiest way would be to buy simple RC car and modify it with Arduino as there is lots more support online, then for the my idea. And it seems to sound so easy, but as my coding really not the best will need to do extra research. So far thanks guys for the ideas will try to buy one and modify.

Reg.

Janis

I've used the arduino controlled RC Car thing for a few high-scool kids. This is because its easy enough for a beginner to grasp how to make it work. Once the wiring is done ,its only about30 minutes to get the going enoug to cause excitement and damage. :)

After that you realize that the same code can control a quadcopter..

have fun!

-jim lee