Advertisement
jmyean

Using a Photoresistor with 3 LEDs

Apr 2nd, 2019
160
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.  
  15. void setup()
  16. {
  17.  pinMode(LedPin1, OUTPUT);
  18.  pinMode(LedPin2, OUTPUT);
  19.  pinMode(LedPin3, OUTPUT);
  20.  pinMode(PhotoPin, INPUT);
  21.  Serial.begin(9600);
  22. }
  23.  
  24. void loop()
  25. {
  26.   // test potentiometer
  27.   // PotVal = analogRead(PotPin);
  28.   PhotoVal = analogRead(PhotoPin);
  29.   Serial.println(PhotoVal);
  30.   delay (200);
  31.   // Serial.print("      ");
  32.   int MapVal = map(PhotoVal,0,173,255,0);
  33.   MapVal = constrain(MapVal, 0,255);
  34.   Serial.println(MapVal);
  35.   analogWrite(LedPin1, MapVal);
  36.   analogWrite(LedPin2, MapVal);
  37.   analogWrite(LedPin3, MapVal);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement