Hello,
I’m working on a project where I have the RGB led blink a series of 6 different colors. The user then has to recreate that series of colors, by pressing different buttons and the pressure sensor. I was unsure of how to allow for the user to respond to the color series, and someone on this forum suggested using a state machine. So, I’m trying it out, but I’m having some difficulty.
Here’s what’s happening:
I plug in the arduino, push the button to start the sequence. It plays a series of colors, but the series does not change every time the program starts. If I unplug the arduino and start the program again, the same series plays.
Next, I try to recreate the sequence. I push buttonLeft to initiate the user input portion of the program, and the led turns blue. Then, if I try to push other buttons, nothing else happens. If I push buttonLeft again right away, still nothing happens. I’m not sure what’s happening here.
After pushing a few more buttons, if I push buttonLeft again, it starts what appears to be a new sequence of colors. This sequence is different from the first sequence, but if I run the program again and get to this step again, the same sequence plays at this step.
I’m kind of lost, so any help will be appreciated. Should I get rid of all the other states, and just have one ‘output’ state and one ‘input’ state? Why is my ‘input’ portion not working?
Here is the code:
//==============SYSTEM STATES====
//INTIALIZE = 0
//PLAY = 1
//WAIT = 2
//USERINPUT = 3
int state = 0; //master state
//========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;
int correct = 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];
//==============SETUP===============
void setup()
{
for(int i = 0;i<3; i++)
pinMode(ledDigitalOne[i], OUTPUT);
pinMode(buttonRight, INPUT);
pinMode(buttonLeft, INPUT);
}
//=====================LOOP==================
void loop()
{
switch(state)
{
case 0: //INITIALIZE
waitForPress();
break;
case 1: //PLAY
playOutputSequence();
break;
case 2: //WAIT
waitForStart();
break;
case 3: //USERINPUT
recordUserInput();
break;
}
}
//=========================FUNCTIONS=================================
//sets led to color specified
void setColor(int* led, boolean* color)
{
for(int i = 0; i<3;i++)
{
digitalWrite(led[i], color[i]);
}
}
//==================================================
void waitForPress()
{
leftRead = digitalRead(buttonLeft);
if (leftRead ==LOW)
state = 1; //PLAY
}
//====================================================
void playOutputSequence()
{
//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);
}
state = 2; //WAIT
}
//======================================================
void waitForStart()
{
leftRead = digitalRead(buttonLeft);
if (leftRead == LOW)
state = 3; //USERINPUT
else;
}
//========================================
void recordUserInput()
{
pressure = analogRead(pressureSense);
leftRead = digitalRead(buttonLeft);
rightRead = digitalRead(buttonRight);
//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 (leftRead == LOW && pressure>0)
inputArray[i] = CYAN;
else if (pressure>0 && leftRead == HIGH && rightRead == HIGH)
inputArray[i] = MAGENTA;
setColor(ledDigitalOne, inputArray[i]);
delay(1000);
}
state = 0;
}