SHOW:
|
|
- or go back to the newest paste.
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 |
31 | + | for( int i = 0; i < numLEDs; i++){ |
32 | - | if( potVal < LEDpotValWindow){ |
32 | + | if( potVal > (LEDpotValWindow * i) && potVal < ( LEDpotValWindow *(i+1)) ){ |
33 | - | PWMvals[0] = map(potVal, 0, LEDpotValWindow, 0, 255); |
33 | + | PWMvals[i] = map(potVal, LEDpotValWindow * i, LEDpotValWindow *(i+1), 0, 255); |
34 | } | |
35 | - | else{ |
35 | + | else if(potVal > ( LEDpotValWindow *(i+1))) { |
36 | - | PWMvals[0] = 255; |
36 | + | PWMvals[i] = 255; |
37 | } | |
38 | - | analogWrite(LEDpins[0], PWMvals[0]); |
38 | + | else{ |
39 | PWMvals[i] = 0; | |
40 | - | // if statement for 2nd LED |
40 | + | } |
41 | - | if( potVal > LEDpotValWindow && potVal < ( LEDpotValWindow * 2) ){ |
41 | + | analogWrite(LEDpins[i], PWMvals[i]); |
42 | - | PWMvals[1] = map(potVal, LEDpotValWindow, LEDpotValWindow * 2, 0, 255); |
42 | + | |
43 | } |