In one of my first circuit (ECE35) classes, I used a photodiode to output a different signal based on the ambient light. I decided to apply this concept in my own lab. I used a small photosensor, a motor, and a light along with my Arduino to create a circuit that output a different brightness on the light or a different speed on the motor based on the light in the room. First, I calibrated the light level reading myself, but then I discovered a way to automatically adjust the set the low and high light levels. In other words, the Arduino only reads increases and decreases in brightness instead of using predetermined values.
Code: https://github.com/RoboSteve/Light_Dependent_Motor
const int motorPin = 9; const int sensorPin = 0; int lightLevel, high = 0, low = 1023; void setup() { pinMode(motorPin, OUTPUT); } void loop() { lightLevel = analogRead(sensorPin); //manualTune(); autoTune(); analogWrite(motorPin, lightLevel); } void manualTune() { lightLevel = map(lightLevel, 0, 1023, 0, 255); lightLevel = constrain(lightLevel, 0, 255); } void autoTune() { if (lightLevel < low) { low = lightLevel; } if (lightLevel > high) { high = lightLevel; } lightLevel = map(lightLevel, low+30, high-30, 0, 255); lightLevel = constrain(lightLevel, 0, 255); }
Thursday, June 30 2016, 14:55:55