/* The Goal: Using the map() function, we will control 6 LEDs' PWM value, by slicing the incoming value from a potentiometer. */ int LEDpins[] = { 3,5,6,9,10,11};// an array of the PWM pins int PWMvals[6];// an array of the PWM values: 0-255, initialized in setup() int potVal = 0;// potentioter value: 0-1023 int LEDpotValWindow; int numLEDs = 6;// number of LEDs void setup(){ for(int i = 0; i < numLEDs; i++){ //pinMode( LEDpins[i], OUTPUT); PWMvals[i] = 0; } Serial.begin(9600); LEDpotValWindow = 1023 / numLEDs; } void loop(){ // first thing is to grab the potentiometer value potVal = analogRead(A0); // now we want to turn on only the leds that should be lit // and then only as much as they should be lit. // if statement for 1st LED if( potVal < LEDpotValWindow){ PWMvals[0] = map(potVal, 0, LEDpotValWindow, 0, 255); } else{ PWMvals[0] = 255; } analogWrite(LEDpins[0], PWMvals[0]); // if statement for 2nd LED if( potVal > LEDpotValWindow && potVal < ( LEDpotValWindow * 2) ){ PWMvals[1] = map(potVal, LEDpotValWindow, LEDpotValWindow * 2, 0, 255); } else if(potVal > ( LEDpotValWindow * 2)) { PWMvals[1] = 255; } else{ PWMvals[1] = 0; } analogWrite(LEDpins[1], PWMvals[1]); // if statement for 3rd LED if( potVal > ( LEDpotValWindow * 2) && potVal < ( LEDpotValWindow * 3) ){ PWMvals[2] = map(potVal, LEDpotValWindow * 2, LEDpotValWindow * 3, 0, 255); } else if(potVal > ( LEDpotValWindow * 3)) { PWMvals[2] = 255; } else{ PWMvals[2] = 0; } analogWrite(LEDpins[2], PWMvals[2]); // if statement for 4th LED if( potVal > ( LEDpotValWindow * 3) && potVal < ( LEDpotValWindow * 4) ){ PWMvals[3] = map(potVal, LEDpotValWindow * 3, LEDpotValWindow * 4, 0, 255); } else if(potVal > ( LEDpotValWindow * 3)) { PWMvals[3] = 255; } else{ PWMvals[3] = 0; } analogWrite(LEDpins[3], PWMvals[3]); // if statement for 5th LED if( potVal > ( LEDpotValWindow * 4) && potVal < ( LEDpotValWindow * 5) ){ PWMvals[4] = map(potVal, LEDpotValWindow * 4, LEDpotValWindow * 5, 0, 255); } else if(potVal > ( LEDpotValWindow * 5)) { PWMvals[4] = 255; } else{ PWMvals[4] = 0; } analogWrite(LEDpins[4], PWMvals[4]); // if statement for 5th LED if( potVal > ( LEDpotValWindow * 5) && potVal < ( LEDpotValWindow * 6) ){ PWMvals[5] = map(potVal, LEDpotValWindow * 5, LEDpotValWindow * 6, 0, 255); } else if(potVal > ( LEDpotValWindow * 6)) { PWMvals[5] = 255; } else{ PWMvals[5] = 0; } analogWrite(LEDpins[5], PWMvals[5]); }