Button-Controlled RGB scanner

Hello All. Boy, do I have a mess. I think I made this thing WAY more complicated than it needs to be. I will post my own code, but if anyone can contribute something similar that would work as I need it to, I would GREATLY appreciate using your code instead. I have taken bits and pieces of code from various projects in the Arduino tutorials and tried to put it together into one cohesive project.

I have a momentary button hooked to a digital pin, and an RGB LED hooked up to some PWM pins. THAT IS ALL!

All I need this to do is as follows:

-Initially start out with LED off
-Push button once - static RED color
-Push button twice - static GREEN color
-Push button three - static BLUE color
-Push button four - static PURPLE color
-push button five - static WHITE (ALL LIT) color
-Push button six - RGB scanning mode (slowly fade between all possible colors of the single RGB LED).
-REPEAT all of the above if button is pushed again (back to off)

That is ALL I need this to do.

I have the RGB scanning part just fine, but the problem is that as soon as I upload the sketch it goes right into scanning mode. The button is ignored, as are ALL of the cases. I don’t really understand how it can just go right into scanner mode. It does not make sense, and despite my best efforts I can not get it to change. Oh, and you will also notice a little extra code in each case. I also have an LM35 temperature sensor attached to the protoboard as well. I would like to take temperature measurements constantly while each LED color or sequence is illuminated.

I would not mind one bit if someone un-cluttered my code a bit, or just provided me with something that would work better! I am no expert here, but I know that how I have this setup is a totally over-complicated way to do this, and it simply does not work right.

Thank you in advance for ANY assistance!!

-Dave

// 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 black[3]  = { 0, 0, 0 };
int white[3]  = { 100, 100, 100 };
int red[3]    = { 100, 0, 0 };
int green[3]  = { 0, 100, 0 };
int blue[3]   = { 0, 0, 100 };
int yellow[3] = { 40, 95, 0 };
int dimWhite[3] = { 30, 30, 30 };
// etc.

// Set initial color
int redVal = black[0];
int grnVal = black[1]; 
int bluVal = black[2];

int wait = 10;      // 10ms internal crossFade delay; increase for slower fades
int hold = 0;       // 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 = 3;     // 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.
    }
  }
}








/* BELOW THIS LINE IS THE MATH -- YOU SHOULDN'T NEED TO CHANGE THIS FOR THE BASICS
* 
* The program works like this:
* Imagine a crossfade that moves the red LED from 0-10, 
*   the green from 0-5, and the blue from 10 to 7, in
*   ten steps.
*   We'd want to count the 10 steps and increase or 
*   decrease color values in evenly stepped increments.
*   Imagine a + indicates raising a value by 1, and a -
*   equals lowering it. Our 10 step fade would look like:
* 
*   1 2 3 4 5 6 7 8 9 10
* R + + + + + + + + + +
* G   +   +   +   +   +
* B     -     -     -
* 
* The red rises from 0 to 10 in ten steps, the green from 
* 0-5 in 5 steps, and the blue falls from 10 to 7 in three steps.
* 
* In the real program, the color percentages are converted to 
* 0-255 values, and there are 1020 steps (255*4).
* 
* To figure out how big a step there should be between one up- or
* down-tick of one of the LED values, we call calculateStep(), 
* which calculates the absolute gap between the start and end values, 
* and then divides that gap by 1020 to determine the size of the step  
* between adjustments in the value.
*/





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;
}






/* The next function is calculateVal. When the loop value, i,
*  reaches the step size appropriate for one of the
*  colors, it increases or decreases the value of that color by 1. 
*  (R, G, and B are each calculated separately.)
*/

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;
}








/* crossFade() converts the percentage colors to a 
*  0-255 range, then loops 1020 times, checking to see if  
*  the value needs to be updated each time, then writing
*  the color values to the correct pins.
*/

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

    }
  }
  // Update current values for next loop
  prevR = redVal; 
  prevG = grnVal; 
  prevB = bluVal;
  delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
}

The first thing I notice about your code is a lack of any inputs...

What pin do you have your button connected to?

Once you have set up a button and input pin, you then need to check in your code to see if it has been pressed.

Then you can start to work out your program logic. This bit in your loop() function:

  crossFade(red);
  crossFade(green);
  crossFade(blue);
  crossFade(yellow);

needs to have checks to choose which crossfade to do, the way you have it, it will just run all the cases.

needs to be more like:

if (buttonPressedTimes==1){
    crossFade(red);
}
if (buttonPressedTimes==2){
    crossFade(green);
}
if (buttonPressedTimes==3){
    crossFade(blue);
}
if (buttonPressedTimes==4){
    crossFade(yellow);
}

You will still need to work out how to check if the button is pressed and so on, but hopefully this is of some help.

Oh Geesh - it would probably help if I provided the updated code with the Switch statements, etc.
So sorry…

float tempC;
float tempF;
int reading;
int x=0;
int tempValues=0;
int tempPin = 0;
int caseCount=0;

void setup()
{
pinMode(4,INPUT);
digitalWrite(4,HIGH); //pullup resistor
analogReference(INTERNAL);
}

void loop()
{
// Output
int redPin = 6; // Red LED, connected to digital pin 9
int grnPin = 3; // Green LED, connected to digital pin 10
int bluPin = 5; // Blue LED, connected to digital pin 11

// Color arrays
int black[3] = {
0, 0, 0 };
int white[3] = {
100, 100, 100 };
int red[3] = {
100, 0, 0 };
int green[3] = {
0, 100, 0 };
int blue[3] = {
0, 0, 100 };
int yellow[3] = {
40, 95, 0 };
int dimWhite[3] = {
30, 30, 30 };
// etc.

// Set initial color
int redVal = black[0];
int grnVal = black[1];
int bluVal = black[2];

int wait = 3; // 10ms internal crossFade delay; increase for slower fades
int hold = 0; // 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;

float tempC;
float tempF;
int reading;
int x=0;
int tempValues=0;
int tempPin = 0;
int caseCount=0;

// Set up the LED outputs
void setup()
{
pinMode(2,INPUT);
digitalWrite(2,HIGH); //pullup resistor
analogReference(INTERNAL);

pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);

}

// Main program: list the order of crossfades
void loop()
{

if (digitalRead(2)==HIGH)
{
delay (20);
while (digitalRead(2)==LOW)
{
}
delay(20);
caseCount++;
}
if (caseCount<5)
{
switch (caseCount){
case 0: //Red
analogWrite(redPin, 255);
analogWrite(grnPin, 0);
analogWrite(bluPin, 0);

while (digitalRead(2)==LOW)
{
for (x=0; x <= 9; x++){
tempValues = tempValues + analogRead(tempPin);
}
reading = tempValues/10;
//reading = analogRead(tempPin);
tempC = reading / 9.31;
tempF=tempC*1.8+32;
tempValues=0;
}
break;

case 1: //Green
analogWrite(redPin, 0);
analogWrite(grnPin, 255);
analogWrite(bluPin, 0);

while (digitalRead(2)==LOW)
{
for (x=0; x <= 9; x++){
tempValues = tempValues + analogRead(tempPin);
}
reading = tempValues/10;
//reading = analogRead(tempPin);
tempC = reading / 9.31;
tempF=tempC*1.8+32;
tempValues=0;
break;
}

case 2: //Blue
analogWrite(redPin, 0);
analogWrite(grnPin, 0);
analogWrite(bluPin, 255);

while (digitalRead(2)==LOW)
{
for (x=0; x <= 9; x++){
tempValues = tempValues + analogRead(tempPin);
}
reading = tempValues/10;
//reading = analogRead(tempPin);
tempC = reading / 9.31;
tempF=tempC*1.8+32;
tempValues=0;
break;
}

case 3: //Purple
analogWrite(redPin, 255);
analogWrite(grnPin, 0);
analogWrite(bluPin, 255);

while (digitalRead(2)==LOW)
{
for (x=0; x <= 9; x++){
tempValues = tempValues + analogRead(tempPin);
}
reading = tempValues/10;
//reading = analogRead(tempPin);
tempC = reading / 9.31;
tempF=tempC*1.8+32;
tempValues=0;
break;
}

case 4: //White
analogWrite(redPin, 255);
analogWrite(grnPin, 255);
analogWrite(bluPin, 255);

while (digitalRead(2)==LOW)
{
for (x=0; x <= 9; x++){
tempValues = tempValues + analogRead(tempPin);
}
reading = tempValues/10;
//reading = analogRead(tempPin);
tempC = reading / 9.31;
tempF=tempC*1.8+32;
tempValues=0;
break;
}

case 5: //Scanner
crossFade(red);
crossFade(green);
crossFade(blue);
crossFade(white);

if (repeat) { // Do we loop a finite number of times?
j += 1;
if (j >= repeat) { // Are we there yet?
exit(j); // If so, stop.
}
}
while (digitalRead(2)==LOW)
{
for (x=0; x <= 9; x++){
tempValues = tempValues + analogRead(tempPin);
}
reading = tempValues/10;
//reading = analogRead(tempPin);
tempC = reading / 9.31;
tempF=tempC*1.8+32;
tempValues=0;
break;
}
}
}
else
{
caseCount=0;
}
}

/* BELOW THIS LINE IS THE MATH – YOU SHOULDN’T NEED TO CHANGE THIS FOR THE BASICS
*

  • The program works like this:
  • Imagine a crossfade that moves the red LED from 0-10,
  • the green from 0-5, and the blue from 10 to 7, in
  • ten steps.
  • We’d want to count the 10 steps and increase or
  • decrease color values in evenly stepped increments.
  • Imagine a + indicates raising a value by 1, and a -
  • equals lowering it. Our 10 step fade would look like:
  • 1 2 3 4 5 6 7 8 9 10
  • R + + + + + + + + + +
  • G + + + + +
  • B - - -
  • The red rises from 0 to 10 in ten steps, the green from
  • 0-5 in 5 steps, and the blue falls from 10 to 7 in three steps.
  • In the real program, the color percentages are converted to
  • 0-255 values, and there are 1020 steps (255*4).
  • To figure out how big a step there should be between one up- or
  • down-tick of one of the LED values, we call calculateStep(),
  • which calculates the absolute gap between the start and end values,
  • and then divides that gap by 1020 to determine the size of the step
  • between adjustments in the value.
    */

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;
}

/* The next function is calculateVal. When the loop value, i,

  • reaches the step size appropriate for one of the
  • colors, it increases or decreases the value of that color by 1.
  • (R, G, and B are each calculated separately.)
    */

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;
}

/* crossFade() converts the percentage colors to a

  • 0-255 range, then loops 1020 times, checking to see if
  • the value needs to be updated each time, then writing
  • the color values to the correct pins.
    */

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

}

// Update current values for next loop
prevR = redVal;
prevG = grnVal;
prevB = bluVal;
delay(hold); // Pause for optional ‘wait’ milliseconds before resuming the loop
}

Please replace the quote tags with code tags

the second piece of code you posted is worse!

2 setup() and 2 loop() functions

thats yer problem right there.

float tempC;
float tempF;
int reading;
int x=0;
int tempValues=0;
int tempPin = 0;
int caseCount=0;

void setup()
{
pinMode(4,INPUT);
digitalWrite(4,HIGH);   //pullup resistor
analogReference(INTERNAL);
}


void loop()
{
// Output
int redPin = 6;   // Red LED,   connected to digital pin 9
int grnPin = 3;  // Green LED, connected to digital pin 10
int bluPin = 5;  // Blue LED,  connected to digital pin 11

// Color arrays
int black[3]  = {
  0, 0, 0 };
int white[3]  = {
  100, 100, 100 };
int red[3]    = {
  100, 0, 0 };
int green[3]  = {
  0, 100, 0 };
int blue[3]   = {
  0, 0, 100 };
int yellow[3] = {
  40, 95, 0 };
int dimWhite[3] = {
  30, 30, 30 };
// etc.

// Set initial color
int redVal = black[0];
int grnVal = black[1];
int bluVal = black[2];

int wait = 3;      // 10ms internal crossFade delay; increase for slower fades
int hold = 0;       // 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;

float tempC;
float tempF;
int reading;
int x=0;
int tempValues=0;
int tempPin = 0;
int caseCount=0;

// Set up the LED outputs
void setup()
{
  pinMode(2,INPUT);
  digitalWrite(2,HIGH);   //pullup resistor
  analogReference(INTERNAL);

  pinMode(redPin, OUTPUT);   // sets the pins as output
  pinMode(grnPin, OUTPUT);   
  pinMode(bluPin, OUTPUT);

}





// Main program: list the order of crossfades
void loop()
{

  if (digitalRead(2)==HIGH)
  {
    delay (20);
    while (digitalRead(2)==LOW)
    {
    }
    delay(20);
    caseCount++;
  }
  if (caseCount<5)
  {
    switch (caseCount){
    case 0:  //Red
      analogWrite(redPin, 255);
      analogWrite(grnPin, 0);
      analogWrite(bluPin, 0);

      while (digitalRead(2)==LOW)
      {
        for (x=0; x <= 9; x++){
          tempValues = tempValues + analogRead(tempPin);
        }
        reading = tempValues/10;
        //reading = analogRead(tempPin);
        tempC = reading / 9.31;
        tempF=tempC*1.8+32;
        tempValues=0;
      }
      break;


    case 1:  //Green
      analogWrite(redPin, 0);
      analogWrite(grnPin, 255);
      analogWrite(bluPin, 0);

      while (digitalRead(2)==LOW)
      {
        for (x=0; x <= 9; x++){
          tempValues = tempValues + analogRead(tempPin);
        }
        reading = tempValues/10;
        //reading = analogRead(tempPin);
        tempC = reading / 9.31;
        tempF=tempC*1.8+32;
        tempValues=0;
        break;
      }

    case 2:  //Blue
      analogWrite(redPin, 0);
      analogWrite(grnPin, 0);
      analogWrite(bluPin, 255);

      while (digitalRead(2)==LOW)
      {
        for (x=0; x <= 9; x++){
          tempValues = tempValues + analogRead(tempPin);
        }
        reading = tempValues/10;
        //reading = analogRead(tempPin);
        tempC = reading / 9.31;
        tempF=tempC*1.8+32;
        tempValues=0;
        break;
      }

    case 3:  //Purple
      analogWrite(redPin, 255);
      analogWrite(grnPin, 0);
      analogWrite(bluPin, 255);

      while (digitalRead(2)==LOW)
      {
        for (x=0; x <= 9; x++){
          tempValues = tempValues + analogRead(tempPin);
        }
        reading = tempValues/10;
        //reading = analogRead(tempPin);
        tempC = reading / 9.31;
        tempF=tempC*1.8+32;
        tempValues=0;
        break;
      }

    case 4:  //White
      analogWrite(redPin, 255);
      analogWrite(grnPin, 255);
      analogWrite(bluPin, 255);

      while (digitalRead(2)==LOW)
      {
        for (x=0; x <= 9; x++){
          tempValues = tempValues + analogRead(tempPin);
        }
        reading = tempValues/10;
        //reading = analogRead(tempPin);
        tempC = reading / 9.31;
        tempF=tempC*1.8+32;
        tempValues=0;
        break;
      }

    case 5:  //Scanner
      crossFade(red);
      crossFade(green);
      crossFade(blue);
      crossFade(white);

      if (repeat) { // Do we loop a finite number of times?
        j += 1;
        if (j >= repeat) { // Are we there yet?
          exit(j);         // If so, stop.
        }
      }
      while (digitalRead(2)==LOW)
      {
        for (x=0; x <= 9; x++){
          tempValues = tempValues + analogRead(tempPin);
        }
        reading = tempValues/10;
        //reading = analogRead(tempPin);
        tempC = reading / 9.31;
        tempF=tempC*1.8+32;
        tempValues=0;
        break;
      }
    }
  }
else
{
  caseCount=0;
}
}








/* BELOW THIS LINE IS THE MATH -- YOU SHOULDN'T NEED TO CHANGE THIS FOR THE BASICS
 *
 * The program works like this:
 * Imagine a crossfade that moves the red LED from 0-10,
 *   the green from 0-5, and the blue from 10 to 7, in
 *   ten steps.
 *   We'd want to count the 10 steps and increase or
 *   decrease color values in evenly stepped increments.
 *   Imagine a + indicates raising a value by 1, and a -
 *   equals lowering it. Our 10 step fade would look like:
 *
 *   1 2 3 4 5 6 7 8 9 10
 * R + + + + + + + + + +
 * G   +   +   +   +   +
 * B     -     -     -
 *
 * The red rises from 0 to 10 in ten steps, the green from
 * 0-5 in 5 steps, and the blue falls from 10 to 7 in three steps.
 *
 * In the real program, the color percentages are converted to
 * 0-255 values, and there are 1020 steps (255*4).
 *
 * To figure out how big a step there should be between one up- or
 * down-tick of one of the LED values, we call calculateStep(),
 * which calculates the absolute gap between the start and end values,
 * and then divides that gap by 1020 to determine the size of the step 
 * between adjustments in the value.
 */

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;
}

/* The next function is calculateVal. When the loop value, i,
 *  reaches the step size appropriate for one of the
 *  colors, it increases or decreases the value of that color by 1.
 *  (R, G, and B are each calculated separately.)
 */





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;
}

/* crossFade() converts the percentage colors to a
 *  0-255 range, then loops 1020 times, checking to see if 
 *  the value needs to be updated each time, then writing
 *  the color values to the correct pins.
 */





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

        }
 
  // Update current values for next loop
  prevR = redVal;
  prevG = grnVal;
  prevB = bluVal;
  delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
}

It's fairly unusual to put all the stuff that you'd normally put at global scope inside "loop", but if it works for you, that's ok.

I dont know what you want us to do for you.

You have 2 setup() and 2 loop() functions, and they both seem to do different things.

You can only have one function of each name…

either delete the ones you dont need, or move the parts that you do need into just one setup() and loop() function.

I think you might be better off starting again, and just adding a piece at a time, making sure each step works.

Try to keep it simple, and dont duplicate function names - this can only lead to errors (If it compiles).

OK - one more attempt. THAT is not my code. Something is happening between when I paste it in here and post it. Things are out of order. I only have one setup, one loop… that much I’m pretty clear on. Lol.

As I look at it in this edit window, everything is as it should be… declarations, setup, loop, ancillary fade functions.

Again, ALL I need it to do is switch cases per button press, then start back to the beginning case.
Hope that makes things simpler.

// Color arrays
int black[3]  = {  0, 0, 0 };
int white[3]  = {  100, 100, 100 };
int red[3]    = {  100, 0, 0 };
int green[3]  = {  0, 100, 0 };
int blue[3]   = {  0, 0, 100 };
int yellow[3] = {  40, 95, 0 };
int dimWhite[3] = {  30, 30, 30 };
// etc.

// Set initial color
int redVal = black[0];
int grnVal = black[1];
int bluVal = black[2];

int wait = 3;      // 10ms internal crossFade delay; increase for slower fades
int hold = 0;       // 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;

float tempC;
float tempF;
int reading;
int x=0;
int tempValues=0;
int tempPin = 0;
int caseCount=0;

// Set up the LED outputs
void setup()
{
  pinMode(2,INPUT);
  digitalWrite(2,HIGH);   //pullup resistor
  analogReference(INTERNAL);

  pinMode(redPin, OUTPUT);   // sets the pins as output
  pinMode(grnPin, OUTPUT);   
  pinMode(bluPin, OUTPUT);

}





// Main program: list the order of crossfades
void loop()
{

  if (digitalRead(2)==HIGH)
  {
    delay (20);
    while (digitalRead(2)==LOW)
    {
    }
    delay(20);
    caseCount++;
  }
  if (caseCount<5)
  {
    switch (caseCount){
    case 0:  //Red
      analogWrite(redPin, 255);
      analogWrite(grnPin, 0);
      analogWrite(bluPin, 0);

      while (digitalRead(2)==LOW)
      {
        for (x=0; x <= 9; x++){
          tempValues = tempValues + analogRead(tempPin);
        }
        reading = tempValues/10;
        //reading = analogRead(tempPin);
        tempC = reading / 9.31;
        tempF=tempC*1.8+32;
        tempValues=0;
      }
      break;


    case 1:  //Green
      analogWrite(redPin, 0);
      analogWrite(grnPin, 255);
      analogWrite(bluPin, 0);

      while (digitalRead(2)==LOW)
      {
        for (x=0; x <= 9; x++){
          tempValues = tempValues + analogRead(tempPin);
        }
        reading = tempValues/10;
        //reading = analogRead(tempPin);
        tempC = reading / 9.31;
        tempF=tempC*1.8+32;
        tempValues=0;
        break;
      }

    case 2:  //Blue
      analogWrite(redPin, 0);
      analogWrite(grnPin, 0);
      analogWrite(bluPin, 255);

      while (digitalRead(2)==LOW)
      {
        for (x=0; x <= 9; x++){
          tempValues = tempValues + analogRead(tempPin);
        }
        reading = tempValues/10;
        //reading = analogRead(tempPin);
        tempC = reading / 9.31;
        tempF=tempC*1.8+32;
        tempValues=0;
        break;
      }

    case 3:  //Purple
      analogWrite(redPin, 255);
      analogWrite(grnPin, 0);
      analogWrite(bluPin, 255);

      while (digitalRead(2)==LOW)
      {
        for (x=0; x <= 9; x++){
          tempValues = tempValues + analogRead(tempPin);
        }
        reading = tempValues/10;
        //reading = analogRead(tempPin);
        tempC = reading / 9.31;
        tempF=tempC*1.8+32;
        tempValues=0;
        break;
      }

    case 4:  //White
      analogWrite(redPin, 255);
      analogWrite(grnPin, 255);
      analogWrite(bluPin, 255);

      while (digitalRead(2)==LOW)
      {
        for (x=0; x <= 9; x++){
          tempValues = tempValues + analogRead(tempPin);
        }
        reading = tempValues/10;
        //reading = analogRead(tempPin);
        tempC = reading / 9.31;
        tempF=tempC*1.8+32;
        tempValues=0;
        break;
      }

    case 5:  //Scanner
      crossFade(red);
      crossFade(green);
      crossFade(blue);
      crossFade(white);

      if (repeat) { // Do we loop a finite number of times?
        j += 1;
        if (j >= repeat) { // Are we there yet?
          exit(j);         // If so, stop.
        }
      }
      while (digitalRead(2)==LOW)
      {
        for (x=0; x <= 9; x++){
          tempValues = tempValues + analogRead(tempPin);
        }
        reading = tempValues/10;
        //reading = analogRead(tempPin);
        tempC = reading / 9.31;
        tempF=tempC*1.8+32;
        tempValues=0;
        break;
      }
    }
  }
else
{
  caseCount=0;
}
}








/* BELOW THIS LINE IS THE MATH -- YOU SHOULDN'T NEED TO CHANGE THIS FOR THE BASICS
 *
 * The program works like this:
 * Imagine a crossfade that moves the red LED from 0-10,
 *   the green from 0-5, and the blue from 10 to 7, in
 *   ten steps.
 *   We'd want to count the 10 steps and increase or
 *   decrease color values in evenly stepped increments.
 *   Imagine a + indicates raising a value by 1, and a -
 *   equals lowering it. Our 10 step fade would look like:
 *
 *   1 2 3 4 5 6 7 8 9 10
 * R + + + + + + + + + +
 * G   +   +   +   +   +
 * B     -     -     -
 *
 * The red rises from 0 to 10 in ten steps, the green from
 * 0-5 in 5 steps, and the blue falls from 10 to 7 in three steps.
 *
 * In the real program, the color percentages are converted to
 * 0-255 values, and there are 1020 steps (255*4).
 *
 * To figure out how big a step there should be between one up- or
 * down-tick of one of the LED values, we call calculateStep(),
 * which calculates the absolute gap between the start and end values,
 * and then divides that gap by 1020 to determine the size of the step 
 * between adjustments in the value.
 */

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;
}

/* The next function is calculateVal. When the loop value, i,
 *  reaches the step size appropriate for one of the
 *  colors, it increases or decreases the value of that color by 1.
 *  (R, G, and B are each calculated separately.)
 */





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;
}

/* crossFade() converts the percentage colors to a
 *  0-255 range, then loops 1020 times, checking to see if 
 *  the value needs to be updated each time, then writing
 *  the color values to the correct pins.
 */





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

        }
 
  // Update current values for next loop
  prevR = redVal;
  prevG = grnVal;
  prevB = bluVal;
  delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
}

Your code is still pretty hard to digest, because the cases are far apart. Create several functions, one for each case.

void Red()
{
   analogWrite(redPin, 255);
   analogWrite(grnPin, 0);
   analogWrite(bluPin, 0);

   while (digitalRead(2)==LOW)
   {
      for (x=0; x <= 9; x++)
      {
         tempValues = tempValues + analogRead(tempPin);
      }
      reading = tempValues/10;
      tempC = reading / 9.31;
      tempF=tempC*1.8+32;
      tempValues=0;
  }
}

Then, call this function:

    switch (caseCount)
    {
       case 0:  //Red
         Red();
         break;
      case 1:

Serial.print() can be useful. What is the value of caseCount after the while loop?

  if (caseCount<5)

On the other hand, if caseCount IS 5, case 5: will never be executed, because the switch statement is never reached.