I have a project I'm working on. I am taking an existing RC car, and going to drive it with an Arduino + Motor Shield. I'm not particularly savvy wiring and such, however, I do know the code.
The following is a function I have created to attempt a turn depending on the boolean value sent into it.
I then call that function with two decisions: A left turn (which is passes true) and a right turn (which passes false).
My confusion is if I call left turn, when it is supposed to correct to the right side, it proceeds to the left. When rightTurn() is called, it proceeds as desired.
When debugging, the values are good, (left: dir = 1, and !dir = 0, and vice versa)
EDIT: when observing the Motor shield, I've noticed that the signal LED doesn't turn off when leftTurn is inverted. (the !dir when True is passed)
Right, I understand that... the delay is there so it holds (in this case for 2 seconds) so the car can have some time to straighten out. That comment was there for my sake/sanity.
Thank you for taking the time to look at my code. To answer the question, I wasn't expecting to have anyone look at my code. However, I asked this question because I am legitimately at a loss why this is happening.
Though I may have an idea. Because the Motor is running, do you think there would be a bit of back charge, or would the Shield prevent that? (I had this idea while trying to sleep)
I don't have the circuit schematic. I'm more of a software engineer than an electrical engineer.
But I'm happy to share my code here.
/*******************************************
* Defining Constants
*
********************************************/
#define ACCELBRAKE 8
#define TURNBRAKE 9
#define ACCELDIR 13
#define TURNDIR 12
#define SPEED 11
#define DIRSPEED 3
#include <NewPing.h>
// NewPing sonar(1/*trigger*/,2/*echo*/,200);
void setup()
{
Serial.begin(9600);
unsigned int uS = 0;
/*******************************
* Wait until object is removed
*
********************************/
/* while(uS < 100)
{
Serial.print(uS);
Serial.println( "cm" );
delay(1000);
uS = sonar.ping_cm();
}
Serial.println("Out!");*/
digitalWrite(ACCELBRAKE, LOW);
}
/*****************************************************************
* if dir = true, turn left
* else dir = false, turn right
*
******************************************************************/
void turn (boolean dir)
{
digitalWrite(TURNBRAKE, LOW); // allow movement
analogWrite(DIRSPEED,255);
digitalWrite(TURNDIR, dir); // actual turn (TODO: TEST MORE)
delay(1500);
Serial.print("dir = ");
Serial.println(dir);
digitalWrite(TURNBRAKE,HIGH);
delay(1000);
digitalWrite(TURNBRAKE,LOW);
digitalWrite(TURNDIR, !dir); // correction
Serial.print("!dir = ");
Serial.println(!dir);
delay (2000); // Straighten
digitalWrite(TURNBRAKE, HIGH); // stop
}
void leftTurn()
{
turn(true);
}
void rightTurn()
{
turn(false);
}
/******************************************************************
* corrections - adjusting course to avoid obsticals
* if dir = true, correct to the left
* else dir = false, correct to the right
*
* Example Left correction
* Process: Turn Left
* go little
* Turn Right
* Straight
* Turn Right
* go little
* Turn Left
*
*******************************************************************/
void correction (boolean dir)
{
digitalWrite(TURNBRAKE, LOW);
digitalWrite(TURNDIR, dir); // adjust for correction
analogWrite(DIRSPEED,200);
delay(150);
digitalWrite(TURNDIR, !dir); // straighten
delay(150);
analogWrite(DIRSPEED,0);
digitalWrite(TURNBRAKE,HIGH);
}
void leftCorrection()
{
correction(true);
}
void rightCorrection()
{
correction(false);
}
/*****************************************************
* if dir = true; go forward
* else dir = false; go backward
*
* variable speed?
*
******************************************************/
void go(boolean dir, int power = 255)
{
digitalWrite(ACCELBRAKE, LOW); // disengage brake
digitalWrite(ACCELDIR, dir);
analogWrite(SPEED, power);
delay(2000);
digitalWrite(ACCELBRAKE, HIGH); // engage brake
}
void loop()
{
Serial.println("Left");
leftTurn();
delay(5000);
Serial.println("Right");
rightTurn();
delay (5000);
}
Right now, there is no sensor working. I haven't fully implemented straight, but it seems a simple fix as turning off the brake and sending the proper signal.
I like to test one thing out and figure out the why a small thing is failing rather than find out in the finished product why it's failing.
My confusion is if I call left turn, when it is supposed to correct to the right side, it proceeds to the left. When rightTurn() is called, it proceeds as desired.
I don't entirely understand this statement.
For a right turn, explain exactly what happens. Do NOT use terms like "correct". That does not mean anything to anyone but you who can see what is happening.
For a left turn, explain exactly what happens. Do NOT use terms like "correct". That does not mean anything to anyone but you who can see what is happening.
The idea that I was intending was when I turn a corner, I have to turn to the left. I then will have to cause the car to straighten out by causing the motor to turn to the right. Kind of like how you drive a car. When you make a turn, you have to turn in the opposite direction to straighten the vehicle out.
When I call my left turn function (seen above) it veers to the left, but then after the delay, it continues turning left (even though the digitalWrite signal is the opposite of what it should be).
When I call my right turn function (seen above), it veers to the right, and after the delay, it turns left (because the digitalWrite signal has changed) and it corrects the direction as intended.
I think that it is now time to talk about the hardware. You said "an existing RC car". The 4 of them I have steer using a servo. Servos are not driven the way you are driving whatever is steering your car.
Currently, just a motor and a gear box is what is steering this thing. There were some extra components that I didn't know what they were, but I will have pictures.
(I bought 2 but disassembled one and couldn't put it back together properly.)
The existing RC car I am using is an XMOD from Radio Shack. This is the steering portion.
The motor is a simple DC motor.
There is an electrical doodad (which I don't know what it is)
I would say you have, not a simple motor, it is a servo unit, that is how you know where the steering wheels are pointing.
You are controlling a servo not a simple motor.
Taking it apart, as you have done in one of the pictures, you may have got the servo motor and pot out of sync.
PaulS:
What do you have the motor connected to? From the names, it appears that you have some kind of motor shield.
But, I can't figure out why you have 6 names for controlling the speed and direction of one motor.
I have another motor for moving forwards and backwards, just that current sketch doesn't use it. I was testing the turning. Rather than figure out lots of broken things, I'll make sure one thing is perfect, then move forward from there. (No pun intended)
Tom,
Thanks for that! I really appreciate it! Now I have to figure out how to hook up the servo. Off to Google I go!
I have another motor for moving forwards and backwards, just that current sketch doesn't use it. I was testing the turning.
But, your turning function uses 3 of the names, TURNBRAKE, DIRSPEED, and TURNDIR. I guess I would expect that you are using a motor shield, and that you would be using the TURN side to control the brake, speed, and direction, so I'd be less confused by names like TurnBrake, TurnSpeed, and TurnDir. For turning, I really can't see the need to brake the motor. It should never get to the speed where braking is necessary.