Description
First, I added three potentiometers to control the brightness of each LED individually. Next, I remapped the potentiometers. In this version, pot 2 controls "brightness" and pot 3 controls "step delay". Pot 1 is the One Knob that controls hue, by mapping a brightness function for each LED to pot 1. A sin function (offset for each LED) handles this nicely. "Brightness" is accomplished with a percentage multiplier, and "Step delay" is simply a delay between LED updates.
This project brought out some hardware limitations of Arduino. In the project's first iteration, each LED employed a complete set of calculations. This duplication slowed Arduino noticeably, to the extent that it was frustrating to use. Streamlining the script by consolidating calculations improved performance significantly.
Materials
- 3 LEDs
- 3 potentiometers
- 1 resistor
- wires
- Arduino
Code
/* * Pot 1 changes color of all three LEDs according to a sin function * Pot 2 changes brightness of all three LEDs * Pot 3 controls a variable "step delay" between updates */ //select pot input pins int pot1Pin = 0; int pot2Pin = 1; int pot3Pin = 2; //variables to store values from pots int pot1Val = 0; int pot2Val = 0; int pot3Val = 0; int pot2Adj = 0; //select LED output pins int led1Pin = 9; int led2Pin = 10; int led3Pin = 11; //offset for calculating pin colors int led2Offset = 2.1; // roughly 2π/3 int led3Offset = 4.2; // roughly 4π/3 int multiplier = 0; int pot1Rad = 0; void setup() { pinMode(led1Pin, OUTPUT); // declare the led1Pin as an OUTPUT pinMode(led2Pin, OUTPUT); // declare the led2Pin as an OUTPUT pinMode(led3Pin, OUTPUT); } void loop() { pot1Val = analogRead(pot1Pin); // read the value from pot 1, between 0 - 1024, for hue pot1Rad = pot1Val/163; // converts pot 1 value into a radian value (0-2π) pot2Val = analogRead(pot2Pin); // read the value from pot 2, between 0 - 1024, for dimming pot3Val = analogRead(pot3Pin); // read the value from pot 3, for blinking multiplier = 128*pot2Val/1024; // converts sin value to brightness, including dimming factor analogWrite(led1Pin, (sin(pot1Rad)+1)*multiplier); // dim LED to value from pot1 analogWrite(led2Pin, (sin(pot1Rad-led2Offset)+1)*multiplier); // dim LED to value from pot1 analogWrite(led3Pin, (sin(pot1Val-led3Offset)+1)*multiplier); // dim LED to value from pot1 delay(pot3Val); // stop the program for some time }