Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int photoResistor = A4;
- const int BlueBoy = 3;
- const int RedBoy = 11;
- const int GreenBoy = 5;
- const int LED1 = 9;
- const int Button = 12;
- int buttonRead;
- int photoVal;
- unsigned long timecount;
- int minVal = 1000;
- int maxVal = 0;
- bool currentValue = false;
- bool lastValue = false;
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- pinMode (Button, INPUT);
- pinMode (photoResistor, INPUT);
- pinMode (BlueBoy, OUTPUT);
- pinMode (RedBoy, OUTPUT);
- pinMode (GreenBoy, OUTPUT);
- pinMode (LED1, OUTPUT);
- calibrate();
- }
- void calibrate() {
- digitalWrite(LED1, HIGH);
- delay(50);
- digitalWrite(LED1, LOW);
- timecount = millis();
- while (millis() - timecount < 5000) {
- photoVal = analogRead(photoResistor);
- if (photoVal > maxVal) {
- maxVal = photoVal;
- }
- if (photoVal < minVal) {
- minVal = photoVal;
- }
- }
- }
- void loop() {
- buttonRead = analogRead(Button);
- boolean current = digitalRead(Button);
- if (lastValue != current)
- {
- delay(5);
- currentValue = digitalRead(Button);
- }
- if (lastValue == LOW && currentValue == HIGH){
- calibrate();
- }
- photoVal = analogRead(photoResistor);
- Serial.println(photoVal);
- photoVal = map(photoVal, minVal, maxVal, 255, 0);
- photoVal = constrain(photoVal, 0, 255);
- analogWrite(BlueBoy, photoVal);
- analogWrite(RedBoy, photoVal);
- analogWrite(GreenBoy, photoVal);
- lastValue = currentValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement