Advertisement
3rdWard_Arduino

class4_6LEDs to pot mapping

Feb 15th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. /*
  2. The Goal: Using the map() function, we will control
  3.  6 LEDs' PWM value, by slicing the incoming value
  4.  from a potentiometer.
  5.  */
  6.  
  7. int LEDpins[] = {
  8.   3,5,6,9,10,11};// an array of the PWM pins
  9. int PWMvals[6];// an array of the PWM values: 0-255, initialized in setup()
  10. int potVal = 0;// potentioter value: 0-1023
  11. int LEDpotValWindow;
  12.  
  13. int numLEDs = 6;// number of LEDs
  14.  
  15. void setup(){
  16.   for(int i = 0; i < numLEDs; i++){
  17.     //pinMode( LEDpins[i], OUTPUT);
  18.     PWMvals[i] = 0;
  19.   }
  20.   Serial.begin(9600);
  21.   LEDpotValWindow = 1023 / numLEDs;
  22. }
  23.  
  24. void loop(){
  25.   // first thing is to grab the potentiometer value
  26.   potVal = analogRead(A0);
  27.  
  28.   // now we want to turn on only the leds that should be lit
  29.   // and then only as much as they should be lit.
  30.  
  31.   // if statement for 1st LED
  32.   if( potVal < LEDpotValWindow){
  33.     PWMvals[0] = map(potVal, 0, LEDpotValWindow, 0, 255);
  34.   }
  35.   else{
  36.     PWMvals[0] = 255;
  37.   }
  38.   analogWrite(LEDpins[0], PWMvals[0]);
  39.  
  40.   // if statement for 2nd LED
  41.   if( potVal > LEDpotValWindow && potVal < ( LEDpotValWindow * 2) ){
  42.     PWMvals[1] = map(potVal, LEDpotValWindow, LEDpotValWindow * 2, 0, 255);
  43.   }
  44.   else if(potVal > ( LEDpotValWindow * 2)) {
  45.     PWMvals[1] = 255;
  46.   }
  47.   else{
  48.     PWMvals[1] = 0;
  49.   }
  50.   analogWrite(LEDpins[1], PWMvals[1]);
  51.  
  52.   // if statement for 3rd LED
  53.   if( potVal > ( LEDpotValWindow * 2) && potVal < ( LEDpotValWindow * 3) ){
  54.     PWMvals[2] = map(potVal, LEDpotValWindow * 2, LEDpotValWindow * 3, 0, 255);
  55.   }
  56.   else if(potVal > ( LEDpotValWindow * 3)) {
  57.     PWMvals[2] = 255;
  58.   }
  59.   else{
  60.     PWMvals[2] = 0;
  61.   }
  62.   analogWrite(LEDpins[2], PWMvals[2]);
  63.  
  64.   // if statement for 4th LED
  65.   if( potVal > ( LEDpotValWindow * 3) && potVal < ( LEDpotValWindow * 4) ){
  66.     PWMvals[3] = map(potVal, LEDpotValWindow * 3, LEDpotValWindow * 4, 0, 255);
  67.   }
  68.   else if(potVal > ( LEDpotValWindow * 3)) {
  69.     PWMvals[3] = 255;
  70.   }
  71.   else{
  72.     PWMvals[3] = 0;
  73.   }
  74.   analogWrite(LEDpins[3], PWMvals[3]);
  75.  
  76.   // if statement for 5th LED
  77.   if( potVal > ( LEDpotValWindow * 4) && potVal < ( LEDpotValWindow * 5) ){
  78.     PWMvals[4] = map(potVal, LEDpotValWindow * 4, LEDpotValWindow * 5, 0, 255);
  79.   }
  80.   else if(potVal > ( LEDpotValWindow * 5)) {
  81.     PWMvals[4] = 255;
  82.   }
  83.   else{
  84.     PWMvals[4] = 0;
  85.   }
  86.   analogWrite(LEDpins[4], PWMvals[4]);
  87.  
  88.   // if statement for 5th LED
  89.   if( potVal > ( LEDpotValWindow * 5) && potVal < ( LEDpotValWindow * 6) ){
  90.     PWMvals[5] = map(potVal, LEDpotValWindow * 5, LEDpotValWindow * 6, 0, 255);
  91.   }
  92.   else if(potVal > ( LEDpotValWindow * 6)) {
  93.     PWMvals[5] = 255;
  94.   }
  95.   else{
  96.     PWMvals[5] = 0;
  97.   }
  98.  
  99.   analogWrite(LEDpins[5], PWMvals[5]);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement