Now that the HW is in place, bring up the Arduino IDE; make sure that the proper port and device are selected and enter the following code:
The code is straightforward. It consists of the following three blocks:
int pwmA = 3; int pwmB = 11; int brakeA = 9; int brakeB = 8; int directionA = 12; int directionB = 13;
setup()
function, which sets the directionA
, directionB
, brakeA
, and brakeB
digital output pins:pinMode(directionA, OUTPUT); pinMode(brakeA, OUTPUT); pinMode(directionB, OUTPUT); pinMode(brakeB, OUTPUT);
loop()
function. This is an example of how to make the wheeled robot go forward and then turn to the right. At each of these steps, you'll need to use the brake to stop the robot. The code is as follows:// Move forward digitalWrite(directionA, HIGH); digitalWrite(brakeA, LOW); analogWrite(pwmA, 255); digitalWrite(directionB, HIGH); digitalWrite(brakeB, LOW); analogWrite(pwmB, 255); delay(2000); digitalWrite(brakeA, HIGH); digitalWrite(brakeB, HIGH); delay(1000); //Turn right digitalWrite(directionA, LOW); //Establishes backward direction of Channel A digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A analogWrite(pwmA, 128); //Spins the motor on Channel A at half speed digitalWrite(directionB, HIGH); //Establishes forward direction of Channel B digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B analogWrite(pwmB, 128); //Spins the motor on Channel B at full speed delay(2000); digitalWrite(brakeA, HIGH); digitalWrite(brakeB, HIGH); delay(1000);
Once you have uploaded the code, the program should run in a loop. If you want to run your robot without connection to the computer, place batteries into the battery holder, disconnect the USB cable connecting Arduino to the computer, and then press the RESET button. Your robot can move all by itself!
18.118.149.19