
25
You will see the motor start at a low speed
and then it will slowly increase the speed
until it reaches its maximum speed. When
the motor reaches its maximum speed, the
speed of the motor will start to slowly
decrease and reach its minimum speed.
What will you see
1. Declare the pin the MOSFET is connected to.
2. Define MOSFET as an output module.
3. Slowly increase the speed of the motor.
4. Slowly decrease the speed of the motor.
Code overview
Declare two variables named mosfetPin and delayTime, which means that we can use MOSFET
through D5 port and the value of delayTime is 50.
Integer Variables: int mosfetPin = 5; int delayTime = 50;
The Arduino for loop provides a mechanism to repeat a section of code depending on the value of a
variable. So you set the initial value of the variable, the condition to exit the loop (testing the variable),
and the action on the variable each time around the loop. Initialiser section: The initial value of the
control variable. Condition Section: The condition to stop the loop. Iterator Section: The loop variable
action (increment or decrement).
For() statement: for (<initialiser code> ; <condition test expression> ; <iterator
expression>){code to be run if condition test expression is true }
The prototype of the analogWrite () function is analogWrite(pin,value). We know the analogWrite()
function is to write the value on an analog pin. Actually, writing the analog value is controlled by PWM,
so we can directly use analogWrite () to write the PWM wave. Similar to digitalWrite() function, it takes
two parameter, But the second parameter is no longer only two high and low states, it can be any
number you want to write, and each value you write will give it the corresponding state. Note that value
has 256 levels, from 0-255.
Analog output: analogWrite(mosfetPin, i);
Code usage
Summary of Contents for Crowtail
Page 48: ......