Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Jung Min Yean
- * 04/02/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;
- const int PotPin = A1;
- int PotVal = 0;
- int PhotoVal = 0;
- unsigned long timeStart = 0;
- int Max = 0;
- int Min = 1000;
- const byte Button = 8;
- byte ReadVal;
- int count = 0;
- void setup()
- {
- pinMode(LedPin1, OUTPUT);
- pinMode(LedPin2, OUTPUT);
- pinMode(LedPin3, OUTPUT);
- pinMode(PhotoPin, INPUT);
- Serial.begin(9600);
- digitalWrite(LedPin1, HIGH);
- delay(100);
- digitalWrite(LedPin1, LOW);
- calibrate();
- }
- void calibrate()
- {
- timeStart = millis();
- while (millis()-timeStart <= 5000)
- {
- PhotoVal = analogRead(PhotoPin);
- if(PhotoVal > Max)
- {
- Max = PhotoVal;
- }
- if(PhotoVal < Min)
- {
- Min = PhotoVal;
- }
- }
- Serial.print("The Max Value is ");
- Serial.println(Max);
- }
- void loop()
- {
- // test potentiometer
- // PotVal = analogRead(PotPin);
- PhotoVal = analogRead(PhotoPin);
- Serial.println(PhotoVal);
- delay (200);
- // 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement