Dimming and scales

Components:

  • Arduino Uno
  • Breadboard
  • 3x potentiometers
  • 3x LEDs (red, green, blue)
  • 3x 220Ω resistors
  • 18x wires
  • 1 USB cable
  • 1 Mac computer


Option 2:

I added my three pots to the breadboard, such that each pot was synced up with an individual LED, and to digital pins in the arduino. This round of code allows for each pot to control the brightness of a single LED. Note that because analogRead reads analog voltage (max value of 1023), while analogWrite writes a digital voltage (max value of 255), we have to divide the analog value by 4 to map to digital.

Photo of LEDs with different levels of brightness


CODE:

int potPin1 = A0;
int potPin2 = A2;
int potPin3 = A3; // Analog input pin that the potentiometer is attached to
int potValue1 = 0;
int potValue2 = 0;
int potValue3 = 0; // value read from the pot
int blueLed = 9; 
int redLed = 10;
int greenLed = 11; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9
 
void setup() {
 // initialize serial communications at 9600 bps:
 Serial.begin(9600);
 // declare the led pin as an output:
 pinMode(blueLed, OUTPUT);
 pinMode(redLed, OUTPUT);
 pinMode(greenLed, OUTPUT);
}
 
void loop() {
 potValue1 = analogRead(potPin1);
 potValue2 = analogRead(potPin2);
 potValue3 = analogRead(potPin3); // read the pot value
 analogWrite(blueLed, potValue1/4);
 analogWrite(redLed, potValue2/4);
 analogWrite(greenLed, potValue3/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
}


Extra Credit:

Each LED is are hooked up to its own Pot, which is plugged into analog pins.  This code turns the red LED on first, followed by the blue and then green, with the green then turning off first, then the blue, and back to the red turning on and off in an ascending and descending sequence, similar to playing musical scales on a piano. Each LED’s pot determines how quickly that LED will blink, which allows for the creation of interesting patterns.

Watch the ascending and descending light sequence!


CODE

int sensorPin1 = A0;
int sensorPin2 = A2;
int sensorPin3 = A3; // select the input pin for the potentiometer
int redledPin = 13;
int blueledPin = 8;
int greenledPin = 12;// select the pin for the LED
int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0; // variable to store the value coming from the sensor

void setup() {
 // declare the ledPin as an OUTPUT:
 pinMode(redledPin, OUTPUT);
 pinMode(blueledPin, OUTPUT);
 pinMode(greenledPin, OUTPUT);
}

void loop() {
 // read the value from the sensor:
 sensorValue1 = analogRead(sensorPin1);
 sensorValue2 = analogRead(sensorPin2);
 sensorValue3 = analogRead(sensorPin3);
 
 // turn the ledPin on
 digitalWrite(redledPin, HIGH);
 // turn the ledPin off:
 delay(sensorValue1);
 digitalWrite(blueledPin, HIGH);
 delay(sensorValue2);
 digitalWrite(greenledPin, HIGH);
 delay(sensorValue3);
 
 digitalWrite(greenledPin, LOW);
 delay(sensorValue3);
 digitalWrite(blueledPin, LOW);
 delay(sensorValue2);
 digitalWrite(redledPin, LOW);
 delay(sensorValue1);
}

Leave a Reply