Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Jung Min Yean
- * 04/04/19
- * Learning How to Use a Photoresistor with LEDs
- */
- const int LedPin1 = 9;
- const int LedPin2 = 10;
- const int LedPin3 = 11;
- const int PhotoPin = A0;
- int PhotoVal = 0;
- unsigned long timeStart = 0;
- int Max = 0;
- int Min = 1000;
- boolean currentVal;
- boolean lastVal;
- const int Button = 2;
- void setup()
- {
- pinMode(LedPin1, OUTPUT);
- pinMode(LedPin2, OUTPUT);
- pinMode(LedPin3, OUTPUT);
- pinMode(PhotoPin, INPUT);
- pinMode(Button, INPUT);
- Serial.begin(9600);
- digitalWrite(LedPin1, HIGH);
- delay(100);
- digitalWrite(LedPin1,LOW);
- calibrate();
- }
- boolean debounce(boolean last)
- {
- boolean current = digitalRead(Button);
- if (last != current)
- {
- delay(5);
- current = digitalRead(Button);
- Serial.println(current);
- }
- return current;
- }
- void calibrate()
- {
- Max = 0;
- Min = 1000;
- analogWrite(LedPin1,200);
- analogWrite(LedPin2, 0);
- analogWrite(LedPin3,0);
- delay(200);
- analogWrite(LedPin1, 0);
- analogWrite(LedPin2, 0);
- analogWrite(LedPin3,0);
- timeStart = millis();
- while (millis()-timeStart <= 5000)
- {
- delay(5);
- PhotoVal = analogRead(PhotoPin);
- Serial.println(PhotoVal);
- if(PhotoVal > Max)
- {
- Max = PhotoVal;
- }
- if(PhotoVal < Min)
- {
- Min = PhotoVal;
- }
- }
- Serial.print("The Max Value is ");
- Serial.println(Max);
- Serial.print("The Min Value is");
- Serial.println(Min);
- }
- void loop()
- {
- currentVal = debounce(lastVal);
- if (lastVal == LOW && currentVal == HIGH) // Checks if the button has been pressed
- {
- calibrate();
- }
- PhotoVal = analogRead(PhotoPin);
- // Serial.println(PhotoVal);
- // Serial.print(" ");
- int MapVal = map(PhotoVal,Min,Max,255,0);
- MapVal = constrain(MapVal, 0,255);
- // Serial.println(MapVal);
- analogWrite(LedPin1, MapVal);
- analogWrite(LedPin2, MapVal);
- analogWrite(LedPin3, MapVal);
- lastVal=currentVal;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement