Advertisement
jmyean

Using a Potentiometer and AnalogWrite to Control 3 LEDs

Apr 2nd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  Jung Min Yean
  3.  *  04/01/19
  4.  *  Learning How to Use Analog Read and Write
  5.  */
  6.  
  7.  const int LedPin1 = 9;
  8.  const int LedPin2 = 10;
  9.  const int LedPin3 = 11;
  10.  const int PotPin = A0;
  11.  int PotVal = 0;
  12.  
  13. void setup()
  14. {
  15.  pinMode(LedPin1, OUTPUT);
  16.  pinMode(LedPin2, OUTPUT);
  17.  pinMode(LedPin3, OUTPUT);
  18.  pinMode(PotPin, INPUT);
  19.  Serial.begin(9600);
  20. }
  21.  
  22. void loop()
  23. {
  24.   // test potentiometer
  25.   PotVal = analogRead(PotPin);
  26.   Serial.print(PotVal);
  27.   Serial.print("      ");
  28.   int MapValue = map(PotVal,0,1023,255,0);
  29.   Serial.println(MapValue);
  30.   analogWrite(LedPin1, PotVal);
  31.   analogWrite(LedPin2, PotVal);
  32.   analogWrite(LedPin3, PotVal);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement