Hi, i am making a game whereby a random LED will light up (from 0-11) and if the corresponding switch is flipped, the LED will off and add one to the scoreboard (7-segments). Right now, when a random LED lights up, the program will wait until the correct switch is flipped before proceeding,. However, the LED just waits as the switch seems to only read once.
Switch used: DIP switches
I/O expander used: PCF8574p
The code for the LED generates 12 numbers that will not repeat the previous number
char k, i;
for(i = 0; i<12; i++){
while(1){
k = random(0, 12);
if(inArray(k, i) != 1){
break;
}
}
num[i] = k;
//Serial.println((int)k); // Random Sequence, Stored in num[i] //((int)k) : casting
delay(1000);
DisplayLED(k);
}
}
char inArray (char Rnum, char limit){//Check if generated Num is already in array
char i;
for(i=0; i<limit; i++){
if(Rnum == num[i]){
return 1; //in array
}
}
return 0; //not in array
}
A switch/case is used to light each individual LED.
*ignore the while( c != 509){} lines.
void DisplayLED (int k) {
expanderWrite3(B11111111); //turns all the LED off (from 0-7)
expanderWrite4(B11111111); //turns all the LED off (from 8-11)
int c = 0;
c = readsw();
switch (k) { // A random gen. number goes to the corresponding case
case 0:
expanderWrite3(B11111110);
readsw();
while( c != 509){
}
delay(500);
expanderWrite3(B11111111);
break;
case 1:
expanderWrite3(B11111101);
readsw();
while( c != 509){
}
delay(500);
expanderWrite3(B11111111);
break;
case 2:
expanderWrite3(B11111011);
readsw();
while( c !=510){
}
delay(500);
expanderWrite3(B11111111);
readsw();
break;
case 3:
expanderWrite3(B11110111);
readsw();
while( c !=509){
}
delay(500);
expanderWrite3(B11111111);
break;
case 4:
expanderWrite3(B11101111);
readsw();
while( c !=509){
}
delay(500);
expanderWrite3(B11111111);
break;
case 5:
expanderWrite3(B11011111);
readsw();
while( c !=509){
}
delay(500);
expanderWrite3(B11111111);
break;
case 6:
expanderWrite3(B10111111);
readsw();
while( c !=509){
}
delay(500);
expanderWrite3(B11111111);
break;
case 7:
expanderWrite3(B01111111);
readsw();
while( c !=509){
}
delay(500);
expanderWrite3(B11111111);
break;
case 8:
expanderWrite4(B01111111);
readsw();
while( c !=509){
}
delay(500);
expanderWrite4(B11111111);
break;
case 9:
expanderWrite4(B10111111);
readsw();
while( c !=509){
// temp = readsw();
}
delay(500);
expanderWrite4(B11111111);
break;
case 10:
expanderWrite4(B11011111);
readsw();
while( c !=509){
}
delay(500);
expanderWrite4(B11111111);
break;
case 11:
expanderWrite4(B11101111);
readsw();
while( c !=509){
}
delay(500);
expanderWrite4(B11111111);
break;
}
}
This is the read switch function that reads that gets the 2bytes.
int readsw (void){
i2cRecData(expander7, 1, &swStatus1); //Reads the switch (I/O address, bytes, to where)
i2cRecData(expander8, 1, &swStatus2);
swVal = swStatus1 + swStatus2; // Gets the total 2 byte value (max:510)
return swVal;
}
The readsw() should be constantly reading the switch so that when any pin is flipped to high, it will go through the while() statement and wait until the correct switch is flipped and off the LED (eventually adding 1 to the score)
The I/O address are all correct and the program runs with no error. However, the readsw() function just reads once making the program stuck at the while loop forever. So what do i do to make it check the readsw() constantly?
I've tried adding in for loops within the readsw() function but it does not work.
Thanks in advance.