I have tried to comment my code as much as I can. If you have any questions about it, please feel free to ask. The code itself should have a lot of details and the comments mostly explain what is going on. The task description is in the code.
#pragma config(Sensor, dgtl1, allowButton, sensorTouch)
#pragma config(Sensor, dgtl2, resetButton, sensorTouch)
#pragma config(Sensor, dgtl3, redLED1, sensorLEDtoVCC)
#pragma config(Sensor, dgtl4, contestantButton1, sensorTouch)
#pragma config(Sensor, dgtl5, redLED2, sensorLEDtoVCC)
#pragma config(Sensor, dgtl6, contestantButton2, sensorTouch)
#pragma config(Sensor, dgtl7, redLED3, sensorLEDtoVCC)
#pragma config(Sensor, dgtl8, contestantButton3, sensorTouch)
#pragma config(Sensor, dgtl9, resetLED, sensorLEDtoVCC)
#pragma config(Sensor, dgtl10, greenLEDStart, sensorLEDtoVCC)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*
Project Title: Jeopardy Game
Team Members:
Date: 11/12/13
Section:
Task Description:
The Jeopardy game should be programmed to perform the following functions:
1. Alex has two buttons at his podium; a button that enables players to ring in once he has read the question or to ring in after someone misses the question (the green LED should light up to indicate the button has been pressed), and the other to reset the program.
2. Each contestant has their own signalling button when they ring in, their red light flashes with the flashes getting closer and closer together until it stays lit showing they are out of time.
3. After a contestant rings in, they should not be able to ring in again if they miss the question.
4. If no one answers the question or if someone gets the question right, the reset button should be hit to reset the program.
5. Wowing the teacher can be achieved if the green light acts as a timer for any contestant to ring in and if a player is locked out if they hold down their signalling button when Alex presses his enable button.
Hint: A thorough knowledge of functions is required.
Pseudo code:
After the question is read, Alex hits allowButton, this enables the contestants buttons
timer starts
Contestants hits their button,their button is disabled and red light starts flashing
reset button resets all the buttons and timers
*/
bool contestantsAllow;
bool badContestant1;
bool badContestant2;
bool badContestant3;
int y = 500;
void blinkLED(tSensors sensorPort)
{
ClearTimer(T2);
while(time1[T2] < 10000) //Performs body for 10 seconds (10000 Milliseconds
{
turnLEDOn(sensorPort);
waitInMilliseconds (y);
turnLEDOff(sensorPort);
waitInMilliseconds (y);
y = y *(5/10); //cuts wait time in half
}
}
task resetAll
{
while (1) //Keeps task running at all times
{
if (SensorValue[resetButton] == 1)//Resets all Sensors and Booleans
{
turnLEDOff (redLED1);
turnLEDOff (redLED2);
turnLEDOff (redLED3);
turnLEDOff (greenLEDStart);
SensorValue[contestantButton1] = 0;
SensorValue[contestantButton2] = 0;
SensorValue[contestantButton3] = 0;
SensorValue[allowbutton] = 0;
badContestant1 = false;
badContestant2 = false;
badContestant3 = false;
contestantsAllow = false;
turnLEDOn(resetLED);
wait (.5);
turnLEDOff(resetLED);
}
}
}
task main()
{
while(1)
{
StartTask(resetAll); //Starts the reset task so it is running in parallel with the main task
while (SensorValue[allowButton] == 0) //makes sure that the allowbutton isn't pressed
{
if (SensorValue[contestantButton1] == 1) //Checks to see if they have tried to answer before the question was finished
{
turnLEDOn(redLED1); //Visual that they can no longer answer
badContestant1 = true; //They are not allowed to answer if true
}
if (SensorValue[contestantButton2] == 1) //Checks to see if they have tried to answer before the question was finished
{
turnLEDOn(redLED2); //Visual that they can no longer answer
badContestant2 = true; //They are not allowed to answer if true
}
if (SensorValue[contestantButton3] == 1) //Checks to see if they have tried to answer before the question was finished
{
turnLEDOn(redLED3); //Visual that they can no longer answer
badContestant3 = true; //They are not allowed to answer if true
}
}
contestantsAllow = true; //Sets value to true so that contestants can answer the question
turnLEDOn(greenLEDStart); //Visual allowing them to answer the question
ClearTimer(T1);
while (time1[T1]<6000) //Starts timer, contestants 6 seconds
{
if (SensorValue[contestantButton1] == 1 && badContestant1 == false) //If they press their button and badcontestant is false then they can answer the question
{
badContestant2 = true; //locks other contestants out
badContestant3 = true; //locks other contestants out
{
ClearTimer(T2); //resets timer[T2]
while(time1[T2] < 10000) //Starts Timer for 10 seconds
blinkLED(redLED1);//runs function blinkLED
turnLEDOn(redLED2); //Visual showing that their turn is over
badContestant1 = true; //Contestant can't answer again
badContestant2 = false; //allows other contestants to answer
badContestant3 = false;//allows other contestants to answer
ClearTimer(T1); //Clears timer so that there is another 6 seconds added to the clock
}
}
else if (SensorValue[contestantButton2] == 1 && badContestant2 == false) //If they press their button and badcontestant is false then they can answer the question
{
badContestant1 = true; //locks other contestants out
badContestant3 = true; //locks other contestants out
{
ClearTimer(T2); //resets timer[T2]
while(time1[T2] < 10000) //Starts Timer for 10 seconds
blinkLED(redLED2);//runs function blinkLED
turnLEDOn(redLED2); //Visual showing that their turn is over
badContestant2 = true;//Contestant can't answer again
badContestant1 = false;//allows other contestants to answer
badContestant3 = false;//allows other contestants to answer
ClearTimer(T1); //Clears timer so that there is another 6 seconds added to the clock
}
}
else if (SensorValue[contestantButton3] == 1 && badContestant3 == false) //If they press their button and badcontestant is false then they can answer the question
{
badContestant2 = true; //locks other contestants out
badContestant1 = true; //locks other contestants out
ClearTimer(T2); //resets timer[T2]
while(time1[T2] < 10000) //Starts Timer for 10 seconds
blinkLED(redLED3);//runs function blinkLED
turnLEDOn(redLED3); //Visual showing that their turn is over
badContestant3 = true;//Contestant can't answer again
badContestant2 = false;//allows other contestants to answer
badContestant1 = false;//allows other contestants to answer
ClearTimer(T1); //Clears timer so that there is another 6 seconds added to the clock
}
}
}
}