Hello everyone, I am building a RC controlled combat bot for a school project. I need help with the programming of the project. I am transmitting signals from a transmitter of 2.4 GHz, to a receiver, with this received PWM, I am trying to convert those values into values that can be interpreted by the Arduino Uno rev3 and output the appropriate response to the 2 DC motors that I am using.
Instead when I switch the transmitter on, it immediately gets the motors going, in this case the left motor is motor B, and motor A is the right motor. As soon as I switch it on, motor B goes backwards, and motor A goes forwards. When I change the PulseWidthDIR (which is simply the wheel on the pistol grip to control the direction), the motors go slower, or side one goes faster than the other. Even when i switch the transmitter off it still keeps going.
The throttle button does absolutely nothing. I've tried using the serial monitor to look at if the code is being picked up and it is reflecting what I want or what it is meant to print out, but for some reason the motors are still not in tandem with the code or how I want. The code would clarify my intentions a bit further.
Please let me know how I could improve this, or if there are any faulty parts in the logic, or maybe even any potential mechanical/electronic issues, even though this is a programming section. I am still very much a beginner to Arduino, programming and electronics and keen to learn more! This project is due this coming week monday, the parts have only recently come so I've had to spend lots of time on all aspects of it grinding it away.
[code]
#define speedpin 3 // pin on arduino for interrupt on receiver
#define dirpin 2 // pin on arduino for interrupt on receiver
#define shield_dir_pinA 12
#define shield_dir_pinB 13 // pin for direction
#define pwm_pinA 10 // Need to bend leads of motorshield in pin 3 and make it connect to pin 10 on the uno rev3 by jumper wires
#define pwm_pinB 11 // Controls the speed of the motor
volatile long StartTimeTHROTTLE = 0;
volatile long CurrentTimeTHROTTLE = 0;
volatile long PulsesTHROTTLE = 0;
int PulseWidthTHROTTLE = 0;
volatile long StartTimeDIR = 0;
volatile long CurrentTimeDIR = 0;
volatile long PulsesDIR = 0;
int PulseWidthDIR = 0;
int drive = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(speedpin, INPUT_PULLUP);
pinMode(dirpin, INPUT_PULLUP);
pinMode(shield_dir_pinA, OUTPUT);
pinMode(shield_dir_pinB, OUTPUT);
pinMode(pwm_pinA, OUTPUT);
pinMode(pwm_pinB, OUTPUT);
attachInterrupt(digitalPinToInterrupt(speedpin), PulseTimerTHROTTLE, CHANGE);
attachInterrupt(digitalPinToInterrupt(dirpin), PulseTimerDIR, CHANGE);
}
void loop() {
digitalWrite(shield_dir_pinA, HIGH);
digitalWrite(shield_dir_pinB, HIGH); //defaulted to clockwise direction
if (PulsesTHROTTLE < 2000) {
PulseWidthTHROTTLE = PulsesTHROTTLE;
}
if (PulsesDIR < 2000) {
PulseWidthDIR = PulsesDIR;
}
Serial.print(PulseWidthDIR);
Serial.print(" ");
Serial.println(PulseWidthTHROTTLE);
if (PulseWidthDIR <= 1520 && PulseWidthTHROTTLE <= 1100) {
Serial.println("Control system switched off "); //When switched off it closes loop
return;
}
if (PulseWidthDIR >= 1680 && PulseWidthDIR <= 1724 && PulseWidthTHROTTLE >= 1560 && PulseWidthTHROTTLE <= 1580) {
Serial.println("You are in neutral position "); //When in neutral position, the speed is neutralised to 0, when we initialised analogwrites as 0
return;
}
drive = map(PulseWidthTHROTTLE, 1200, 1800, -100, 100); // PWM conversion to values -100 -> 100
drive = abs(drive) + 155;
if (PulseWidthDIR > 1740 ) // moving right
{
Serial.print("Moving Right ");
digitalWrite(shield_dir_pinA, LOW);
digitalWrite(shield_dir_pinB, HIGH);
}
else if (PulseWidthDIR <= 1650) // moving left
{
Serial.print("Moving Left ");
digitalWrite(shield_dir_pinA, HIGH);
digitalWrite(shield_dir_pinB, LOW);
}
else if (PulseWidthDIR >= 1685 && PulseWidthDIR <= 1724)// No movement of the wheel on remote
{
Serial.print("Centred ");
}
if (PulseWidthTHROTTLE >= 1580) //Thrust going forwards
{
Serial.print("Forwards ");
analogWrite(pwm_pinA, drive);
analogWrite(pwm_pinB, drive);
}
else if (PulseWidthTHROTTLE <= 1560)
{
Serial.print("Backwards ");
digitalWrite(shield_dir_pinA, LOW);
digitalWrite(shield_dir_pinB, LOW);
analogWrite(pwm_pinA, 200);
analogWrite(pwm_pinB, 200);
}
else if (PulseWidthTHROTTLE >= 1560 && PulseWidthTHROTTLE <= 1580)
{
Serial.println("Neutral drive ");
}
}
void PulseTimerTHROTTLE() //PWM for throttle
{
CurrentTimeTHROTTLE = micros();
if (CurrentTimeTHROTTLE > StartTimeTHROTTLE) {
PulsesTHROTTLE = CurrentTimeTHROTTLE - StartTimeTHROTTLE;
StartTimeTHROTTLE = CurrentTimeTHROTTLE;
}
}
void PulseTimerDIR() // PWM for direction
{
CurrentTimeDIR = micros();
if (CurrentTimeDIR > StartTimeDIR) {
PulsesDIR = CurrentTimeDIR - StartTimeDIR;
StartTimeDIR = CurrentTimeDIR;
}
}
Here are the PWM values for each part of the transmitter control.
/* When switched off, need to create a failsaf e:
* PulseWidthTHROTTLE: 1016
* PulseWidthDIR: 1504 - 1500
*
* When switched on and neutral (Buttons not pressed):
* PulseWidthTHROTTLE: 1572, 1568,1576
* PulseWidthDIR: 1708, 1704, 1700, 1696
*
*
* When switched on and throttled forwards
* PulseWidthTHROTTLE: 1744, 1740
*
* When switched on and throttled backwards
* PulseWidthTHROTTLE: 1400, 1396
*
*
*
* When switched on and wheel turned clockwise
* PulseWidthDIR: 1056, 1060
*
* When switched on and wheel turned counter clockwise
* PulseWidthDIR: 1976, 1984, 1972, 1988, 1996
*
*
*/
To clarify further questions of my hardware:
2 DCMotors used: 160 RPM, max current is 200mA, input voltage recommend is 12V, but i am using 9V
Arduino Motor Shield:
Arduino Uno Rev 3: interrupt function only on pins 3 and 2, hence I had to bend the leads of pin 3 on the arduino motor shield, since it uses pwm A or pwm for motor A in pin 3, I connected attached this lead with a jumper wire connecting to pin 10 on the uno rev3, which is itself a pwm pin as well.
ROFUN Receiver: working current is 30mA, 4.8 - 10V, which is being supplied by 5V pin and GND on arduino uno, that which is powered by my cable to laptop. Also a surface receiver. Has six channels. Only channel 1 and 2 being used. Channel 2 is the throttle, channel 1 is the wheel. Has a gyroscope or something i dont think I need that at this stage.
Transmitter is a pistol Grip, RC-X6 ROVAN. Voltage range is 4.8V to 12V. I'm giving it 4 AA batteries for juice.
Also a 9V battery powering the Arduino Motor Shield for the motors. I cut the Vin connection power line to avoid damage to the arduino uno, so that the power is specifically used for the motors.
Wiring
I am also using a brushless esc with a brushless motor, but i am sure that has nothing to do with it, since it wasnt even powered, and it was connected to the receiver directly.
In detail i'll explain the wiring of the arduino and the receiver. GND and 5V into the respective power and GND pins on the receiver for channel 1, channel 1 was connected to pin 2 through male to female connector, as the motor shield would be on top. Channel 2 was bent to fit into a crevice inside the pin 3. This is because pin 3 was being blocked by the leads of the arduino motor shield, which uses it for pwm A, i bent the lead of pin 3 on the motor shield, and connected that to pin 10, using a jumper wire. And the motor wires were correctly screwed in the respective motor terminals.
Thank you so much for your time and your help!!