7.3 DC Motor Control
Connect a DC motor to port A and try the code below using either Logo or Tinker.
to motorcontrol
a, on wait 10 off
beep
end
This program will turn on motor A for 1 second (the wait command has a unit of one-tenth of a second)
Motor Selection
Before performing any action, a motor port or ports must first be selected. In Logo, this is done using the port name followed by a comma. For example, “a,” will select port A. “ab,” will select both port A and B. “abcd,” will select all four ports. Notice that the alphabetical order does not affect port selection. That is “abcd,” is the same as “dcab,”.
In Tinker, motor port selection is done visually using the “talk to motor” block. Tick on the port name to select that port. The example below will select port A.
Motor Actions
Once a port or ports have been selected, actions can be performed on them as shown below.
Tinker | Logo | Action on the selected motor(s) |
---|---|---|
on | Turns on | |
off | Turns off | |
onfor 1 | Turns on for one-tenth of a second. This number can be changed. | |
cc | Rotate motor clockwise | |
ccw | Rotate motor counter-clockwise | |
rd | Reverse the current rotation to the opposite direction. | |
setpower 100 | Set the power level to 100. This number can be between 0 (off) to 100 (full power). |
Motor State
The on-off state of the motor ports can be tested using the following reporter.
Tinker | Logo | Value |
---|---|---|
aon? | Returns “true” if the specified motor is “on”. If more than one port is selected, returns “true” only if all ports are on. |
Here’s an example that will turn off port A if it is on.
to off_if_on
if aon? [ a, off]
end