Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //const int Button=7;
- const int PhotoPin=A0;
- //this way, it behaves like analog instead of other stuff
- const int LEDPin=5;
- const int ButtonPin=12;
- int ButtonValue, ReadValue=0;
- int MinValue=1000;
- int MaxValue=0;
- //these two int max and min;
- unsigned long MyTime;
- bool Check;
- void calibrate ()
- {
- digitalWrite(LEDPin,HIGH);
- MyTime=millis();
- while (millis()-MyTime<=5000)
- {
- ReadValue=analogRead(PhotoPin);
- if (ReadValue<MinValue)
- {
- MinValue=ReadValue;
- }
- if (ReadValue>MaxValue)
- {
- MaxValue=ReadValue;
- }
- }
- digitalWrite(LEDPin,LOW);
- }
- void setup()
- {
- pinMode (PhotoPin, INPUT);
- pinMode (LEDPin,OUTPUT);
- pinMode (ButtonPin,INPUT);
- Serial.begin(9600);
- //serial initializes it
- // put your setup code here, to run once:
- }
- void loop()
- {
- ButtonValue=digitalRead(ButtonPin);
- if (Check)
- {
- if (ButtonValue==1)
- {
- calibrate();
- }
- }
- else
- {
- calibrate();
- Check=true;
- }
- ReadValue=analogRead(PhotoPin);
- ReadValue=map (ReadValue, MinValue, MaxValue,255,0);
- ReadValue=constrain(ReadValue, 0, 255);
- //constrain helps make sure it doesnt flicker anymore
- //these numbers indicate min (0) and max (170) and it inserts it into the readvalue)
- Serial.println(ReadValue);
- analogWrite (LEDPin, ReadValue);
- // put your main code here, to run repeatedly:
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement