Motor Speed via Light Sensor

Content

How to control the speed of a motor based on the value read on the light sensor. 

Code
This (Version 1) reads the ambient light in the room and drives the power of the motor. It happens continuously and instantly.  Covering up the light sensor (making it "go to zero") will stop the motor; putting the light sensor up to a bright light will drive the motor forward really fast.
Code
This (version 2) multiplies the value of the light by 2.  This is helpful if you are in a dark room where the values read by the light sensor range from 0 (dark) to ~50 (just "kinda light").  This then takes advantage of the full forward range of the motor (0 to 100) by multiplying the light sensor value by two priot to setting the motor speed.
Code
The prior examples continuously/instantly updated the motor speed.  This ONLY updates the motor speed with the new light value when the touch sensor is bumped.  Note: the bumped function could go at the beginning of the code sequence or at the end (as it currently is).  The only difference is the initial case (first time through the loop).

Leave a Reply

Your email address will not be published. Required fields are marked *