Switch/Case function with tilt switch and RGB

Hi,

I'm working on a school project, which will ultimately result in a game, where you need to match colours on a board to colours indicated on a "die." I want this "die" to consist of one RGB led and one tilt switch, where each tilt brings up a random colour, chosen from a predetermined set.

I wasn't sure how to do this, then I found the Switch/Case function, and an example of code that looked perfect. However, I can't figure out how to incorporate the switch into the code I found.

Here is what I'm working with:

int ledcolor = 0;
int a = 1000; //this sets how long the stays one color for
int red = 9; //this sets the red led pin
int green = 10; //this sets the green led pin
int blue = 11; //this sets the blue led pin
int tilt = 12; //pin for tilt switch
int tiltState;

void setup() { //this sets the output pins
  Serial.begin(9600);
  pinMode (tilt, INPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
}

void loop() {
  tiltState = digitalRead(tilt);
  Serial.print(ledcolor);
  if (tiltState = HIGH) {
    ledcolor = random(0,7); //this randomly selects a number between 0 and 6
    
    switch (ledcolor) {
      case 0: //if ledcolor equals 0 then the led will turn red
        analogWrite(red, 51);
        delay(a);
        analogWrite(red, 255);
        break;
      case 1: //if ledcolor equals 1 then the led will turn green
        digitalWrite(green, LOW);
        delay(a);
        digitalWrite(green, HIGH);
        break;
      case 2: //if ledcolor equals 2 then the led will turn blue
        digitalWrite(blue, LOW);
        delay(a);
        digitalWrite(blue, HIGH);
        break;
      case 3: //if ledcolor equals 3 then the led will turn yellow
        analogWrite(red, 95);
        digitalWrite(green, LOW);
        delay(a);
        analogWrite(red, 255);
        digitalWrite(green, HIGH);
        break;
      case 4: //if ledcolor equals 4 then the led will turn cyan
        analogWrite(red, 168);
        digitalWrite(blue, LOW);
        delay(a);
       analogWrite(red, 255);
       digitalWrite(blue, HIGH);
        break;
      case 5: //if ledcolor equals 5 then the led will turn magenta
        digitalWrite(green, LOW);
        digitalWrite(blue, LOW);
        delay(a);
        digitalWrite(green, HIGH);
        digitalWrite(blue, HIGH);
        break;
      case 6: //if ledcolor equals 6 then the led will turn white
        analogWrite(red, 155);
        digitalWrite(green, LOW);
        digitalWrite(blue, LOW);
        delay(a);
        analogWrite(red, 255);
        digitalWrite(green, HIGH);
        digitalWrite(blue, HIGH);
        break;
    }

  }    else {
    analogWrite(red, 0);
    analogWrite(green, 0);
    analogWrite(blue, 0);
  }

}

Any ideas? Am I going about this the wrong way? Another alternative had three separate tilt switches and a lot of "if/else" conditions, but the code I found for THAT wouldn't work on my UNO :/.

Thanks in advance! I don't have the wiring with me right now, but if it would help I could post it.

Need more info on your tilt sensor, specifically on how it works and how it is wired.

Also: what does your code actually do, and how does this differ from what you expect/want it to do?

I don't think your sketch is going to do what you expect, remember loop() loops continuously.
So let's say the tiltState is HIGH what is your code going to do first time round the loop?
What happens the next time round the loop if tiltState stays HIGH?
What happens if tiltState goes low and stays that way?

Start small and build your program up gradually, put in print statements so that you can see what it is doing and why.
Just try reading and printing the switch state to start.

Right now, the LED just runs randomly through the cases, and the Serial Monitor shows random numbers (in the range specified) being generated. So, the code I copied seems to be working as it should, just my edits in regards to the switch are not doing anything :confused:

ardly - I thought my problem was exactly what you described - when the switch is up, the code produces a random number, and generates the corresponding case, on loop.

What I'm confused about is that it keeps doing this even when the switch/button/tilt state is low - so it seems that the switch isn't registering at all, which doesn't make sense to me.

Here's a very amateur schematic and a link to a video of the setup.

RGB_schema.pdf (62.3 KB)

narmstrong:

  if (tiltState = HIGH) {

That's one problem. This will indeed always evaluate "true".

Try replacing it by:

  if (tiltState == HIGH) {

One is assignment, the other boolean comparison.