Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Analog input from photocell to determine LED brightness
- Auto theshold calibration
- Matt Richard
- http://idblab.blogspot.com
- */
- int sensorPin = A0; // select the input pin for the potentiometer
- int ledPin = 3; // select the pin for the LED
- int sensorValue = 0; // variable to store the value coming from the sensor
- int minThresh = 1023;// replace this number with a lowest number from the serial monitor
- int maxThresh = 0;// replace this number with a highest number from the serial monitor
- void setup() {
- Serial.begin(9600);
- // use the LED attached to pin 13 as a signal when we are autocalibrating
- pinMode(13, OUTPUT);
- digitalWrite(13, HIGH);
- // begin auto calibration
- for( int i = 0; i < 1000; i++){
- // grab the sensor value
- sensorValue = analogRead(sensorPin);
- // check to see if the sensor value is lower than the minimum threshold
- if( sensorValue < minThresh){
- // if it is, set the minimum threshold to the current sensorValue
- minThresh = sensorValue;
- }
- // check to see if the sensor value is higher than the maximum threshold
- if( sensorValue > maxThresh){
- // if it is, set the maximum threshold to the current sensorValue
- maxThresh = sensorValue;
- }
- delay(5);
- }
- digitalWrite(13, LOW);
- }
- void loop() {
- // read the value from the sensor
- sensorValue = analogRead(sensorPin);
- // print the value from the sensor
- Serial.println(sensorValue);
- // constrain the sensor value so that it does not exceed the thresholds
- sensorValue = constrain(sensorValue, minThresh, maxThresh);
- // map the sensor value to the PWM range
- pwmValue = map(sensorValue, minThresh, maxThresh, 0, 255);
- // power the LED using the pwmValue
- analogWrite(ledPin, pwmValue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement