The car was going; forwards, backwards and at two speeds.
The code was an extension of a tutorial and needed to be rewritten for easier development.
The next step was experimenting with turns, and using LEDs for 2 x reverse lights,
2 x brake lights, and 4 x indicators.
The turns were hugely successful; stopping the wheels on one side made it spin on a sixpence. The code is below.
/*
4WD Car chassis.
Paul Goodliffe, May 2014
*/
//L293 Pin assignments
const int FULL = 255;
const int SLOW = 0;
//Motor one
int m1_enable = 3; // Must be PWM for speed control
int m1_forward = 2;
int m1_backward = 4;
//Motor two
int m2_enable = 5; // Must be PWM for speed control
int m2_forward = 7;
int m2_backward = 6;
//Motor 3
int m3_enable = 10; // Must be PWM for speed control
int m3_forward = 8;
int m3_backward = 9;
//Motor 4
int m4_enable = 13; // Must be PWM for speed control
int m4_forward = 12;
int m4_backward = 11;
void setup(){
pinMode(m1_enable, OUTPUT); //Set the three pins for bridge 1 & 2 to outputs
pinMode(m1_forward, OUTPUT);
pinMode(m1_backward, OUTPUT);
pinMode(m2_enable, OUTPUT); //Set the three pins for 3 & 4 to outputs
pinMode(m2_forward, OUTPUT);
pinMode(m2_backward, OUTPUT);
pinMode(m3_enable, OUTPUT); //Set the three pins for bridge 1 & 2 to outputs
pinMode(m3_forward, OUTPUT);
pinMode(m3_backward, OUTPUT);
pinMode(m4_enable, OUTPUT); //Set the three pins for 3 & 4 to outputs
pinMode(m4_forward, OUTPUT);
pinMode(m4_backward, OUTPUT);
}
void loop()
{
setMyspeed(FULL);
goForwards();
delay(1000);
goBackwards();
delay(1000);
turnLeft(); // WOW ! Turns on a ninepence !!
goForwards();
delay(1000); // 1 second of max turn is about 180 deg :-) )
turnRight();
goBackwards();
delay(1000);
turnLeft();
goForwards();
delay(1000);
}
void turnLeft()
{
brakeAll();
setLeftSpeed(SLOW);
setRightSpeed(FULL);
}
void turnRight()
{
brakeAll();
setLeftSpeed(FULL);
setRightSpeed(SLOW);
}
void setLeftSpeed(int newSpeed)
{ // Motors : Front is 2 and 1. Rear is 3 and 4. Left is 2 and 3. Right is 1 and 4.
analogWrite(m2_enable, newSpeed);
analogWrite(m3_enable, newSpeed);
}
void setRightSpeed(int newSpeed)
{ // Motors : Front is 2 and 1. Rear is 3 and 4. Left is 2 and 3. Right is 1 and 4.
analogWrite(m1_enable, newSpeed);
analogWrite(m4_enable, newSpeed);
}
////// Motor One Routines ////////////
void goForwards()
{
brakeAll();
digitalWrite(m1_forward, LOW);
digitalWrite(m1_backward, HIGH);
digitalWrite(m2_forward, LOW);
digitalWrite(m2_backward, HIGH);
digitalWrite(m3_forward, LOW);
digitalWrite(m3_backward, HIGH);
digitalWrite(m4_forward, LOW);
digitalWrite(m4_backward, HIGH);
}
void goBackwards()
{
brakeAll();
digitalWrite(m1_forward, HIGH);
digitalWrite(m1_backward, LOW);
digitalWrite(m2_forward, HIGH);
digitalWrite(m2_backward, LOW);
digitalWrite(m3_forward, HIGH);
digitalWrite(m3_backward, LOW);
digitalWrite(m4_forward, HIGH);
digitalWrite(m4_backward, LOW);
}
void setMyspeed(int newSpeed)
{
analogWrite(m1_enable, newSpeed);
analogWrite(m2_enable, newSpeed);
analogWrite(m3_enable, newSpeed);
analogWrite(m4_enable, newSpeed);
}
void brakeAll()
{
digitalWrite(m1_forward, LOW);
digitalWrite(m1_backward, LOW);
digitalWrite(m2_forward, LOW);
digitalWrite(m2_backward, LOW);
digitalWrite(m3_forward, LOW);
digitalWrite(m3_backward, LOW);
digitalWrite(m4_forward, LOW);
digitalWrite(m4_backward, LOW);
delay(500); // Give the motors time to react
}
|
|
My 4WD car chassis kit. The chassis has the protecting layer on at the mo. 4 motors, one already fixed in. 4 wheels. There's also another 'wheel' to attach inside the chassis, that you can just see. I think it can be used for measuring distance. More on that later. |
|