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.