Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- for( int i = 0; i < numLEDs; i++){
- if( potVal > (LEDpotValWindow * i) && potVal < ( LEDpotValWindow *(i+1)) ){
- PWMvals[i] = map(potVal, LEDpotValWindow * i, LEDpotValWindow *(i+1), 0, 255);
- }
- else if(potVal > ( LEDpotValWindow *(i+1))) {
- PWMvals[i] = 255;
- }
- else{
- PWMvals[i] = 0;
- }
- analogWrite(LEDpins[i], PWMvals[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement