Developing on my previous project, I made a circuit in which the color of the RGB led can be smoothly changed (one color turns into the other, basically covering all possible colors that could be displayed).
Instead of the three buttons, now I used one single control item, a flex sensor. For developing my code, I now used another part of the same SIK Guide example. I took the method showRGB() to make the LED light display different colors. In this function, each color is assigned to a number from 0 to 676. As it is explained in the example code: “0 = pure red, 255 = pure green, 511 = pure blue, 767 = pure red (again), and numbers between the above colors will create blends”. (How this exactly works can easily be seen from my codes and the comments I kept.) The way this function is used with the flex sensor is that values from the sensor (which range from about 635 to 980) are read and mapped to numbers ranging from 0 to 767. By setting the map fuction to map values from 0 to 1023, we can change our sensor to a potentiometer
You can see my code here:
const int RED_PIN = 9; const int GREEN_PIN = 10; const int BLUE_PIN = 11; int x; void setup() { pinMode(RED_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); Serial.begin(9600); //first used to monitor the values form the sensor, then to monitor x } void loop() { x = map (analogRead(A0), 630, 1000, 0, 767); //for a flex sensor //or for a potentiometer: x = map (analogRead(A0), 630, 1000, 0, 767) Serial.println(x); showRGB(x); } // showRGB() // This function translates a number between 0 and 767 into a // specific color on the RGB LED. If you have this number count // through the whole range (0 to 767), the LED will smoothly // change color through the entire spectrum. // The "base" numbers are: // 0 = pure red // 255 = pure green // 511 = pure blue // 767 = pure red (again) // Numbers between the above colors will create blends. For // example, 640 is midway between 512 (pure blue) and 767 // (pure red). It will give you a 50/50 mix of blue and red, // resulting in purple. void showRGB(int color) { int redIntensity; int greenIntensity; int blueIntensity; // Here we'll use an "if / else" statement to determine which // of the three (R,G,B) zones x falls into. Each of these zones // spans 255 because analogWrite() wants a number from 0 to 255. // In each of these zones, we'll calculate the brightness // for each of the red, green, and blue LEDs within the RGB LED. if (color <= 255) // zone 1 { redIntensity = 255 - color; // red goes from on to off greenIntensity = color; // green goes from off to on blueIntensity = 0; // blue is always off } else if (color <= 511) // zone 2 { redIntensity = 0; // red is always off greenIntensity = 255 - (color - 256); // green on to off blueIntensity = (color - 256); // blue off to on } else // color >= 512 // zone 3 { redIntensity = (color - 512); // red off to on greenIntensity = 0; // green is always off blueIntensity = 255 - (color - 512); // blue on to off } // Now that the brightness values have been set, command the LED // to those values analogWrite(RED_PIN, redIntensity); analogWrite(BLUE_PIN, blueIntensity); analogWrite(GREEN_PIN, greenIntensity); }