RGB-led cycle colours of the rainbow arduino - arduino

I am fairly new to programming and have bought an arduino uno for practice. I want to make an RGB-led light up circling the colours of the rainbow (cyan - blue - magenta etc..)
There is some sort of logical problem with my code. The first circle is fine, it goes through the colors nicely. It is in the second circle that the red color starts acting up and flashes 9 times when it is not supposed to in the "blue segment of the circle".
My whole code is like this:
int BLUEARR[] = {1,1,1,0,0,0};
int GREENARR[] = {1, 0, 0, 0, 1, 1};
int REDARR[] = {0, 0, 1, 1, 1, 0};
int red, green, blue;
int RedPin = 9;
int GreenPin = 10;
int BluePin = 11;
void setup() {
}
void loop() {
for (int i=0; i<7; i++) {
int k = (i+1)%6;
for (int j=0; j<256; j++) {
blue = BLUEARR[i]*255+(BLUEARR[k] - BLUEARR[i])*j;
red = REDARR[i]*255+(REDARR[k] - REDARR[i])*j;
green = GREENARR[i]*255+(GREENARR[k] - GREENARR[i])*j;
analogWrite (RedPin, red);
analogWrite (GreenPin, green);
analogWrite (BluePin, blue);
delay(20);
}
}
}
anyone knows? Kind regards.

Related

Optimize code with predefined variables with same prefix

I'm well on my way on learning coding for Arduino.
For the following case I Googled my ass off, but can't find any helpful answer.
So, asking here. Maybe you can help me with some terminology to Google, or direct links where this is elaborated.
I want to compress the following code. The predefined colors have to be as they are.
All variables have the same prefix, namely COLOR_. Is there any way to compress the part that is in the setup?
See following link.
// Define the colors of remote control in RGB values
// R G B
#define COLOR_0 { 0, 0, 0}
#define COLOR_1 {255, 0, 0} // Red
#define COLOR_2 {255, 160, 25} // Orange
#define COLOR_3 {255, 255, 0} // Yellow
#define COLOR_4 {127, 255, 127} // Pink
#define COLOR_5 { 0, 255, 0} // Green
#define COLOR_6 { 0, 255, 255} // Cyan
#define COLOR_7 { 0, 127, 255} // Light blue
#define COLOR_8 { 0, 0, 255} // Blue
#define COLOR_9 {191, 0, 255} // light green
uint16_t RGB_565(uint8_t red, uint8_t green, uint8_t blue){
// 16-bit value to express the RGB565-color:
// |R|R|R|R|R|G|G|G|G|G|G|B|B|B|B|B|
uint16_t red_565 = red / 8;
uint16_t green_565 = green / 4;
uint16_t blue_565 = blue / 8;
uint16_t mixed_565 = (red_565 << 11) | (green_565 << 5) | blue_565;
return mixed_565;
}
void setup(){
// Convert the preset colors to a RGB565-value
const uint8_t tint_1[] = COLOR_1;
uint16_t color_1 = RGB_565(tint_1[0], tint_1[1], tint_1[2]);
const uint8_t tint_2[] = COLOR_2;
uint16_t color_2 = RGB_565(tint_2[0], tint_2[1], tint_2[2]);
const uint8_t tint_3[] = COLOR_3;
uint16_t color_3 = RGB_565(tint_3[0], tint_3[1], tint_3[2]);
const uint8_t tint_4[] = COLOR_4;
uint16_t color_4 = RGB_565(tint_4[0], tint_4[1], tint_4[2]);
// etc...
// This part should be able to be compressed
}
void loop(){
// Not relevant
}
What about
#define COLOR_1 255, 0, 0
Then simply call
RGB_565(COLOR_1);

Arduino FastLED: Trying to group LEDs into pairs of two

I have a strip of leds cut into pairs; each pair is in its own lantern, and I want the lanterns to alternate whatever I put through them.
I am using FastLED library. I have posted what I have come up with. It seems to work in the console; the arrays generated are right(or seem to be)' even[] = {0,1,4,5,8,9...}; odd[] = {2,3,6,7,10,11...}' but lantern #1 does not work as the rest do for some reason.
lantern #1 = leds[even[0]] and leds[even[1]] should both display Eval color but leds[even[1]] is displaying Oval color.
the other lanterns display properly
lantern #2 = leds[odd[0]] and leds[odd[1]] displays Oval color.
lantern #3 = leds[even[2]] and leds[even[3]] displays Eval color.
lantern #4 = leds[odd[2]] and leds[odd[3]] displays Oval color.
etc...
#include <FastLED.h>
#define NUM_LEDS 12
#define DATA_PIN 3
#define POT_PIN A0
int potValue;
int even[NUM_LEDS]; //{0,1,4,5,8,9...}
int odd[NUM_LEDS]; //{2,3,6,7,10,11...}
int Epos = 0; //count position for adding lanterns to even/odd arrays
int Opos = 0;
int lantern = 1; // 1 and 2 switch back and forth between lanterns
int pos = 0; // count 2 leds per lantern
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
for (int i=0;i<NUM_LEDS;i++){ //go through leds
if (pos == 2){
pos = 0;
if (lantern == 1){
lantern = 2;
}
else if (lantern == 2){
lantern = 1;
}
}
switch (lantern){
case 1: // even lantern
even[Epos] = i; // i = led on strip
Epos++;
pos++;
break;
case 2: // odd lantern
odd[Opos] = i; // i = led on strip
Opos++;
pos++;
break;
}
}
Serial.print("\n even: ");
for (int x=0;x<NUM_LEDS;x++){
Serial.print(even[x]);
}
Serial.print("\n odd: ");
for (int x=0;x<NUM_LEDS;x++){
Serial.print(odd[x]);
}
}
//////////////////////////////////////////////////////////////////////////
void split(){
potValue = analogRead(POT_PIN);
int Eval;
int Oval;
int Eval = potValue; //color value for even lanterns
int Oval = map(potValue,0,1023, 10,500); //offset color value for odd lanterns
for (int i=0;i<NUM_LEDS;i++){
leds[even[i]] = CHSV(Eval,255,255);
delay(1);
leds[odd[i]] = CHSV(Oval,255,255);
}
FastLED.show();
}
///////////////////////////////////////////////////////////////////////////
void loop() {
split();
}
I am pretty new to Arduino.have I done something wrong? Is this an efficient way to go about this?

Picking the dominant color in Qt

How can I get the dominant color of a QImage or QIcon in Qt5 ?
Does it need generating a histogram manually ?
And if yes , how ?
You can't get this data via Qt.
But I found for You this web page (http://robocraft.ru/blog/computervision/1063.html).
On this web page some guy wrote code for your question! But all description only on Russian language. I translated it for you.
He is using OpenCV lib.
//
// determination of the prevailing colors in the image
// via k-Means
//
#include <cv.h>
#include <highgui.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
// getting an image pixel (by type of image and coordinates)
#define CV_PIXEL(type,img,x,y) (((type*)((img)->imageData+(y)*(img)->widthStep))+(x)*(img)->nChannels)
const CvScalar BLACK = CV_RGB(0, 0, 0); // black
const CvScalar WHITE = CV_RGB(255, 255, 255); // white
const CvScalar RED = CV_RGB(255, 0, 0); // red
const CvScalar ORANGE = CV_RGB(255, 100, 0); // orange
const CvScalar YELLOW = CV_RGB(255, 255, 0); // yellow
const CvScalar GREEN = CV_RGB(0, 255, 0); // green
const CvScalar LIGHTBLUE = CV_RGB(60, 170, 255); // blue
const CvScalar BLUE = CV_RGB(0, 0, 255); // blue 2
const CvScalar VIOLET = CV_RGB(194, 0, 255); // Violet
const CvScalar GINGER = CV_RGB(215, 125, 49); // redhead
const CvScalar PINK = CV_RGB(255, 192, 203); // pink
const CvScalar LIGHTGREEN = CV_RGB(153, 255, 153); // light green
const CvScalar BROWN = CV_RGB(150, 75, 0); // brown
typedef unsigned char uchar;
typedef unsigned int uint;
typedef struct ColorCluster {
CvScalar color;
CvScalar new_color;
int count;
ColorCluster():count(0) {
}
} ColorCluster;
float rgb_euclidean(CvScalar p1, CvScalar p2)
{
float val = sqrtf( (p1.val[0]-p2.val[0])*(p1.val[0]-p2.val[0]) +
(p1.val[1]-p2.val[1])*(p1.val[1]-p2.val[1]) +
(p1.val[2]-p2.val[2])*(p1.val[2]-p2.val[2]) +
(p1.val[3]-p2.val[3])*(p1.val[3]-p2.val[3]));
return val;
}
// sorting flowers by quantity
bool colors_sort(std::pair< int, uint > a, std::pair< int, uint > b)
{
return (a.second > b.second);
}
int main(int argc, char* argv[])
{
// for save image
IplImage* image=0, *src=0, *dst=0, *dst2=0;
//
// loading image
//
char img_name[] = "Image0.jpg";
// name of image in first arg
char* image_filename = argc >= 2 ? argv[1] : img_name;
// get image
image = cvLoadImage(image_filename, 1);
printf("[i] image: %s\n", image_filename);
if(!image){
printf("[!] Error: cant load test image: %s\n", image_filename);
return -1;
}
// show image
cvNamedWindow("image");
cvShowImage("image", image);
// resize image (for processing speed)
src = cvCreateImage(cvSize(image->width/2, image->height/2), IPL_DEPTH_8U, 3);
cvResize(image, src, CV_INTER_LINEAR);
cvNamedWindow("img");
cvShowImage("img", src);
// image for storing cluster indexes
IplImage* cluster_indexes = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
cvZero(cluster_indexes);
#define CLUSTER_COUNT 10
int cluster_count = CLUSTER_COUNT;
ColorCluster clusters[CLUSTER_COUNT];
int i=0, j=0, k=0, x=0, y=0;
// initial cluster colors
#if 0
clusters[0].new_color = RED;
clusters[1].new_color = ORANGE;
clusters[2].new_color = YELLOW;
// clusters[3].new_color = GREEN;
// clusters[4].new_color = LIGHTBLUE;
// clusters[5].new_color = BLUE;
// clusters[6].new_color = VIOLET;
#elif 0
clusters[0].new_color = BLACK;
clusters[1].new_color = GREEN;
clusters[2].new_color = WHITE;
#else
CvRNG rng = cvRNG(-1);
for(k=0; k<cluster_count; k++)
clusters[k].new_color = CV_RGB(cvRandInt(&rng)%255, cvRandInt(&rng)%255, cvRandInt(&rng)%255);
#endif
// k-means
float min_rgb_euclidean = 0, old_rgb_euclidean=0;
while(1) {
for(k=0; k<cluster_count; k++) {
clusters[k].count = 0;
clusters[k].color = clusters[k].new_color;
clusters[k].new_color = cvScalarAll(0);
}
for (y=0; y<src->height; y++) {
for (x=0; x<src->width; x++) {
// get the RGB components of the pixel
uchar B = CV_PIXEL(uchar, src, x, y)[0]; // B
uchar G = CV_PIXEL(uchar, src, x, y)[1]; // G
uchar R = CV_PIXEL(uchar, src, x, y)[2]; // R
min_rgb_euclidean = 255*255*255;
int cluster_index = -1;
for(k=0; k<cluster_count; k++) {
float euclid = rgb_euclidean(cvScalar(B, G, R, 0), clusters[k].color);
if( euclid < min_rgb_euclidean ) {
min_rgb_euclidean = euclid;
cluster_index = k;
}
}
// set the cluster index
CV_PIXEL(uchar, cluster_indexes, x, y)[0] = cluster_index;
clusters[cluster_index].count++;
clusters[cluster_index].new_color.val[0] += B;
clusters[cluster_index].new_color.val[1] += G;
clusters[cluster_index].new_color.val[2] += R;
}
}
min_rgb_euclidean = 0;
for(k=0; k<cluster_count; k++) {
// new color
clusters[k].new_color.val[0] /= clusters[k].count;
clusters[k].new_color.val[1] /= clusters[k].count;
clusters[k].new_color.val[2] /= clusters[k].count;
float ecli = rgb_euclidean(clusters[k].new_color, clusters[k].color);
if(ecli > min_rgb_euclidean)
min_rgb_euclidean = ecli;
}
//printf("%f\n", min_rgb_euclidean);
if( fabs(min_rgb_euclidean - old_rgb_euclidean)<1 )
break;
old_rgb_euclidean = min_rgb_euclidean;
}
//-----------------------------------------------------
// Now we drive the array into a vector and sort it :)
std::vector< std::pair< int, uint > > colors;
colors.reserve(CLUSTER_COUNT);
int colors_count = 0;
for(i=0; i<CLUSTER_COUNT; i++){
std::pair< int, uint > color;
color.first = i;
color.second = clusters[i].count;
colors.push_back( color );
if(clusters[i].count>0)
colors_count++;
}
// sort
std::sort( colors.begin(), colors.end(), colors_sort );
// show color
dst2 = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 3 );
cvZero(dst2);
int h = dst2->height / CLUSTER_COUNT;
int w = dst2->width;
for(i=0; i<CLUSTER_COUNT; i++ ){
cvRectangle(dst2, cvPoint(0, i*h), cvPoint(w, i*h+h), clusters[colors[i].first].color, -1);
printf("[i] Color: %d %d %d (%d)\n", (int)clusters[colors[i].first].color.val[2],
(int)clusters[colors[i].first].color.val[1],
(int)clusters[colors[i].first].color.val[0],
clusters[colors[i].first].count);
}
cvNamedWindow("colors");
cvShowImage("colors", dst2);
//cvResize(dst2, image, CV_INTER_LINEAR);
//cvSaveImage("dominate_colors_table.png", image);
//-----------------------------------------------------
// show the picture in the colors found
dst = cvCloneImage(src);
for (y=0; y<dst->height; y++) {
for (x=0; x<dst->width; x++) {
int cluster_index = CV_PIXEL(uchar, cluster_indexes, x, y)[0];
CV_PIXEL(uchar, dst, x, y)[0] = clusters[cluster_index].color.val[0];
CV_PIXEL(uchar, dst, x, y)[1] = clusters[cluster_index].color.val[1];
CV_PIXEL(uchar, dst, x, y)[2] = clusters[cluster_index].color.val[2];
}
}
cvNamedWindow("dst");
cvShowImage("dst", dst);
//cvResize(dst, image, CV_INTER_LINEAR);
//cvSaveImage("dominate_colors.png", image);
//-----------------------------------------------------
// waiting for the keystroke
cvWaitKey(0);
// free up resources
cvReleaseImage(&image);
cvReleaseImage(&src);
cvReleaseImage(&cluster_indexes);
cvReleaseImage(&dst);
cvReleaseImage(&dst2);
// delete windows
cvDestroyAllWindows();
return 0;
}
Finally with the help of QImage class I'm able to get this done and I implemented a simple algorithm from scratch.
The idea is to get the most frequent intensity in each color channel , and then combining them to get the final RGB color.
It returns a QVector<int> of three channels.
Helper function:
int MainWindow::get_max(QVector<int> vec){
int max = vec[0];
int index = 0;
for(int i = 0 ; i < 255 ; i++){
if(vec[i] > max){
max = vec[i];
index = i;
}
}
return index;
}
And the picker function :
QVector<int> MainWindow::get_mean_rgb_for_img(const QImage img){
QRgb *ct;
int width , height;
width = img.width();
height = img.height();
QVector<int> red(256);
QVector<int> green(256);
QVector<int> blue(256);
for(int i = 0 ; i < height ; i++){
ct = (QRgb *)img.scanLine(i);
for(int j = 0 ; j < width ; j++){
red[qRed(ct[j])]+=1;
}
}
for(int i = 0 ; i < height ; i++){
ct = (QRgb *)img.scanLine(i);
for(int j = 0 ; j < width ; j++){
green[qGreen(ct[j])]+=1;
}
}
for(int i = 0 ; i < height ; i++){
ct = (QRgb *)img.scanLine(i);
for(int j = 0 ; j < width ; j++){
blue[qBlue(ct[j])]+=1;
}
}
int red_val = get_max(red);
int green_val = get_max(green);
int blue_val = get_max(blue);
QVector<int> result(3);
result.insert(0 , red_val);
result.insert(1 , green_val);
result.insert(2 , blue_val);
return result;
}
Although there is just one thread for those calculations (At least on the paper , but in practice Qt spawned a handful of threads without my notice by itself) it's pretty much fast and requires little CPU time even for 4K images !!!

Arduino function to fade an RGB from one color to the next

For my Arduino project I have a Neopixel RGB Strip with 72 LED's.
I can successfully change the colour of any of the LED's (at the moment I'm only setting the first one 0 for testing purposes) so I know my wiring isn't the problem here, it's my coding.
What I would like to do is be able to select a color and then another color and have the first color fade to the next color and so forth (much like the LIFX bulbs behave when using the iPhone application).
This is what I have at the moment:
I am logging output of all the variables to give you an indication of what's going on. I'm not 100% sure on where I'm going wrong or whether there's a much easier way to do what I'm after (I'm open to suggestions).
The function takes a parameter called command, which is a string separated by commas:
e.g. 255, 0, 0 (RED) or 0, 255, 0 (GREEN).
/*******************************************************************************
* Function Name : tinkerSetColour
* Description : Sets the strip with the appropriate colour
* Input : Pin and value
* Output : None.
* Return : 1 on success and a negative number on failure
*******************************************************************************/
int Rstart = 0, Gstart = 0, Bstart = 0;
int Rnew = 0, Gnew = 0, Bnew = 0;
int tinkerSetColour(String command)
{
sprintf(rgbString, "Rstart %i, Gstart %i, Bstart %i", Rstart, Gstart, Bstart);
Spark.publish("rgb", rgbString);
sprintf(rgbString, "Rnew %i, Gnew %i, Bnew %i", Rnew, Gnew, Bnew);
Spark.publish("rgb", rgbString);
// Clear strip.
strip.show();
int commaIndex = command.indexOf(',');
int secondCommaIndex = command.indexOf(',', commaIndex+1);
int lastCommaIndex = command.lastIndexOf(',');
int red = command.substring(0, commaIndex).toInt();
int grn = command.substring(commaIndex+1, secondCommaIndex).toInt();
int blu = command.substring(lastCommaIndex+1).toInt();
int Rend = red, Gend = grn, Bend = blu;
sprintf(rgbString, "Rend %i, Gend %i, Bend %i", Rend, Gend, Bend);
Spark.publish("rgb", rgbString);
// Larger values of 'n' will give a smoother/slower transition.
int n = 200;
for (int i = 0; i < n; i++)
{
Rnew = Rstart + (Rend - Rstart) * i / n;
Gnew = Gstart + (Gend - Gstart) * i / n;
Bnew = Bstart + (Bend - Bstart) * i / n;
// Set pixel color here.
strip.setPixelColor(0, strip.Color(Rnew, Gnew, Bnew));
}
sprintf(rgbString, "Rnew %i, Gnew %i, Bnew %i", Rnew, Gnew, Bnew);
Spark.publish("rgb", rgbString);
Rstart = red, Gstart = grn, Bstart = blu;
sprintf(rgbString, "Rstart %i, Gstart %i, Bstart %i", Rstart, Gstart, Bstart);
Spark.publish("rgb", rgbString);
return 1;
}
The problem is the colors are not fading between themselves.
Apologies if any of this is confusing. I can provide more information if necessary.
Here's the output selecting RED to begin with:
Rstart 0, Gstart 0, Bstart 0
Rnew 0, Gnew 0, Bnew 0
Rend 255, Gend 0, Bend 0
Rnew 253, Gnew 0, Bnew 0
Here's the output selecting GREEN directly afterwards:
Rstart 255, Gstart 0, Bstart 0
Rnew 253, Gnew 0, Bnew 0
Rend 0, Gend 255, Bend 0
Rnew 2, Gnew 253, Bnew 0
And then the output selecting BLUE after that:
Rstart 0, Gstart 255, Bstart 0
Rnew 2, Gnew 253, Bnew 0
Rend 0, Gend 23, Bend 255
Rnew 0, Gnew 25, Bnew 253
I've been working on the same problem for a couple days. I'm fairly new to arduino and coding so my code might be a bit simple, but for right now this seems to work.
I'm using the FastLED library.
#include "FastLED.h"
#define NUM_LEDS 15
#define DATA_PIN 6
CRGB leds[NUM_LEDS];
int fade = 2; //minutes
byte cred; //current red
byte cgreen;
byte cblue;
byte targetred; //red after fade
byte targetgreen;
byte targetblue;
byte oldred; //red before fade
byte oldgreen;
byte oldblue;
byte deltared; //difference before and after fade
byte deltagreen;
byte deltablue;
unsigned long start;
unsigned long current;
unsigned long whole;
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
cred = 0; cblue = 0; cgreen = 0;
oldred = 0; oldblue = 0; oldgreen = 0;
update();
start = millis();
}
void loop() {
targetred = 20;
targetgreen = 220;
targetblue = 130;
deltared = targetred - oldred; deltagreen = targetgreen - oldgreen; deltablue = targetblue - oldblue;
whole = fade * 60000 + start; //fade time in milliseconds
if (cred <= targetred && millis() <= whole){
current = millis();
cred = current * deltared / whole;
cred = cred + oldred;}
if (cgreen <= targetgreen && millis() <= whole){
current = millis();
cgreen = current * deltagreen / whole;
cgreen = cgreen + oldgreen;}
if (cblue <= targetblue && millis() <= whole){
current = millis();
cblue = current * deltablue / whole;
cblue = cblue + oldblue;}
update();
}
void update(){
for (int i = 0; i <= NUM_LEDS; i++){
leds[i] = CRGB (cred, cgreen, cblue);
FastLED.show();
}};
This version doesn't actually fade to another colour afterwards, but you would just assign oldred = cred etc, then update your targetcolours. another thing is that this code is tuned to fade up due to the if cred <= target red. I originally was using != but sometimes ccolour would shoot past ctarget and keep going. I need to figure out how to set better tolerances. I am trying to avoid using delay so it will be easier later to receive outside input without lag.
Anyway, it's good to look at the same problem from different approaches, and this is another. Good luck with your fades!
I think your code is not so bad, you just need delays. Because your forloop will be too quick for you to notice the fading.
From a project I'm working on, here is a c++/pseudocode example for fading a led between to rgb colors.
It can be modified to work with your library pretty easily. The Serial.print() are for a debug purpose and can be removed once it works. Notice at the end of each loop iteration the waitMS(). you can also replace it with the Arduino delay() function.
void fade(uint16_t duration, Color startColor, Color endColor) {
int16_t redDiff = endColor.getR() - startColor.getR();
int16_t greenDiff = endColor.getG() - startColor.getG();
int16_t blueDiff = endColor.getB() - startColor.getB();
int16_t delay = 20;
int16_t steps = duration / delay;
int16_t redValue, greenValue, blueValue;
for (int16_t i = 0 ; i < steps - 1 ; ++i) {
redValue = (int16_t)startColor.getR() + (redDiff * i / steps);
greenValue = (int16_t)startColor.getG() + (greenDiff * i / steps);
blueValue = (int16_t)startColor.getB() + (blueDiff * i / steps);
Serial.print(redValue);
Serial.print("\t");
Serial.print(greenValue);
Serial.print("\t");
Serial.print(blueValue);
Serial.println("\t");
led.shine(redValue, greenValue, blueValue);
waitMs(delay);
}
led.shine(endColor);
}
Hope this helps :)
EDIT:
Here is links to the code of Color and Led:
Led Class
Color Class

Arduino RGB LED code issues

I'm having some trouble with my arduino code. My code has 3 potentiometers that alter each individual colors in a LED, and a button that will fade through red, green, blue (when pressed, it cycles once and when held it continuously cycles). It used to work, but when I tried to add a LCD screen that displays each RGB value, it stopped working. I know it's not written the best, it's just an experimental code as I just got an arduino and am just trying to familiarize myself. When I plug it in, the LCD prints all white squares on the top row and the led is red with flashing blue, and nothing happens when I press the button or turn the pots. Here is the code, any help is appreciated:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int potPin1 = 14 ; // select the input pin for the potentiometer, ANALOG
int potPin2 = 15;
int potPin3 = 16;
int potVal1 = 0; // variable to store the value coming from the sensor
int potVal2 = 0;
int potVal3 = 0;
int ledPin1 = 6; // select the pin for the LED, PWM for analogWrite capability?
int ledPin2 = 9;
int ledPin3 = 10;
int buttonPin = 7;
int buttonState= LOW;
String printCycleVal1 = (""); //print values when cycling colors
String printCycleVal2 = ("");
String printCycleVal3 = ("");
String printVal1 = ("");//print values when not cycling colors
String printVal2 = ("");
String printVal3 = ("");
int val1 = 0; //values to store rgb colors in
int val2 = 0;
int val3 = 0;
void setup()
{
pinMode(potPin1, INPUT);
pinMode(potPin2, INPUT);
pinMode(potPin3, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT);
lcd.begin(2,16);
}
void loop()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("RGB Values: ");
lcd.setCursor(0,1);
potVal1 = analogRead(potPin1);
val1 = map(potVal1, 0, 1023, 0, 255);
analogWrite(ledPin1, val1);
printVal1 += ("V1: ");
printVal1 += (val1);
printVal1 += (" | ");
potVal2 = analogRead(potPin2);
val2 = map(potVal2, 0, 1023, 0, 255);
analogWrite(ledPin2, val2);
printVal2 += ("V2: ");
printVal2 += (val2);
printVal2 += (" | ");
potVal3 = analogRead(potPin3);
val3 = map(potVal3, 0, 1023, 0, 255);
analogWrite(ledPin3, val3);
printVal3 = ("V3: ");
printVal3 += (val3);
buttonState = digitalRead(buttonPin);
while (buttonState == HIGH)
cycle();
}
void cycle() {
setColourRgb(0, 0, 0);
unsigned int rgbColour[3];
// Start off with red.
rgbColour[0] = 255;
rgbColour[1] = 0;
rgbColour[2] = 0;
// Choose the colours to increment and decrement.
for (int decColour = 0; decColour < 3; decColour += 1) {
int incColour = decColour == 2 ? 0 : decColour + 1;
// cross-fade the two colours.
for(int i = 0; i < 255; i += 1) {
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("RGB Values: ");
lcd.setCursor(0,1);
printCycleVal1 += ("V1: ");
printCycleVal1 += (rgbColour[0]);
printCycleVal1 += (" | ");
printCycleVal2 += ("V2: ");
printCycleVal2 += (rgbColour[1]);
printCycleVal2 += (" | ");
printCycleVal3 += ("V3: ");
printCycleVal3 += (rgbColour[2]);
delay(5);
}
buttonState = LOW; n
}
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
analogWrite(ledPin1, red);
analogWrite(ledPin2, green);
analogWrite(ledPin3, blue);
}

Resources