Don’t you already have the arbitrary TouchLED stuff? How about this:
#pragma config(Sensor, port1, LED0, sensorVexIQ_LED)
#pragma config(Sensor, port2, LED1, sensorVexIQ_LED)
#pragma config(Sensor, port3, LED2, sensorVexIQ_LED)
//#pragma config(Sensor, port4, LED4, sensorVexIQ_LED)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//
const int LEDCount = 3;
short LEDarray[LEDCount] = {LED0, LED1, LED2};
short LEDcolor[LEDCount] = {colorRed, colorRed, colorRed}; // colorRed
short LEDsound[LEDCount] = {noteA, noteA, noteA}; // noteA
const int length=8;
int level = 1;
int pressed = -1; //keeps track of which button was pressed (4= no button pressed)
int sequence[length];
void flash_touch(int index) {
setTouchLEDColor(LEDarray[index], LEDcolor[index]);
playNote(LEDsound[index],octave2,20);
sleep(100);
setTouchLEDColor(LEDarray[index], colorNone);
sleep(200);
}
bool check_LED(int index)
{
if (getTouchLEDValue(LEDarray[index]))
{
pressed=index; //blue to confirm
setTouchLEDColor(LEDarray[index],colorBlue);
//While the button is still being pushed (ie. Player hasn’t taken their finger off)
//don’t do any commands. (hence the empty set of {} brackets
while(getTouchLEDValue(LEDarray[index])){}
setTouchLEDColor(LEDarray[index],colorNone); //Turn off the TouchLED
return true;
}
return false;
}
task main()
{
srand(nSysTime); // generate seed for rand() from current system time
eraseDisplay();
//Load up the sequence with a random number. rand() gives a number between 0 and 32,767.
//By doing a modulo function with 3 (%3) it will divide the random number by 3 and keep the remainder
//This will give a final number of either 0,1 or 2
for(int x=0 ; x<length; x++)
{
sequence[x] = rand()%LEDCount;
}
//Light up the TouchLED's in the correct order based on the sequence and the Level you are currently on
for(int i=0 ; i<length ; i++) {
displayCenteredBigTextLine(2, "Level: %d", level); //display what Level you're on
sleep(1000);
for (int j=0; j<level ; j++) {
flash_touch(sequence[j]);
}
//Figure out which TouchLED has been pressed.
for (int j=0; j<level ; j++)
{
pressed=-1; //4 means no touchLED has been pressed
while(pressed==-1)
{
for(int k=0;k<LEDCount ; k++)
{
if (check_LED(k))
{
// break; //??
}
}
}
//If the TouchLED that was pressed is the same as the sequence then that's awesome!
if(pressed==sequence[j])
{
playNote(noteF, octave2, 10);
sleep(200);
} else // if it's not the same, change the display
{
eraseDisplay();
displayCenteredBigTextLine(0, "Game Over!");
displayCenteredBigTextLine(3, "Level: %d", level);
playSound(soundWrongWay);
playSound(soundWrongWay);
playSound(soundWrongWay);
sleep(3000);
stopAllTasks();
}
}
level++; //increase the level which will add another TouchLED to the sequence.
}
//If you make it through the whole sequence -> Horray!!!!
eraseDisplay();
displayCenteredBigTextLine(0, "You Rock!");
displayCenteredBigTextLine(3, "Level: %d", level--);
playSound(soundTada);
sleep(3000);
}