Lab 3 – Potentiometers

For the lab, I used two soldered potentiometers – used one for controlling brightness of LEDs and other for controlling the blink frequency.

Materials:

  • Jumper cables
  • Potentiometers (2)
  • Arduino
  • 3 LED’s
  • 3 Resistors
  • Breadboard

Code:

/*
* Serial RGB LED
* —————
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is “<colorCode(1)><colorCode(n)>”, where “colorCode” is
* one of “r”,”g”,or “b”.
* E.g. “rr” sets the red LED to 20% brightness.
* “gggg” sets the green LED to 40% brightness
* “bbb” sets the blue LED to 30% brightness
*
* Created 13 Sep 2016
*
*/

char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
// -> you must state how long the array will be else it won’t work properly
char colorCode;
int colorVal;

int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
int sensorPin_blink = A0; // Input pin for the potentiometer
int sensorPin_bright = A1;
int sensorValue_blink = 0;
int sensorValue_bright = 0;

void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 25);
analogWrite(greenPin, 25);
analogWrite(bluePin, 25);
//Serial.println(“enter color command (e.g. ‘r43’) :”);
}

void loop () {
//Serial.println(“in the loop”);
sensorValue_blink = analogRead(sensorPin_blink);
sensorValue_bright = analogRead(sensorPin_bright);
analogWrite(bluePin, sensorValue_bright/4);
analogWrite(redPin, sensorValue_bright/4);
analogWrite(greenPin, sensorValue_bright/4);
delay(sensorValue_blink);
analogWrite(bluePin, 0);
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
delay(sensorValue_blink);

}

Video – https://drive.google.com/a/berkeley.edu/file/d/0ByHw8c_nutT2QVhqN0tOQ0piYms/view?usp=sharing

Mood Jar

For “Mood Jar” I assembled a circuit that connected two potentiometers–one tall, one short–to three LEDs, and put the whole assembly into a jar sprayed with frosted glass spray paint. While I originally wanted to have rotating the lid of the jar activate one of the switches, and rotating the “cuff” of the lid activate the other, this proved difficult and so I did what felt like the right thing to do at the time: drilled holes.

The first potentiometer emerging from the first hole controls the color, which can be “any color” along the range of colors we produced last week with our inputs. The other dial controls blink speed. Overall I think it needs more frosting for diffusion, and given enough coats I think a solid diffusion could be accomplished.

 

Materials:

1 Jar

1 Arduino Micro

Wires

Solder/Soldering Iron

2 Potentiometers

3 Resistors (220 Ohms)

3 LEDs

Hot glue gun/hot glue

Frosted Glass spray paint

 

Code:

 * Modified version of: RGB Pot Mixer2
 * --------------
 *
 * Code for making one potentiometer control 3 LEDs, red, grn and blu, 
 * or one tri-color LED.
 *
 * Uses a purportedly correct algorithm for converting a hue number into RGB values
 * 
 * Code assumes you have the LEDs connected in a common-anode configuration,
 * with the LED's anode connected to +5V via a resistor and the cathode connected
 * to Arduino pins 9,10,11. 
 *
 * Tod E. Kurt &lt;tod@todbot.com&gt;, serial debug by Clay Shirky
 *
 */
// INPUT: Potentiometer should be connected to 5V and GND
int potPinColor = A4; // Potentiometer output connected to analog pin 3
int potPinBlink = A5; // Second potentiometer controlling blink speed
int potValColor = 0; // Variable to store the input from the potentiometer
int potValBlink = 0; // Variable to store the input from the second potentiometer (controlling blink)

// OUTPUT: Use digital pins 9-11, the Pulse-width Modulation (PWM) pins
// LED’s cathodes should be connected to digital GND
int redPin = 9; // Red LED, connected to digital pin 9
int grnPin = 10; // Green LED, connected to digital pin 10
int bluPin = 11; // Blue LED, connected to digital pin 11

// Program variables
int redVal = 0; // Variables to store the values to send to the pins
int grnVal = 0;
int bluVal = 0;

void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
}

// Main program
void loop()
{
potValColor = analogRead(potPinColor); // read the potentiometer value at the input pin
potValBlink = analogRead(potPinBlink);

if (potValColor < 341) // Lowest third of the potentiometer’s range (0-340)
{
potValColor = (potValColor * 3) / 4; // Normalize to 0-255

redVal = 256 – potValColor; // Red from full to off
grnVal = potValColor; // Green from off to full
bluVal = 1; // Blue off
}
else if (potValColor < 682) // Middle third of potentiometer’s range (341-681)
{
potValColor = ( (potValColor-341) * 3) / 4; // Normalize to 0-255

redVal = 1; // Red off
grnVal = 256 – potValColor; // Green from full to off
bluVal = potValColor; // Blue from off to full
}
else // Upper third of potentiometer”s range (682-1023)
{
potValColor = ( (potValColor-683) * 3) / 4; // Normalize to 0-255

redVal = potValColor; // Red from off to full
grnVal = 1; // Green off
bluVal = 256 – potValColor; // Blue from full to off
}
analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);
delay(potValBlink);
analogWrite(redPin, redVal*0); // Write values to LED pins
analogWrite(grnPin, grnVal*0);
analogWrite(bluPin, bluVal*0);
delay(potValBlink);
}

https://youtu.be/UEiwWYq9T2I

https://youtu.be/zKnCbmoYxjU

 

20160921_062336 20160921_055043

What’s your fortune?

Description:
In this one, I tried to create a pulsating effect (as in a fortune-teller’s crystal orb) with a ping-pong ball as a diffuser. I wanted to vary the heart-rate, the baseline intensity and the amplitude of the heartbeat using the potentiometers. I also had to be mindful of how bright my blue LED. One important lesson was to write the code from start to finish even if some of the code can be reused from another source on the internet.

Components
Cotton/wool
1 ping pong ball
3 potentiometers
3 LEDs (Red, Green and Blue)
3 Resistors (220 ohms each)
Arduino UNO
Breadboard and cables

Code


int heartRatePin = A0; // this controls tempo/delay
int heartRate = 0;

int beatWidthPin = A1; // this controls amplitude of the beat
int beatWidth = 0;

int intensityPin = A2; // this controls baseline brightness around which the amplitude is then set
int intensity = 0;

int crest = 0;
int trough = 0;

// initializing the LEDs
int ledPinGreen = 9;
int ledPinBlue = 10;
int ledPinRed = 11;

void setup(){
  // declaring pins as OUTPUTS
  pinMode(ledPinGreen, OUTPUT);
  pinMode(ledPinBlue, OUTPUT);
  pinMode(ledPinRed, OUTPUT);

  Serial.begin(9600); 
}

void driveColor(int pinColor, int c, int t, int hr){
  // this will be the cookie-cutter function for starting LEDs and then fading LEDs
  int startLED = t;
  // int difference = (c-t)/20;
  for (startLED = t; startLED <= c; startLED +=30){
    analogWrite(pinColor, startLED);
    delay(90);
  }
  delay(hr);
  for (startLED = c; startLED >= t; startLED -=30){
    analogWrite(pinColor, startLED);
    delay(90);
  }
  
//  analogWrite(pinColor, c); // don't launch this yet.
//  Serial.println("LED brightness value (crest): ");
//  Serial.println(c);
//  Serial.println("Heart Rate: ");
//  Serial.println(hr);
//  delay(hr);
//  analogWrite(pinColor, t);
//  Serial.println("LED brightness value (trough): ");
//  Serial.println(t);
//  Serial.println("\n");
//  delay(hr);
}

void loop(){
  intensity = analogRead(intensityPin);
  intensity = intensity/4; // intensity can now take values from 0 to 255
  beatWidth = analogRead(beatWidthPin);
  beatWidth = beatWidth/8; // reducing the domain for amplitudes
  // beatWidth can now only take values from 0 to ~32

  heartRate = analogRead(heartRatePin);
  heartRate = 2*heartRate;
  // this will be used across the LEDs to inform delay

  Serial.println("Intensity: ");
  Serial.println(intensity);
  Serial.println("Beat width: ");
  Serial.println(beatWidth);
//  Serial.println("Heart Rate: ");
//  Serial.println(heartRate);
//  Serial.println("--------------");

  
  crest = intensity + beatWidth;
  trough = intensity - beatWidth;

  // setting the wave limits
  // edge case truth table:
  // when both intensity and beat are MAXIMUM, 
    // crest goes beyond 255, trough is okay.
  // when both intensity and beat are MINIMUM,
    // crest is okay (extreme value = 0), but trough goes below 0.
  // when intensity is HIGH but beat is LOW,
    // crest can go beyond 255, trough is still okay
  // when intensity is LOW, but beat is HIGH,
    // crest is okay, but trough again goes below 0

  // Conclusion: handle cases for trough < 0 and crest going above 255
  // INDEPENDENTLY.

  if ((crest >=0) && (trough >=0)){
    if (crest < 255 ){
      if (trough > 0){
        driveColor(ledPinGreen, crest, trough, heartRate);
        delay(100);
        driveColor(ledPinBlue, crest/2, trough, heartRate);
        delay(100);
        driveColor(ledPinRed, crest, trough, heartRate);
        Serial.println(crest);
        Serial.println(trough);
      }
      else{
        driveColor(ledPinGreen, crest, 0, heartRate);
        delay(100);
        driveColor(ledPinBlue, crest/2, 0, heartRate);
        delay(100);
        driveColor(ledPinRed, crest, 0, heartRate);
        Serial.println(crest);
        Serial.println(0);
      }
    }

    else{
      // set your crests to 255 explicitly
      if (trough > 0){
        driveColor(ledPinGreen, 255, trough, heartRate);
        delay(100);
        driveColor(ledPinBlue, 255/2, trough, heartRate);
        delay(100);
        driveColor(ledPinRed, 255, trough, heartRate);
        Serial.println(255);
        Serial.println(trough);
      }
      else{
        driveColor(ledPinGreen, 255, 0, heartRate);
        delay(100);
        driveColor(ledPinBlue, 255/2, 0, heartRate);
        delay(100);
        driveColor(ledPinRed, 255, 0, heartRate);
        Serial.println(255);
        Serial.println(0);
      }
    }
    
  }

  else{

    //debugger
    Serial.println("Crests and Troughs are now: ");
    Serial.println(crest);
    Serial.println(trough);
    Serial.println("----------------------------");
    delay(2000);
  }
}

IMG_20160921_000834

Let there be light!

Description

I used 3 potentiometers to vary the brightness of 3 LEDs individually, wherein each LED can be controlled independently of the others. This setup enabled me to carry out some interesting effects with dimming and rising of the LEDs. The video can be seen here – https://goo.gl/photos/y1YiUhkJ8XSZjCb58

Components

1 Arduino Uno
3 LEDs
3 220Ω Resistors
3 potentiometers
1 Breadboard
Jumper wires
USB cable

int potPin = 0;
int potPin1 = 1;
int potPin2 = 2; // Analog input pin that the potentiometer is attached to
int potValue = 0;
int potValue1 = 0;
int potValue2 = 0; // value read from the pot
int led = 9;
int led1 = 10; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9
int led2 = 11;

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// declare the led pin as an output:
pinMode(led, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}

void loop() {
potValue = analogRead(potPin);
potValue1 = analogRead(potPin1);
potValue2 = analogRead(potPin2); // read the pot value
analogWrite(led, potValue/4);
analogWrite(led1, potValue1/4);
analogWrite(led2, potValue2/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
Serial.println("hello"); // print the pot value back to the debugger pane
delay(10); // wait 10 milliseconds before the next loop
}

IMG_20160920_230003      IMG_20160920_230144      IMG_20160920_231022

Blinking and dimming LEDs

Components

  • 1 Arduino Uno
  • 3 220 ohm resistors
  • 3 LEDs (one red, one green, one blue)
  • 2 potentiometers
  • 1 breadboard

Description

Make lights blink faster or slower by turning one of the pots. Make the lights dim by turning one of the pots.

Code

int sensorPin = A0; // select the input pin for the potentiometer
int blueLedPin = 11; // select the pin for the LED
int greenLedPin = 10;
int redLedPin = 9;
int sensorValue = 0; // variable to store the value coming from the sensor

int potPin = A1; // Analog input pin that the potentiometer is attached to
int potValue = 0; // value read from the pot
//int led = 9; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9

void setup() {
 // declare the ledPin as an OUTPUT:
 pinMode(blueLedPin, OUTPUT);
 pinMode(redLedPin, OUTPUT);
 pinMode(greenLedPin, OUTPUT);

 // initialize serial communications at 9600 bps:
 Serial.begin(9600);
 // declare the led pin as an output:
// pinMode(led, OUTPUT);
}

void loop() {
 // read the value from the sensor:
 sensorValue = analogRead(sensorPin);
 potValue = analogRead(potPin); // read the pot value
 // turn the ledPin on
 analogWrite(blueLedPin, potValue/4);
 analogWrite(redLedPin, potValue/4);
 analogWrite(greenLedPin, potValue/4);
 // stop the program for &lt;sensorValue&gt; milliseconds:
 delay(sensorValue);
 // turn the ledPin off:
 analogWrite(blueLedPin, 0);
 analogWrite(redLedPin, 0);
 analogWrite(greenLedPin, 0);
 // stop the program for for &lt;sensorValue&gt; milliseconds:
 delay(sensorValue);
}

<img class="alignnone size-medium wp-image-1012" src="http://blogs.ischool.berkeley.edu/i262/files/2016/09/IMG_7357-300x225.jpg" alt="IMG_7357" width="300" height="225" />

Lab 3 – Potentiometers

For the lab, I used two soldered potentiometers – used one for controlling brightness of LEDs and other for controlling the blink frequency.

Materials:

  • Jumper cables
  • Potentiometers (2)
  • Arduino
  • 3 LED’s
  • 3 Resistors
  • Breadboard

Code:

/*
* Serial RGB LED
* —————
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is “<colorCode(1)><colorCode(n)>”, where “colorCode” is
* one of “r”,”g”,or “b”.
* E.g. “rr” sets the red LED to 20% brightness.
* “gggg” sets the green LED to 40% brightness
* “bbb” sets the blue LED to 30% brightness
*
* Created 13 Sep 2016
*
*/

char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
// -> you must state how long the array will be else it won’t work properly
char colorCode;
int colorVal;

int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
int sensorPin_blink = A0; // Input pin for the potentiometer
int sensorPin_bright = A1;
int sensorValue_blink = 0;
int sensorValue_bright = 0;

void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 25);
analogWrite(greenPin, 25);
analogWrite(bluePin, 25);
//Serial.println(“enter color command (e.g. ‘r43’) :”);
}

void loop () {
//Serial.println(“in the loop”);
sensorValue_blink = analogRead(sensorPin_blink);
sensorValue_bright = analogRead(sensorPin_bright);
analogWrite(bluePin, sensorValue_bright/4);
analogWrite(redPin, sensorValue_bright/4);
analogWrite(greenPin, sensorValue_bright/4);
delay(sensorValue_blink);
analogWrite(bluePin, 0);
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
delay(sensorValue_blink);

}

Pic – https://drive.google.com/a/berkeley.edu/file/d/0ByHw8c_nutT2ZURjSVdjeVFXV00/view?usp=sharing

Video – https://drive.google.com/a/berkeley.edu/file/d/0ByHw8c_nutT2QVhqN0tOQ0piYms/view?usp=sharing

 

 

Lab 3 – Arduino LED with Potentiometer

Components
• 1 Arduino Uno
• 3 resistor (220 ohms)
• 2 potentiometers
• 3 LEDs (red, blue, green)
• 1 breadboard

Description
I have continued with the color fading code from Lab2. I chose option 1 where I have one pot controlling the brightness and another pot controlling the delay/wait time for the fading speed. For the pot that’s controlling the brightness, I have used the (pot value/4) as an additional parameters that will be added or subtracted from the fading loop. I’m aware that it will also increase or slow the speed of fading, which might conflict with the wait time control. However, it adds a bit more unpredictability and surprises to the results. Playing around both the delay pot and the brightness pot, I can sometimes get a very fast fading temple with low brightness or a slow fading temple with high brightness.

Youtube Video Link
Arduino LED with Potentiometer

Code



/*
* Code for cross-fading 3 LEDs, red, green and blue, or one tri-color LED, using PWM
* The program cross-fades slowly from red to green, green to blue, and blue to red
* The debugging code assumes Arduino 0004, as it uses the new Serial.begin()-style functions
* Clay Shirky <clay.shirky@nyu.edu> 
*/

char b_input[100];
int b_value = 0;

// Output
int greenPin = 9;  // Green LED, connected to digital pin 9
int bluePin  = 10;  // Blue LED,  connected to digital pin 10
int redPin   = 11;   // Red LED,   connected to digital pin 11

// Program variables
int redVal   = 1; // Variables to store the values to send to the pins
int greenVal = 1;   // Initial values are Red full, Green and Blue off
int blueVal  = 255;

// Sensor Setup
int sensorPin0 = A0;    // select the input pin for the potentiometer
int sensorPin1 = A1;    // select the input pin for the potentiometer
int sensorValue0 = 0;  // variable to store the value coming from the sensor
int sensorValue1 = 0;  // variable to store the value coming from the sensor

int i = 0;     // Loop counter    
int wait = 100; // 50ms (.05 second) delay; shorten for faster fades
int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial

void setup()
{
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);         // If we want to see the pin values for debugging...
    Serial.begin(9600);  // ...set up the serial ouput on 0004 style
    Serial.println("just continue to send me b"); 
}

// Main program
void loop()
{
   // read the value from the sensor:
  sensorValue0 = analogRead(sensorPin0);
  sensorValue1 = analogRead(sensorPin1);  

  i += 20;      // Increment counter
  if (i < 255) // First phase of fades
  {
    greenVal += 1 + sensorValue1/4; // Green up
    redVal -= 1 + sensorValue1/4; // Red low
    blueVal = 1 + sensorValue1/4;
  }
  else if (i < 509) // Second phase of fades
  {
    greenVal -= 1 + sensorValue1/4; // Green down
    redVal  = 1 + sensorValue1/4; // red low
    blueVal += 1 + sensorValue1/4; // Blue up
  } 
  else if (i < 763) // Third phase of fades
  {
    greenVal = 1 + sensorValue1/4; // Green low
    redVal += 1 + sensorValue1/4; // Red down
    blueVal -= 1 + sensorValue1/4; // Blue up
  }
  else // Re-set the counter, and start the fades again
  {
    i = 1;
  }  
  memset(b_input, 0, 100);   
      
  readSerialString(b_input);

  if(b_input[0] == 'b'){
    b_value = b_value + 1;
    Serial.println(b_value); 
  }
  if (b_value == 13) {
    b_value = 0;
  }
  
  analogWrite(redPin,   redVal);   // Write current values to LED pins
  analogWrite(greenPin, greenVal); 
  analogWrite(bluePin,  blueVal + b_value*20);  

  Serial.print("red increment: wait time");
  Serial.println(sensorValue0);
  Serial.print("red increment: brightness");
  Serial.println(sensorValue1);

  delay(sensorValue0); // Pause for 'wait' milliseconds before resuming the loop

  
//  if (DEBUG) { // If we want to read the output
//    DEBUG += 1;     // Increment the DEBUG counter
//    if (DEBUG > 10) // Print every 10 loops
//    {
//      DEBUG = 1;     // Reset the counter
//
//      Serial.print(i);       // Serial commands in 0004 style
//      Serial.print("\t");    // Print a tab
//      Serial.print("R:");    // Indicate that output is red value
//      Serial.print(redVal);  // Print red value
//      Serial.print("\t");    // Print a tab
//      Serial.print("G:");    // Repeat for green and blue...
//      Serial.print(greenVal);
//      Serial.print("\t");    
//      Serial.print("B:");    
//      Serial.println(blueVal); // println, to end with a carriage return
//    }
//  }
}

void readSerialString (char *strArray) {
  int i = 0;
  if(!Serial.available()) {
    return;
  }
  while (Serial.available()) {
    strArray[i] = Serial.read();
    //Serial.print(strArray);
    i++;
  }
}

RGB Fade with Potentiometers

I did Option 2 with a few modifications. I had the potentiometers control the rate of change of the red, green and blue LEDs. Turning the potentiometer to zero would therefore “freeze” the color at whatever value it was at before the knob was fully turned to zero.

I also replaced the cross-fading code so that the red, green and blue LEDs were no longer in lock-step. Depending on the increment rate set by the knobs, each color of LED could overtake the other colors, depending on how quickly each LED hit the bounds set by the function. I.e. the sin/cos curve representing each function can be overlaid over each other with different shifts along the x-axis. (For example, you could theoretically line up all curves exactly with careful adjustment of the knobs, which would remove any color-mixing effect.)

I also noticed some blinking on my LEDs, which I troubleshooted as being caused by the LEDs being sent invalid values, such as -1 or 256. I set fatter bounds on both sides to avoid this blinking and for a smooth transition.

/*
* Andrew Chong
* Alternating RGB Fade with Potentiometers
* Modified from Clay Shirky <clay.shirky@nyu.edu> 
*/

// Output
int redPin   = 9;   // Red LED,   connected to digital pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 11;  // Blue LED,  connected to digital pin 11

float red_direction = 1; 
float blue_direction = 1; 
float green_direction = 1; 

// brightness 
float blue_brightness = 0; 
float red_brightness = 0;
float green_brightness = 0; 

// Program variables
float redVal   = 255; // Variables to store the values to send to the pins
float greenVal = 255;   
float blueVal  = 0;

int i = 0;     // Loop counter    
int j = 0; // display counter 
int wait = 50; // 50ms (.05 second) delay; shorten for faster fades
int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial

void setup()
{
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);         // If we want to see the pin values for debugging...
    Serial.begin(9600);  // ...set up the serial ouput on 0004 style
}

// Main program
void loop()
{
  blue_brightness = analogRead(A0)*3/(float)1023;
  red_brightness =  analogRead(A5)*3/(float)1023;
  green_brightness =  analogRead(A1)*3/(float)1023;
  i += 5;      // Increment counter
  j += 1; 

  if (redVal>=250) {
    red_direction = -1; 
  }
  if (redVal<=4) {
    red_direction = 1; 
  }

  if (blueVal>=250) {
    blue_direction = -1;
  }
  if (blueVal<=4) {
    blue_direction = 1; 
  }

  if (greenVal>=250) {
    green_direction = -1;  
  }
  if (greenVal<=4) {
    green_direction = 1; 
  }

    redVal += red_direction*red_brightness;
    greenVal += green_direction*green_brightness; 
    blueVal  += blue_direction*blue_brightness; 

  if (j > 20) {
    j = 1;
  Serial.println(""); 
  Serial.print("red increment:");
  Serial.println(red_brightness);

  Serial.print("blue increment:");
  Serial.println(blue_brightness);

  Serial.print("green increment:");
  Serial.println(green_brightness);
  };

  //Serial.println("red:");
  //Serial.println("red:");
  
  analogWrite(redPin, redVal); 
  analogWrite(greenPin, greenVal); 
  analogWrite(bluePin,  blueVal);  

if (DEBUG) { // If we want to read the output
    DEBUG += 1;     // Increment the DEBUG counter
    if (DEBUG > 20) // Print every 10 loops
    {
      DEBUG = 1;     // Reset the counter
      Serial.print(i);       // Serial commands in 0004 style
      Serial.print("\t");    // Print a tab
      Serial.print("R:");    // Indicate that output is red value
      Serial.print(redVal);  // Print red value
      Serial.print("\t");    // Print a tab
      Serial.print("G:");    // Repeat for green and blue...
      Serial.print(greenVal);
      Serial.print("\t");    
      Serial.print("B:");    
      Serial.println(blueVal); // println, to end with a carriage return
    }
  }

  delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}

Here is a short video showing how the knobs can be adjusted (knob adjustment out of shot).

DJ Spectrumizer

Components
• 1 Arduino Uno
• 3 resistor (220 ohms)
• 3 potentiometers
• 3 LEDs (red, blue, green)
• 1 breadboard

Description
Welcome to DJ Spectrumizer. This lab project involves three potentiometers that control LEDs in three ways

  1. To control the color in the spectrum (Hue)
  2. To control the brightness of the light ( Vue)
  3. To control the rate at which they blink . (Beats)

Extra Credit :

A combination of these controls enables a person using the DJ Spectrumizer to set the mood of the party with the color and brightness and make the color blink based on the beats of the song he/she is playing.

I researched about the range of beats a song can have and found out that it ranges from 40bpm to 220bpm. I have used the potentiometer to map the values to this.

Code

 

// The three primary colour LEDs, driven as analgue outputs (actually PWM, but
// close enough for our analogue eyes).

const int ledPinRed = 11; // Red LED connected to analogue out pin
const int ledPinGrn = 10; // Green LED connected to analogue out pin
const int ledPinBlu = 9; // Blue LED connected to analogue out pin

// The Brightness potentiometer goes on an analogue pin, taking the pin from
// 0V to 5V.

const int potPinSat = 1;

// The Hue potentiometer goes on an analogue pin, taking the pin from
// 0V to 5V.

const int potPinHue = 2;

// The Beats potentiometer goes on an analogue pin, taking the pin from
// 0V to 5V.

const int potPinBeats = 3;

// Constants to define the ranges.

const int hueRedLow = 0;
const int hueRedHigh = 255;
const int hueBlue = 170;

// The size of the angle of one sector (1/6 of a colour wheel), and of a complete
// cycle of the colour wheel.

const int angleMin = 0;
const int angleSector = 60;
const int angleMax = 360;

const int brightMin = 0;
const int brightMax = 255;

//constants to define the range of beats
const int beatsMin = 40;
const int beatsMax = 220;

// Work variables.

// The hue potentiometer value is mapped to the range 0 to 360 (degrees).
int potValueHue;

//The brightness potentiometer value is mapped from 0 to 255 (full brightness)
int potValueSat;

//The beats potentiometer value is mapped from 40 to 220
int potValueBeats;

int hue, brightness;

const int saturation = 255;

// The brightess of each LED (0 to 255).

unsigned int r, g, b;

void setup() {
// Still need to set a baud rate, even for USB.
Serial.begin(9600);

// Set LED pins to output.
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinGrn, OUTPUT);
pinMode(ledPinBlu, OUTPUT);

// Poteniometer analogue pin is an input.
pinMode(potPinHue, INPUT);
pinMode(potPinSat, INPUT);
pinMode(potPinBeats, INPUT);
}

void loop() {

// The Hue potentiometer value is mapped to degrees - 0 to 360 - for convenience.
potValueHue = map(analogRead(potPinHue), 0, 1023, angleMin, angleMax);
// The Brightness potentiometer value is mapped to values from 0 to 255.
potValueSat = map(analogRead(potPinSat), 0, 1023, brightMin, brightMax);
//The beats potentiometer value is mapped to values from 40 to 220 bpm
potValueBeats = map(analogRead(potPinBeats),0,1023, beatsMin,beatsMax);

// Colour wheel mode (red to red, wrapped around in a cycle).
hue = map(potValueHue, angleMin, angleMax, hueRedLow, hueRedHigh);
brightness = potValueSat;

// Do the conversion.
HSBToRGB(hue, saturation, brightness, &amp;r, &amp;g, &amp;b);

analogWrite(ledPinRed, r);
analogWrite(ledPinGrn, g);
analogWrite(ledPinBlu, b);

// converting beats per minute to delay
int bpmToDelay = 1000 / (220/60);
delay(bpmToDelay);
}

// This function taken from here:
// http://eduardofv.com/read_post/179-Arduino-RGB-LED-HSV-Color-Wheel-

void HSBToRGB(
unsigned int inHue, unsigned int inSaturation, unsigned int inBrightness,
unsigned int *oR, unsigned int *oG, unsigned int *oB )
{
if (inSaturation == 0)
{
// achromatic (grey)
*oR = *oG = *oB = inBrightness;
}
else
{
unsigned int scaledHue = (inHue * 6);
unsigned int sector = scaledHue &gt;&gt; 8; // sector 0 to 5 around the color wheel
unsigned int offsetInSector = scaledHue - (sector &lt;&lt; 8); // position within the sector unsigned int p = (inBrightness * ( 255 - inSaturation )) &gt;&gt; 8;
unsigned int q = (inBrightness * ( 255 - ((inSaturation * offsetInSector) &gt;&gt; 8) )) &gt;&gt; 8;
unsigned int t = (inBrightness * ( 255 - ((inSaturation * ( 255 - offsetInSector )) &gt;&gt; 8) )) &gt;&gt; 8;

switch( sector ) {
case 0:
*oR = inBrightness;
*oG = t;
*oB = p;
break;
case 1:
*oR = q;
*oG = inBrightness;
*oB = p;
break;
case 2:
*oR = p;
*oG = inBrightness;
*oB = t;
break;
case 3:
*oR = p;
*oG = q;
*oB = inBrightness;
break;
case 4:
*oR = t;
*oG = p;
*oB = inBrightness;
break;
default: // case 5:
*oR = inBrightness;
*oG = p;
*oB = q;
break;
}
}
}
potLab

Could not show it properly in a Video. Will demonstrate it in the class.

Pots, pots, pots

Components
• 1 Arduino Uno
• 3 resistor (220 ohms)
• 3 pots
• 3 LEDs (red, blue, green)
• 1 breadboard
• 1 USB cable
• Jumper wires
• Laptop

Description
For this lab I used my Arduino Uno board to control the brightness of the three LEDs, by manipulating 3 pots.

Code

int sensorRed = A0;    // select the input pin for the potentiometer 1

int sensorBlue = A1;    // select the input pin for the potentiometer 2

int sensorGreen = A2;    // select the input pin for the potentiometer 3

int redPin = 9;        // select the pin for the red LED

int bluePin = 10;      // select the pin for the blue LED

int greenPin = 11;     // select the pin for the green LED

int redValue=0;

int blueValue=0;

int greenValue=0;

void setup() {

// declare the ledPin as an OUTPUT:

pinMode(redPin, OUTPUT);

pinMode(bluePin, OUTPUT);

pinMode(greenPin, OUTPUT);

}

void loop() {

// read the values from the sensors:

redValue = analogRead(sensorRed);

blueValue = analogRead(sensorBlue);

greenValue = analogRead(sensorGreen);

redValue=(redValue/4);

blueValue=(blueValue/4);

greenValue=(greenValue/4);

analogWrite(redPin, redValue);

analogWrite(bluePin, blueValue);

analogWrite(greenPin, greenValue);

}

20160920_205043