Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int photoPin=A0;
- int photoValue=0;
- int lightVal=0;
- const int PinRed=9;
- const int PinGreen=10;
- const int PinBlue=11;
- const int pinButt=6;
- int newButt=0;
- int lastButt=0;
- int minimum = 1000;
- int maximum = -1000;
- unsigned long timeVal;
- void setup()
- {
- // put your setup code here, to run once:
- Serial.begin(9600);
- pinMode(photoPin, INPUT);
- pinMode (PinRed, OUTPUT);
- pinMode (PinBlue, OUTPUT);
- pinMode(PinGreen, OUTPUT);
- analogWrite(PinRed, 255);
- analogWrite(PinGreen, 0);
- analogWrite(PinBlue, 0);
- delay (500);
- analogWrite(PinRed, 0);
- analogWrite(PinGreen, 255);
- analogWrite(PinBlue, 0);
- delay(500);
- analogWrite(PinRed, 0);
- analogWrite(PinGreen, 0);
- analogWrite(PinBlue, 255);
- }
- void calibrate ()
- {
- minimum = 1000;
- maximum = -1000;
- analogWrite (PinGreen, HIGH);
- timeVal =millis();
- while (millis ()-timeVal<5000)
- {
- photoValue = analogRead(photoPin);
- if (photoValue > maximum)
- {
- maximum = photoValue;
- }
- else if (photoValue < minimum)
- {
- minimum = photoValue;
- }
- }
- Serial.println(minimum);
- Serial.println(maximum);
- digitalWrite (PinGreen, LOW);
- }
- boolean debounce (boolean last)
- {
- boolean current = digitalRead(pinButt);
- if (last != current)
- {
- delay(5);
- current = digitalRead(pinButt);
- }
- return current;
- }
- void loop()
- {
- // put your main code here, to run repeatedly:
- newButt = debounce (lastButt);
- if(newButt==HIGH && lastButt ==LOW)
- {
- calibrate();
- }
- lastButt=newButt;
- photoValue=analogRead (photoPin);
- lightVal=map (photoValue, minimum, maximum, 255, 0);
- lightVal=constrain (lightVal, 0, 255);
- analogWrite (PinRed, lightVal);
- analogWrite (PinGreen,lightVal);
- }
Advertisement
Add Comment
Please, Sign In to add comment