The code is pretty simple. I wrote a few functions to simplify the main portion of the program. You may notice I am using different PWM values for each motor in the goForward and goBack functions. For one of the motors I am using a PWM value of 233, while I'm using 255 for the other. These were the values I found would provide a straight track on Clusterbot. Whether because of physical difference between the cheap toy motors or alignment issues in the chassis - this was a quick and dirty way to have the bot track somewhat straight.
#define PWMA 10
#define AIN1 6
#define AIN2 7
#define PWMB 11
#define BIN1 8
#define BIN2 9
void setup()
{
pinMode(PWMA,OUTPUT);
pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(PWMB,OUTPUT);
pinMode(BIN1,OUTPUT);
pinMode(BIN2,OUTPUT);
}
void loop() {
goForward();
delay(2000);
turnAround();
goForward();
delay(2000);
turnAround();
goBackward();
delay(2000);
rotateLeft();
delay(560);
rotateRight();
delay(560);
goForward();
delay(2000);
applyBrakes();
delay(2000);
}
void goForward ()
{
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
analogWrite(PWMA,234);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
analogWrite(PWMB,255);
}
void goBackward ()
{
digitalWrite (AIN1,LOW);
digitalWrite (AIN2,HIGH);
analogWrite(PWMA,233);
digitalWrite (BIN1,LOW);
digitalWrite (BIN2,HIGH);
analogWrite(PWMB,255);
}
void rotateRight ()
{
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
analogWrite(PWMA,255);
digitalWrite (BIN1,LOW);
digitalWrite (BIN2,HIGH);
analogWrite(PWMB,255);
}
void rotateLeft ()
{
digitalWrite (AIN1,LOW);
digitalWrite (AIN2,HIGH);
analogWrite(PWMA,255);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
analogWrite(PWMB,255);
}
void veerLeft ()
{
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
analogWrite(PWMA,190);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
analogWrite(PWMB,255);
}
void veerRight ()
{
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
analogWrite(PWMA,255);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,LOW);
analogWrite(PWMB,190);
}
void applyBrakes ()
{
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,HIGH);
analogWrite(PWMA,255);
digitalWrite (BIN1,HIGH);
digitalWrite (BIN2,HIGH);
analogWrite(PWMB,255);
}
void turnAround()
{
rotateLeft();
delay(1370);
}
In the video, you will notice that when I remove the USB cable, the motors stop turning. I think the problem was that I was supplying 6VDC to the VIN pin on the Arduino Nano. The Nano needs an input voltage of 7-12VDC on VIN, because the voltage regulator apparenlty needs the input voltage to be at least 2 volts higher than the regulated 5VDC output.
My immediate solution was to simply swing the 6VDC to the 5VDC pin on the Arduino. This is not the smartest thing in the world, but it worked. I think the better solution will be to simply use a 9 volt battery for powering the Arduino Nano.
One other issue - I think I am providing too much voltage to the motors in the Tamiya twin motor gearbox. I read the specs at Pololu, and the recommended voltage is 3VDC. I am using 6VDC, which will result in a short lifespan for these motors. I will either lower the voltage with a resistor divider, or I may switch out battery packs.
To read more about voltages with these Tamiya gear boxes, you can check out this research: http://www.pololu.com/docs/0J11. After reading this, maybe I could get away with running 4 rechargeables...that would be 4.8 volts. It's not like I would run these for hours at a time like Adam did with his test. I'm looking at runs of ~10-20 minutes, with plenty of cool down time in between.
During my test run, I noticed the zip tie on the bottom of the motors was touching the floor instead of the wheel caster. Going to have to trim this down. Otherwise, I was happy to see the new bot in locomotion.
Now it's time to add some sensors!