Purpose:
These sketches are getting long so I am putting them on separate pages . The following is the sample code for controlling the motors on the robot
/*
Motor Control IThis sketch shows how to drive both wheel motors together at the same speed.
On the Qbot the motors are on pins 5 (right) and 6 (left) and the direction
control pins are 7 (right) and 8 (left)For those who have Arduino Experience:
The motor pins are PWM pins to allow for speed control in the usual way.
If you want to change the pin you're using, be sure to use another PWM capable
pin. On most Arduinos, the PWM pins are identified with a "~" sign on the board
like ~3, ~5, ~6, ~9, ~10 and ~11.For those new to Arduino.
Go through the lab tutorial about Pulse Width Modulation (PWM) until you feel
that you have some understanding of what it means. On the Qbot we can use some
pins like dimmers instead of just switches. When we use the digitalWrite();
command the pins acts like an on/off switch. If we use the analogWrite();
command we can set the pin to any level between 0 and 255. O is like LOW and is
completely off while 255 is like HIGH and is completely on. Only certain pins
are capable of 'dimming' but 5 and 6 which run the motors are capable of this.6/22/20
Bruce Emerson*/
// initialize variables that will used repetitively during execution
int rightMotor = 5; // the PWM pin for the right motor
int leftMotor = 6; // the PWM pin for the left motor
int rightForward = 7; // forward-reverse right motor
int leftForward = 8; // forward-reverse left motor
int maxspeed = 250; // set max motor speed for safety, 255 is absolute max// adjustments to balance the motors
int forwardOffset = 0; // correction to motor going forward: 0 means no adjust
int reverseOffset = 0; // correction to motor going in reverse: 0 means no adjust// settings for turning
int turnSpeed = 50; // set speed for making turns
int turn90Time = 1820; // turn time for 90 degrees// motor speed for this test
int motorSpeed = 100; //set speed for the test
// the setup routine runs once when you press reset:
void setup() {
// declare motor pins to be outputs:
pinMode(rightMotor, OUTPUT);
pinMode(leftMotor, OUTPUT);
pinMode(rightForward, OUTPUT);
pinMode(leftForward, OUTPUT);
delay(5000); // this allows you 5 s after you start the Qbot to set it down}
// the loop routine runs over and over again forever:
void loop() {
// set the direction of motor: forward
digitalWrite(rightForward, LOW);
digitalWrite(leftForward, LOW);
// set the speed of motor:
analogWrite(rightMotor, motorSpeed+forwardOffset);
analogWrite(leftMotor, motorSpeed);
// forward for 5 s
delay(5000);//stop the motors
analogWrite(rightMotor, 0);
analogWrite(leftMotor, 0);
// wait 3 s
delay(3000);
// insert code for turning here// end turning code here
// set the direction of motor:reverse
digitalWrite(rightForward, HIGH);
digitalWrite(leftForward, HIGH);
// set the speed of motor:
analogWrite(rightMotor, motorSpeed + reverseOffset);
analogWrite(leftMotor, motorSpeed);
// reverse for 5 s
delay(5000);
//stop the motors
analogWrite(rightMotor, 0);
analogWrite(leftMotor, 0);// wait 3 s
delay(3000);// insert same code for turning here
// end turning code here
}
Copy and paste this into your sketch and then save it with a useful name of your choice. For me I called it QMotorControlI.