Potentio Fun

Assignment: Sensing: Potentiometers

Collaborators:

Assignment: Sensing: Potentiometers
Collaborators:

Assignment: Sensing: Potentiometers
Collaborators:

 

Description:

In this lab I learned how to use potentiometers. I used them to control brightness and blinking of the

LEDs.

 

Materials Used:

3 pots

3 LEDs

assorted wires

3 resistors

 

Option 1:

One pot controls brightness, another pot controls blinking

 

As instructed, I accomplished this through use of the http://www.arduino.cc/en/Tutorial/AnalogInput Code.

 

(It is modestly modified...but overall I take no credit for the code...although I do follow what it

 

does.)

 

/*

* one pot dims, the other pot changes the blinking rate

* modification of the following

* http://www.arduino.cc/en/Tutorial/AnalogInput

*/

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 led1Pin = 9;   // select the pin for the LED 1

int led2Pin = 11;  // select the pin for the LED 2

void setup() {

pinMode(led1Pin, OUTPUT);  // declare the led1Pin as an OUTPUT

pinMode(led2Pin, OUTPUT);  // declare the led2Pin as an OUTPUT

}

void loop() {

//get analog input

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 blinking

//use the analog input to manipulate the LED

analogWrite(led2Pin, pot1Val/4); // dim LED to value from pot1 (divide by four to convert to 0-255)

delay(pot2Val);                  // stop the program for some time, meaning, LED is on for this time

analogWrite(led2Pin, 0);         // dim LED to completely dark (zero)

delay(pot2Val);                  // stop the program for some time, meaning, LED is OFF for this time

}

 

 

 

Option 2:

3 Pots for 3 LEDs (each pot controls brightness of RGB Hue / colors)

I took out the error checking etc because I don't use it.

 

/*

* Adapation of "Coffee-cup" Color Mixer:

*

*/

 

// Analog pin settings

int aIn = 0;    // Potentiometers connected to analog pins 0, 1, and 2

int bIn = 1;    //   (Connect power to 5V and ground to analog ground)

int cIn = 2;

 

// Digital pin settings

int aOut = 9;   // LEDs connected to digital pins 9, 10 and 11

int bOut = 10;  //   (Connect cathodes to digital ground)

int cOut = 11;

 

// Values

int aVal = 0;   // Variables to store the input from the potentiometers

int bVal = 0;

int cVal = 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 = 1; // Set to 1 to turn on debugging output

 

void setup()

{

pinMode(aOut, OUTPUT);   // sets the digital pins as output

pinMode(bOut, OUTPUT);

pinMode(cOut, OUTPUT);

Serial.begin(9600);     // Open serial communication for reporting

}

 

void loop()

{

i += 1; // Count loop

 

aVal = analogRead(aIn) / 4;  // read input pins, convert to 0-255 scale

bVal = analogRead(bIn) / 4;

cVal = analogRead(cIn) / 4;

 

analogWrite(aOut, aVal);    // Send new values to LEDs

analogWrite(bOut, bVal);

analogWrite(cOut, cVal);

}

 

 

 

Extra point:

 

Come up with an interesting mapping between the rotational position pot and LED output.

 

/*

* Adapation of "Coffee-cup" Color Mixer:

* The LED acts as an indicator (zero, dim, medium and bright)

* Rotating the pot will place the LED at one of four possible levels, rather than dimming on a gradient

*

*/

 

// Analog pin settings

int aIn = 0;    // Potentiometers connected to analog pins 0, 1, and 2

int bIn = 1;    //   (Connect power to 5V and ground to analog ground)

int cIn = 2;

 

// Digital pin settings

int aOut = 9;   // LEDs connected to digital pins 9, 10 and 11

int bOut = 10;  //   (Connect cathodes to digital ground)

int cOut = 11;

 

// Values

int aVal = 0;   // Variables to store the input from the potentiometers

int bVal = 0;

int cVal = 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 = 1; // Set to 1 to turn on debugging output

 

void setup()

{

pinMode(aOut, OUTPUT);   // sets the digital pins as output

pinMode(bOut, OUTPUT);

pinMode(cOut, OUTPUT);

Serial.begin(9600);     // Open serial communication for reporting

}

 

void loop()

{

i += 1; // Count loop

//we are going to play stop light

 

aVal = analogRead(aIn) / 4;  // read input pins, convert to 0-255 scale

bVal = analogRead(bIn) / 4;

cVal = analogRead(cIn) / 4;

 

//a

if ( aVal> 0 && aVal<85) {

analogWrite(aOut, 43);    // Send new values to LED

}

 

else if (aVal> 85 && aVal<170) {

analogWrite(aOut, 128);    // Send new values to LED

}

else if (aVal>170 && aVal<255){

analogWrite(aOut, 213);    // Send new values to LEDs

}

 

//b

if ( bVal> 0 && bVal<85) {

analogWrite(bOut, 43);    // Send new values to LED

}

 

else if (bVal> 85 && bVal<170) {

analogWrite(bOut, 128);    // Send new values to LED

}

else if (bVal>170 && bVal<255){

analogWrite(bOut, 213);    // Send new values to LEDs

}

 

//c

if ( cVal > 0 && cVal < 85) {

analogWrite(cOut, 43);    // Send new values to LED

}

else if (cVal > 85 && cVal < 170) {

analogWrite(cOut, 128);    // Send new values to LED

}

else if (cVal > 170 &&  cVal < 255){

analogWrite(cOut, 213);    // Send new values to LEDs

}

 

}