14
positive and negative poles, and the OUT3 and OUT4 interfaces are connected to the same motor
positive and negative poles. Be careful not to connect the motor to the interval interface, such as
OUT1 and OUT3, otherwise the motor will not work properly. The complete connection is as follows.
Note: Because the motor consumes more power, after uploading the code, you need to provide additional
power for the Crowtail-Motor Base Shield (usually connect 9V power supply for the power input port of the
motor base shield) and then turn on the onboard power switch(you don’t have to power Crowduino/Arduino,
because the motor base shield will share the power with Crowduino/Arduino).
Open the
P05_Tracing_Experiment
with Arduino IDE and upload it.
Assume that motors 1 connected to OUT1 and OUT3 are mounted on the left tire of the car, and
motors 2 connected to OUT3 and OUT4 are mounted on the right tire of the car. Place the IR
reflective sensors connected to D3, D5, and D7 at the left, middle, and right positions of the bottom
of the cart, respectively. When a black object is held only by the sensor of D3, motor2 rotates faster
than motor1(turn left). When using a black object to block only the D5 sensor, motor1 rotates as fast
as motor2 (forward). When using a black object to block only the sensor of D7, motor1 rotates faster
than motor2 (turn right). When none of the three sensors are blocked by a black object, the speed of
motor1 and motor2 becomes 0 (stopped).
What will you see
1. Declare the pins of three IR Reflective Sensors and variables to store the state of them.
2. Declare the pins to control the speed and direction of rotation of the two motors.
3. Setup the pins which are used to control motors as output.
4. Create functions to control the two motors to turn left, turn right, stop and forward.
5. Read the state of the three IR Reflective Sensors.
6. Use if statement to judge which function should be called to control motor.
Code overview
Code usage
'const' is the abbreviation of constants. The biggest difference between constants and variables is
that constants cannot change values while the program is running, while variables can be changed.
We use three constants to declare three IR reflective sensor pins because the pins of the three
sensors do not need to be changed during the program running. Then we use three variables to
declare the status of the three IR reflective sensors so that we can continuously read the status
values of the IR reflective sensors and update them.
Constant and integer Variables: const int irPin1 = 3; const int irPin2 = 5; const
int irPin3 = 7; int ir1State = 0; int ir2State = 0; int ir3State = 0;
"&&" stands for "AND", that is, the AND operation is performed on the pre and post operands. If the
before and after operands are true, the expression evaluates to true. In the formula, if the left operand
is false, the right operand is no longer calculated.
AND operation: if((ir1State == 0)&(ir2State != 0)&(ir3State != 0)){}
Summary of Contents for Crowtail Deluxe Kit
Page 47: ......