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.