Advertisement
jmyean

Calibrating the Photoresistor

Apr 3rd, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  Jung Min Yean
  3.  *  04/02/19
  4.  *  Learning How to Use a Photoresistor with LEDs
  5.  */
  6.  
  7.  const int LedPin1 = 9;
  8.  const int LedPin2 = 10;
  9.  const int LedPin3 = 11;
  10.  const int PhotoPin = A0;
  11.  const int PotPin = A1;
  12.  int PotVal = 0;
  13.  int PhotoVal = 0;
  14.  unsigned long timeStart = 0;
  15.  int Max = 0;
  16.  int Min = 1000;
  17.  const byte Button = 8;
  18.  byte ReadVal;
  19.  int count = 0;
  20.  
  21. void setup()
  22. {
  23.  pinMode(LedPin1, OUTPUT);
  24.  pinMode(LedPin2, OUTPUT);
  25.  pinMode(LedPin3, OUTPUT);
  26.  pinMode(PhotoPin, INPUT);
  27.  Serial.begin(9600);
  28.   digitalWrite(LedPin1, HIGH);
  29.   delay(100);
  30.   digitalWrite(LedPin1, LOW);
  31.     calibrate();
  32. }
  33.  
  34. void calibrate()
  35.  {
  36.   timeStart = millis();
  37.   while (millis()-timeStart <= 5000)
  38.   {
  39.     PhotoVal = analogRead(PhotoPin);
  40.     if(PhotoVal > Max)
  41.     {
  42.       Max = PhotoVal;
  43.     }
  44.     if(PhotoVal < Min)
  45.     {
  46.       Min = PhotoVal;
  47.     }
  48.   }
  49.   Serial.print("The Max Value is ");
  50.   Serial.println(Max);
  51.  }
  52.  
  53. void loop()
  54. {
  55.   // test potentiometer
  56.   // PotVal = analogRead(PotPin);
  57.   PhotoVal = analogRead(PhotoPin);
  58.   Serial.println(PhotoVal);
  59.   delay (200);
  60.   // Serial.print("      ");
  61.   int MapVal = map(PhotoVal,Min,Max,255,0);
  62.   MapVal = constrain(MapVal, 0,255);
  63.   Serial.println(MapVal);
  64.   analogWrite(LedPin1, MapVal);
  65.   analogWrite(LedPin2, MapVal);
  66.   analogWrite(LedPin3, MapVal);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement