2 Pots - Directional Color Shifting

ldevendorf's picture

Description:

This code users two potentiometers to control three LEDS.  Twisting one pot "moves" the light along the LEDS in a direction parallel to the direction of turning.  Twisting the other pot adjusts the brightness of of the LEDs. 

Materials:

  • 2 Potentiometers
  • 3 LEDs
  • 1 Arduino Uno Controller

Arduino Code:

/*
Uses the position of one pot to determine which lights are on
and the position from the other pot to determine the brightness

Lights "move" in a direction parallel the the direction of motion.

Began with 2 Pots template code posted on website.

 */
int pot1Pin = 0;   // select the input pin for the potentiometer 1
int pot2Pin = 1;   // select the input pin for the potentiometer 2
int pot1Val = 0;   // variable to store the value coming from pot 1
int pot2Val = 0;   // variable to store the value coming from pot 2
int redPin = 9;    // select the pin for the Red LED
int greenPin = 10;  // select the pin for the Green LED
int bluePin = 11;  // select the pin for the Blue LED

void setup() {
   pinMode(redPin, OUTPUT);
   pinMode(greenPin, OUTPUT);
   pinMode(bluePin, OUTPUT);  
}
void loop() {
   pot1Val = analogRead(pot1Pin);   // read the value from pot 1, between 0 - 1024, for dimming
   pot2Val = analogRead(pot2Pin);   // read the value from pot 2, between 0 - 1024, for color shifting
   
   if(pot2Val > 768){
     analogWrite(redPin, pot1Val/4);
     analogWrite(greenPin, 0);
     analogWrite(bluePin, 0);
   }else if(pot2Val > 512){
     analogWrite(redPin, pot1Val/4);
     analogWrite(greenPin, pot1Val/4);
     analogWrite(bluePin, 0);
   }else if(pot2Val >  256){
      analogWrite(redPin, 0);
      analogWrite(greenPin, pot1Val/4);
      analogWrite(bluePin, pot1Val/4);
   }else{
      analogWrite(redPin, 0);
      analogWrite(greenPin, 0);
      analogWrite(bluePin, pot1Val/4);
   }  
 

}

Board Setup
0
Your rating: None