We played with the Metro's ability to control the brightness of an LED earlier. Now, we will use the same feature to control the speed of our motor.
The Metro does this using something called Pulse Width Modulation (PWM). This relies on the METRO’s ability to operate really, really fast. Rather than directly controlling the voltage coming from the pin, the Metro will switch the pin on and off very quickly.
In the computer world this is going from 0 to 5 volts many times a second, but in the human world we see it as a voltage. For example: if the Metro is PWM'ing at 50%, we see the light dimmed 50% because our eyes are not quick enough to see it flashing on and off. The same feature works with transistors. (if you want a visual explanation of this concept, click here)
Don't believe me? Try it out!
Copy and paste the code snippet below into the loop()
function of your code:
// motorOnThenOff(); motorOnThenOffWithSpeed(); // motorAcceleration();
Then Upload the program.
You can change the speeds by changing variables onSpeed
and offSpeed
to any number between 0 (stop the motor) and 255 (full power!)
Why stop at two speeds? Why not accelerate and decelerate the motor.
To do this simply change the loop() code to read:
// motorOnThenOff(); // motorOnThenOffWithSpeed(); motorAcceleration();
Then Upload the program and watch as your motor slowly accelerates up to full speed then slows down again.
If you would like to change the speed of acceleration, change the variable delayTime
(larger means a longer acceleration time) to a different value.
Text editor powered by tinymce.