Hello!
I am writing a model program based off of the price is right that runs off of sensor triggers and holds up to two "spins" and then sends the score to a display. Everything was working for one sensor(one input), but when I tried to nest everything inside a for() loop for multiple sensors, I no longer seem to be switching into my "stopped" state. I need my code to actually switch into stopped state and then hold until the sensor that triggered that stopped state changes again.
NOTE: Stop should be triggered when: x == 0 AND x has stayed 0 for three seconds. (I am using N.O. hall effects sensors so unless it senses a magnet, x ==1) .
Here is my code below. I have not yet coded my reset button into my void loop, and as of now I am testing with only two sensors, but eventually there will be 20 sensors. I am also using an arduino mega 2560.
Thanks for checking this out, hoping someone can help me resolve this!
// Model Price is Right Wheel!!!!!!!!
enum {spinning, stopped}; //states of wheel for void looooop
unsigned char state; ///states for wheel
unsigned long startTime = 0;
const long wait = 3000;
int x = 0;
int skipper = 7;
int resetter = 8;
typedef struct
{
const int pin; //first term
const int price; //second term
} pinsandprice;
pinsandprice pandp[] =
{
{22, (05)},
{23, (10)},
{24, (15)},
{25, (20)},
{26, (25)},
{27, (30)},
{28, (35)},
{29, (40)},
{30, (45)},
{31, (50)},
{32, (55)},
{33, (60)},
{34, (65)},
{35, (70)},
{36, (75)},
{37, (80)},
{38, (85)},
{39, (90)},
{40, (95)},
{41, (100)},
{42, (200)},
};
const int list [] = {0, 0};//UP TO two spins per person
int uno = list[0];
int dos = list[1];
int z = uno + dos;
void STORESUMSEND(int w)
{
if(uno == 0)
{
uno = w;
Serial.println("FIRST");
}
else if(uno != 0 && dos == 0)
{
dos = w;
Serial.println("SECOND");
z = uno + dos;
Serial.print("SCORE: ");
SCREEN(z);
}
}
void SCREEN(int z)
{
//send the correct Z value to display
Serial.write(z);
Serial.println(z);
uno = 0;
dos = 0;
z = 0;
}
void setup() {
for (int k = 0; k < 2; k++) //eventually 20 sensors!!
{
pinMode(pandp[k].pin, INPUT);
}
pinMode(skipper, INPUT);
pinMode(resetter, INPUT);
Serial.begin(9600);
}
void loop() {
if(digitalRead(skipper) == HIGH)
{
SCREEN(uno + dos);
}
else
{
for (int j = 0; j < 2; j++) //eventually 20 sensors!!
{
switch (state)
{
case spinning:
{
x = digitalRead(pandp[j].pin);
if(x == 0);
{
startTime = millis();
while((x==0) && (millis()-startTime < wait))
{
Serial.println("waiting...");
x = digitalRead(pandp[j].pin);
}
if((millis() - startTime) >= wait)
{
state = stopped;
}
}
}
break;
case stopped:
{
STORESUMSEND(pandp[j].price);
while(x == 0)
{
x = digitalRead(pandp[j].pin);
}
state = spinning;
}
break;
}
}
}
}