Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int PhotoR = A0;
- const int LEDpin = 6;
- int ReadValue = 0;
- unsigned long Time;
- int MinVal = 1000;
- int MaxVal = 0;
- const int Button = 3;
- int ButtonVal = 0;
- boolean check = false;
- void setup()
- {
- pinMode (PhotoR, INPUT);
- pinMode (LEDpin, OUTPUT);
- pinMode (Button, INPUT);
- Serial.begin(9600);
- }
- void calibrate()
- {
- Serial.println("in calibrate");
- digitalWrite (LEDpin, HIGH);
- Time = millis();
- while (millis() - Time <=5000)
- {
- ReadValue = analogRead(PhotoR);
- if (ReadValue < MinVal)
- {
- MinVal = ReadValue;
- }
- if (ReadValue > MaxVal)
- {
- MaxVal = ReadValue;
- }
- }
- digitalWrite(LEDpin, LOW);
- }
- void loop()
- {
- ButtonVal = digitalRead (Button);
- if (check)
- {
- Serial.println("Check is true");
- if (ButtonVal == 1)
- {
- Serial.println("Button is on");
- calibrate();
- }
- }
- else
- {
- Serial.println("Check is false");
- calibrate();
- check = true;
- }
- ReadValue = analogRead(PhotoR);
- ReadValue = map(ReadValue, MinVal, MaxVal, 255, 0);
- ReadValue = constrain(ReadValue, 0, 255);
- Serial.println(ReadValue);
- analogWrite(LEDpin, ReadValue);
- }
Add Comment
Please, Sign In to add comment