我试图尽可能多地注释我的代码。如果你对此有任何疑问,请随时提问。代码本身应该有很多细节,而注释主要解释了正在发生的事情。任务描述在代码中。
#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)
//*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
}
}
}
}发布于 2013-11-15 11:51:36
contestantsAllow等应该在本地主要声明,因为没有理由在全局命名空间中声明它们。如果您使用的是更面向对象的设计,例如,与传感器相关的所有内容都放在单独的模块(senor.h+senor.c等)中,那么只能由该模块使用的变量可以在文件范围内声明,但可以使用关键字static,从而使用面向对象的私有变量和封装概念。y一个有意义的名字。这是一个以毫秒为单位的延迟,为什么不这样命名呢?例如led_delay_ms。bool badContestant [COMPETITORS];。contestantButton也是如此,等等。这不仅会使您的程序更加强大,还会阻止您在整个程序的不同位置使用相同的代码,从而使其更易于维护。y = y *(5/10);对整数执行除法,而不是对浮点数执行除法。5/10总是0。你需要了解整数和浮动的区别。您可能可以通过将代码更改为y = (y*5) / 10;来修复这个特定的错误。volatile。这将防止代码优化可能导致的错误,因为编译器没有意识到某个特定变量被更新。此外,您可能需要使用信号量来保护这些变量,以实现线程安全(易失性不提供线程安全的原子访问)。wait()需要一个浮点变量吗?在我看来很可疑。{}或while之后使用if, for是一个很好的习惯。这会让你从很多偶然的错误中解脱出来。badContestant2 = true; //locks other contestants out。它将告诉读者此代码行将导致什么。一个坏评论的例子是blinkLED(redLED1); //runs function blinkLED。每个C程序员都知道代码是一个函数调用,所以注释是没有意义的。此外,如果代码本身就说明了问题,则不需要对其进行注释。例如,ClearTimer(T2);自言自语。//resets timer[T2]的评论是多余的。while (time1[T1]<6000)这样的东西,那么除非有评论,否则没有人能知道6000是什么意思。相反,将数值常量放入#define或const变量中。例如,您可以编写while (time1[T1]< 6*SECOND)。然后突然之间,您也可以删除注释,因为代码本身就说明了这一点。https://codereview.stackexchange.com/questions/35401
复制相似问题