Lab3 submission - potentiometers controlling LED

Submitted by wendy.xue on Tue, 02/19/2013 - 17:40

I used three potentiometers in the circuit. The first potentiometer controls the brightness of all LEDs. The second one controls how frequently the selected LED blinks. The third potentiometer is used to select which LED blinks. The input of the 3rd potentiometer was read and divided into intervals of 100. For each increment of 100, it makes the next LED blinks. The sequence is red, green, blue. For example when the potentiometer reads between 0-100, the red LED blinks. When the potentiometer reads between 100-200, the green one blinks. For the next 100 increment, the blue one blinks. The cycle repeats. 

You can think of the LEDs are music tracks. The first potentiometer controls their volume. The 2nd one controls how fast the tracks are being scratched by a DJ. The 3rd one controls which track is being scratched.

Components

1 - Arduino
3 - LEDs
3 - 220 Ohm resistors
3 - 10k potentiometers 
usb cables and wire

Code:Arduino

/* Control 3 LEDs with 3 potentiometers * pot a will set the brightness of a LED * pot b will set a LED to blink with the input frequency * pot c will choose which LED to set. * we take the mod 3 of the value (normalized to scale of 0-255) read from pot c * if the remainder is 0 = set red LED, if it is 1 => set green LED, if it's 2 => set blue LED */
// Analog pin settings
int brightnessIn = 0;    // Potentiometers connected to analog pins 0, 1, and 2
int blinkIn = 1;    //   (Connect power to 5V and ground to analog ground)
int lotteryIn = 2;  
 
// Digital pin settings
int redOut = 9;   // red LED LEDs connected to digital pins 9, 10 and 11
int greenOut = 10;  // green LED  (Connect cathodes to digital ground)
int blueOut = 11; // blue LED 
 
// Values
int brightnessVal = 0;   // Variables to store the input from the potentiometers
int blinkVal = 0;  
int lotteryVal = 0;  
 
// Variables for comparing values between loops
int i = 0;            // Loop counter
int wait = (1000);    // Delay between most recent pot adjustment and output
 
int checkSum     = 0; // Aggregate pot values
int prevCheckSum = 0;
int sens         = 3; // Sensitivity theshold, to prevent small changes in 
                      // pot values from triggering false reporting
// FLAGS
int PRINT = 1; // Set to 1 to output values
int DEBUG = 0; // Set to 1 to turn on debugging output
 
String ledName;
int led;
int ledCount = 0;
int ledPin; 
int oldLotteryVal =0; 
 
void setup()
{
  pinMode(redOut, OUTPUT);   // sets the digital pins as output
  pinMode(greenOut, OUTPUT);   
  pinMode(blueOut, OUTPUT); 
  Serial.begin(9600);     // Open serial communication for reporting
}
 
void loop()
{
  i += 1; // Count loop
 
  brightnessVal = analogRead(brightnessIn) / 4;  // read brightness input pins, convert to 0-255 scale
  blinkVal = analogRead(blinkIn);
  lotteryVal = analogRead(lotteryIn);  
 
  analogWrite(redOut, brightnessVal);    // Send new values to all LEDs
  analogWrite(greenOut, brightnessVal);
  analogWrite(blueOut, brightnessVal);
  
  if (lotteryVal - oldLotteryVal >100)
  {
    oldLotteryVal = lotteryVal;
    ledCount= (ledCount+1)%3;
  }
  if (ledCount ==0)
  {
    ledName = "red";
    ledPin = redOut;
  }
  else if (ledCount ==1)
  {
    ledName = "green";
    ledPin = greenOut;
  }
  else
  {
    ledName = "blue";
    ledPin = blueOut;
  }
 
    delay(blinkVal);
    analogWrite(ledPin, 0);
    delay(blinkVal);
 
      if (PRINT)                    // ...and if the PRINT flag is set...
      {
        Serial.print("Brightness: ");        // ...then print the values.
        Serial.print(brightnessVal);         
        Serial.print("\t"); 
        Serial.print("Blink delay: ");        
        Serial.print(blinkVal);
        Serial.print("\t");
        Serial.print("Lottery val: ");
        Serial.print(lotteryVal);
        Serial.print("\t");
        Serial.print("Which LED: ");                
        Serial.println(ledName); 
      }
}
 
0
Your rating: None
Drupal theme by Kiwi Themes.