Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int ledpin = 12;
- const int photopin = A0;
- const int button = 5;
- unsigned long timeValue = 0;
- int minVal = 1000;
- int maxVal = 0;
- int photoValue;
- bool check = false;
- void setup()
- {
- pinMode (photopin, INPUT);
- pinMode (ledpin, OUTPUT);
- Serial.begin (9600);
- }
- void calibrate()
- {
- if (digitalRead (button) == HIGH);
- digitalWrite (ledpin, HIGH);
- timeValue = millis();
- while(millis()-timeValue<5000)//sets the time it takes to callibrate 5 seconds
- {
- photoValue = analogRead (photopin);//This is used to callibrate the photoresistor (place your hand on and off of the photoresistor to get the resistor to see the max and min
- if (photoValue<minVal)
- {
- minVal = photoValue;
- }
- if (photoValue>maxVal)
- {
- maxVal = photoValue;
- }
- }
- digitalWrite (ledpin, LOW);
- Serial.println ("I am in");
- }
- void loop()
- {
- photoValue = digitalRead (button);
- Serial.println (photoValue);
- if (check==false)
- {
- Serial.println ("check is true");
- if (button==1)
- {
- Serial.println ("button is read");
- calibrate();
- }
- else
- {
- Serial.println ("check is false");
- check = true;
- }
- }
- photoValue = analogRead (photopin);
- photoValue = constrain(photoValue, 0, 255);//makes min and max values 0 and 120
- photoValue = map (photoValue, minVal, maxVal, 255, 0);//maps 255-0 along 0-120
- Serial.println (photoValue);
- analogWrite (ledpin, photoValue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement