Hi, I'm working a project where I want the RGB led to flash a random series of colors, then the user has to press the correct buttons/pressure sensor to recreate that series of colors.
I'm confused on how to allow the user to react to the series of colors. The loop() just runs constantly, so where in the code would the user be able to press the correct buttons in order to re-create the correct series of colors?
I have an array to store the colors that the LED has flashed, and I was going to have another array to store the user button presses as colors, and just compare the two at the end to see if the user was correct.
Here's what I want to happen when the user presses these assortments:
Inputs: 2 buttons and pressure sensor
2 buttons --> green
Right button --> red
Left button --> blue
Right button and pressure sensor --> yellow
Left button and pressure sensor --> cyan
Just pressure sensor --> magenta
I'm still pretty new to this, so I appreciate any help.
Here is the code I have so far, it only causes the RGB to display what I hope is a random series of colors. The part where I try to track the user's reaction doesn't work.
//INITIALIZE PINS
int ledDigitalOne[] = {6,10,11};
//6 = red, 10 = green, 11= blue
int buttonRight = 4;
int buttonLeft = 8;
int pressureSense = 2;
//DEFINE VARIABLES
const boolean ON=LOW;
const boolean OFF=HIGH;
int leftRead = HIGH;
int rightRead= HIGH;
int pressure = 0;
//PREDEFINED COLORS
boolean GREEN[] = {OFF, ON, OFF};
boolean RED[] = {ON, OFF, OFF};
boolean BLUE[] = {OFF, OFF, ON};
boolean YELLOW[] = {ON,ON,OFF};
boolean CYAN[] = {OFF,ON,ON};
boolean MAGENTA[] = {ON,OFF,ON};
//ARRAY TO STORE COLORS
const boolean* COLORS[] = {GREEN, RED, BLUE, YELLOW,CYAN,MAGENTA};
//INPUT AND OUTPUT ARRAYS
int randNum[6];
boolean* outputArray[6];
boolean* inputArray[6];
//FUNCTIONS
//sets led to color specified
void setColor(int* led, boolean* color)
{
for(int i = 0; i<3;i++)
{
digitalWrite(led[i], color[i]);
}
}
//SETUP
void setup()
{
for(int i = 0;i<3; i++)
pinMode(ledDigitalOne[i], OUTPUT);
pinMode(buttonRight, OUTPUT);
pinMode(buttonLeft, OUTPUT);
}
void loop()
{
pressure = analogRead(pressureSense);
leftRead = digitalRead(buttonLeft);
rightRead = digitalRead(buttonRight);
//ASSIGN VALUES TO RANDNUM
for (int i =0; i<6; i++)
{
randNum[i] = random(1, 1000);
}
//STORE COLORS IN OUTPUTARRAY
for (int i = 0; i<6; i++)
{
if (randNum[i]>0 && randNum[i] <=167)
outputArray[i] = GREEN;
else if (randNum[i] >167 && randNum[i]<= 334)
outputArray[i] = RED;
else if (randNum[i] >334 && randNum[i]<= 501)
outputArray[i] = BLUE;
else if (randNum[i]>501 && randNum[i]<= 668)
outputArray[i] = YELLOW;
else if (randNum[i] >668 && randNum[i]<= 835)
outputArray[i] = CYAN;
else if (randNum[i] >835 && randNum[i]<= 1000)
outputArray[i] = MAGENTA;
}
//SEND OUTPUT ARRAY TO SETCOLOR ONE AT A TIME
for (int i=0;i<6;i++)
{
setColor(ledDigitalOne, outputArray[i]);
delay(1000);
}
//USER RESPONSE
for (int i= 0; i<6; i++)
{
if (leftRead == LOW && rightRead ==LOW)
inputArray[i] = GREEN;
else if (rightRead ==LOW)
inputArray[i] = RED;
else if (leftRead ==LOW)
inputArray[i] = BLUE;
else if (rightRead == LOW && pressure >0)
inputArray[i] = YELLOW;
else if (rightRead == LOW && pressure>0)
inputArray[i] = CYAN;
else if (pressure>0 && leftRead == HIGH && rightRead == HIGH)
inputArray[i] = MAGENTA;
setColor(ledDigitalOne, inputArray[i]);
delay(1000);
}
}