Switch/Case

Hi there—

This is a novice question, but I am stuck. I believe I understand the use of switch/case… but for some reason when my program moves to the third case— the program behaves differently despite identical copy/pasted code blocks for the other cases. Would love some feedback.

The program is supposed to map the light sensor to the brightness value of the LEDS. Upon pushing the left button, I want it to change from red, to green, to blue.

It works perfectly for the first 2 cases but in the 3rd instance the program seems to stop looping.

#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_SleepyDog.h>

float Color;
bool leftButtonPressed;
int currentIndex = 0;

void setup() {

leftButtonPressed = CircuitPlayground.leftButton();
CircuitPlayground.begin();

}

void loop() {

for (int i=0;i<=9;i++) {
Serial.begin (9600);
Serial.println (currentIndex);
if (CircuitPlayground.lightSensor() < 0 || CircuitPlayground.lightSensor() > 255 ) {
Color = 0 ;
}
else {
Color = (255- (CircuitPlayground.lightSensor());
}
if (leftButtonPressed) {
currentIndex++;
delay(1000);
switch (currentColor) {
case 1:
CircuitPlayground.setPixelColor(i, 0, 0, Color);
break;
case 2:
CircuitPlayground.setPixelColor(i, Color, 0, 0);
break;
case 3:
CircuitPlayground.setPixelColor(i, 0, Color, 0);
break;
default:
currentColor = 0
}}}

Next time when you are posting PLEASE consider using

code tags

That, and post the complete code. Because this will NOT compile because currentColor is nowhere defined…

And quick tips

  • Press Ctrl+T and see how that looks
  • NOTHING comes ever after a {, } or ;

The code you have won't even compile. You have two names, currentIndex and currentColor that you seem to think represent the same memory location. Only one of them is actually defined.

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Please note post #2.

Thanks.. Tom... :)