Am working on my first project using the Arduino (or anything related), and have been running into a couple problems with the programming. But here is a quick description of what I'd like to do:
Want to have 4 RGB LEDs hooked up to a pot, where the pot determines the speed at which the LEDs cycle through pre-set colours.
Basically, am having trouble figuring out how to programme the LEDs to cylce through the colours I've set at random. I've been trying to piece together bits of code from here and there I think will work, not sure if you need it all but here it is so far (haven't started with the input stuff yet):
int potpin = 2;
// Output
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
// Color arrays
int darkGrey[3] = {
18, 31, 31 };
int midnightBlue[3] = {
10, 10, 44 };
int darkBlue[3] = {
28, 24, 55 };
int steelBlue[3] = {
27, 51, 71 };
int cadBlue[3] = {
37, 62, 63 };
int darkOlive[3] = {
33, 42, 18 };
int seaGreen[3] = {
18, 55, 34 };
int aqua[3] = {
27, 55, 46 };
int chart[3] = {
27, 55, 0 };
int golden[3] = {
86, 65, 13 };
int peru[3] = {
80, 52, 25 };
int darkOrange[3] = {
100, 55, 0 };
int choc[3] = {
100, 50, 14 };
int indianRed[3] = {
80, 36, 36 };
int fire[3] = {
70, 13, 13 };
int sadBrwn[3] = {
55, 27, 8 };
int darkORed[3] = {
55, 15, 0 };
int rosyBrwn[3] = {
73, 56, 56 };
int orchid[3] = {
55, 28, 54 };
int medGrey[3] = {
37, 28, 55 };
int thistle[3] = {
80, 71, 80 };
int dimWhite[3] = {
30, 30, 30 };
// etc.
// Set initial color
int redVal = dimWhite[0];
int grnVal = dimWhite[1];
int bluVal = dimWhite[2];
int wait = 10; // 10ms internal crossFade delay; increase for slower fades
int hold = 10; // Optional hold when a color is complete, before the next crossFade
int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial
int loopCount = 60; // How often should DEBUG report?
int repeat = 0; // How many times should we loop before stopping? (0 for no stop)
int j = 0; // Loop counter for repeat
// Initialize color variables
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;
// Set up the LED outputs
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
if (DEBUG) { // If we want to see values for debugging...
Serial.begin(9600); // ...set up the serial ouput
}
}
// Main program: list the order of crossfades
void loop()
{
crossFade(red);
crossFade(green);
crossFade(blue);
crossFade(yellow);
if (repeat) { // Do we loop a finite number of times?
j += 1;
if (j >= repeat) { // Are we there yet?
exit(j); // If so, stop.
}
}
}
int calculateStep(int prevValue, int endValue) {
int step = endValue - prevValue; // What's the overall gap?
if (step) { // If its non-zero,
step = 1020/step; // divide by 1020
}
return step;
}
int calculateVal(int step, int val, int i) {
if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
if (step > 0) { // increment the value if step is positive...
val += 1;
}
else if (step < 0) { // ...or decrement it if step is negative
val -= 1;
}
}
// Defensive driving: make sure val stays in the range 0-255
if (val > 255) {
val = 255;
}
else if (val < 0) {
val = 0;
}
return val;
}
void crossFade(int color[3]) {
// Convert to 0-255
int R = (color[0] * 255) / 100;
int G = (color[1] * 255) / 100;
int B = (color[2] * 255) / 100;
int stepR = calculateStep(prevR, R);
int stepG = calculateStep(prevG, G);
int stepB = calculateStep(prevB, B);
for (int i = 0; i <= 1020; i++) {
redVal = calculateVal(stepR, redVal, i);
grnVal = calculateVal(stepG, grnVal, i);
bluVal = calculateVal(stepB, bluVal, i);
analogWrite(redPin, redVal); // Write current values to LED pins
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
if (DEBUG) { // If we want serial output, print it at the
if (i == 0 or i % loopCount == 0) { // beginning, and every loopCount times
Serial.print("Loop/RGB: #");
Serial.print(i);
Serial.print(" | ");
Serial.print(redVal);
Serial.print(" / ");
Serial.print(grnVal);
Serial.print(" / ");
Serial.println(bluVal);
}
DEBUG += 1;
}
}
// Update current values for next loop
prevR = redVal;
prevG = grnVal;
prevB = bluVal;
delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
}
Thanks in advance!