Advertisement
3rdWard_Arduino

class4_6LEDs to pot mapping_with for loop

Feb 15th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.   for( int i = 0; i < numLEDs; i++){
  32.     if( potVal > (LEDpotValWindow * i) && potVal < ( LEDpotValWindow *(i+1)) ){
  33.       PWMvals[i] = map(potVal, LEDpotValWindow * i, LEDpotValWindow *(i+1), 0, 255);
  34.     }
  35.     else if(potVal > ( LEDpotValWindow *(i+1))) {
  36.       PWMvals[i] = 255;
  37.     }
  38.     else{
  39.       PWMvals[i] = 0;
  40.     }
  41.     analogWrite(LEDpins[i], PWMvals[i]);
  42.   }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement