Speed control
Getting a vehicle to move can be treated as a digital operation in the sense that the movement is either on or off. The problem with this approach is that the motors either run at full speed or not at all. Clearly, it is important in certain situations to be able to control the speed so as to run more slowly. This can be achieved with a microprocessor using a technique called pulse-width modulation (PWM) which is covered in the general notes section. The main principle is that the pin which switches a motor on is not held high continuously, but instead is used to deliver a series of pulses at a given frequency. Controlling the width of the pulses allows you to set the proportion of time that the motor is on, and that translates into a speed somewhere between zero and full speed.
Implementing PWM on an Arduino is very simple, and you can find all the information you need by using the link in the resources box.
If you want to use input signals from some other source to control the speed, you will probably find that the range of the input signals is different from the range required by the PWM output. In this case, you may need to use the Arduino map function to translate one range into the other. You can find details of how to use the map function on the Arduino language reference page.
Direction control
The motors used in the vehicles are basic DC motors with an integral gearbox. They provide no internal monitoring in the way that servos do, and there may be considerable variation in their performance. This means that your vehicle may well tend to veer to one side even when you supply the same input signal to both of them. To correct this, you would need to apply some kind of external control. Some possible strategies for doing this are:
-
Calibrate your motors experimentally, and apply a numerical correction factor to the input signals. This approach is known as open loop control: there is still no feedback, but you are taking some action to deliver the behaviour you are trying to achieve.
-
Incorporate a sensor that can provide appropriate feedback on the direction of travel. This might be a magnetometer which is sensitive to absolute orientation, a gyroscope which is sensitive to changes in orientation, an accelerometer which is sensitive to changes in direction of travel, or a combination of these. This type of sensor is covered in the notes on components.
-
Provide some kind of external frame of reference and a sensor to monitor the vehicle's position within that frame of reference. For example, you could use a range sensor to detect the vehicle's distance from a wall, and then use that information to keep that distance constant. Alternatively, you could set up a beacon - for example using visible or IR LEDs - and triangulate the vehicle's position in relation to them. A third option is to set out a path on the ground for the vehicle to follow.
In the remainder of this exercise, we will be using the third strategy above; however, the components for the first two are also available if you prefer.