首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ROBOTC危险程序

ROBOTC危险程序
EN

Code Review用户
提问于 2013-11-15 03:21:10
回答 1查看 810关注 0票数 1

我试图尽可能多地注释我的代码。如果你对此有任何疑问,请随时提问。代码本身应该有很多细节,而注释主要解释了正在发生的事情。任务描述在代码中。

代码语言:javascript
复制
#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
            }
        }
    }
}
EN

回答 1

Code Review用户

发布于 2013-11-15 11:51:36

程序设计问题:

  • 不要使用全局变量。在这种情况下,变量contestantsAllow等应该在本地主要声明,因为没有理由在全局命名空间中声明它们。如果您使用的是更面向对象的设计,例如,与传感器相关的所有内容都放在单独的模块(senor.h+senor.c等)中,那么只能由该模块使用的变量可以在文件范围内声明,但可以使用关键字static,从而使用面向对象的私有变量和封装概念。
  • 应该给y一个有意义的名字。这是一个以毫秒为单位的延迟,为什么不这样命名呢?例如led_delay_ms
  • 每次执行延迟时,都会将整个CPU锁定在100%。在这种情况下,这可能不是一个问题,但它将出现在一个更大的程序中。考虑使用轮询或中断代替。
  • 尽管Jeopardy总是有3个竞争者,但考虑重写代码,这样它就可以与任何数量的竞争对手一起工作。例如,三个不同的badContestant变量应该存储在数组中,以提高可读性,使程序更具通用性:bool badContestant [COMPETITORS];contestantButton也是如此,等等。这不仅会使您的程序更加强大,还会阻止您在整个程序的不同位置使用相同的代码,从而使其更易于维护。

Bugs:

  • y = y *(5/10);对整数执行除法,而不是对浮点数执行除法。5/10总是0。你需要了解整数和浮动的区别。您可能可以通过将代码更改为y = (y*5) / 10;来修复这个特定的错误。
  • RTOS中不同任务之间、主代码与中断之间或线程之间共享的变量必须始终声明为volatile。这将防止代码优化可能导致的错误,因为编译器没有意识到某个特定变量被更新。此外,您可能需要使用信号量来保护这些变量,以实现线程安全(易失性不提供线程安全的原子访问)。
  • 你似乎不包括stdbool.h。张贴的代码真的完整吗?它不应该编译。如果它不使用该头进行编译,则很可能是将其编译为C++,这一直是个坏主意。C和C++在许多方面都是非常不同的语言。
  • 这可能是一个bug,也可能不是一个bug,但是您确定函数wait()需要一个浮点变量吗?在我看来很可疑。

编码风格:

  • 总是在每次{}while之后使用if, for是一个很好的习惯。这会让你从很多偶然的错误中解脱出来。
  • 您的缩进不一致,使得代码很难读懂。总是缩进两个或四个空格(选择一种风格,并坚持它)。如果您使用选项卡键进行缩进,请确保IDE用空格替换制表符。
  • 在可能的情况下,考虑在特定的列中放置注释,比如在40或50个符号之后。(有一些缩进程序可以为您做到这一点,比如UniversalIndent。)示例:代替ClearTimer(T2);//重置计时器T2this(time1T2< 10000) //启动计时器10秒blinkLED(redLED1);//运行函数blinkLED将注释放在以下位置: ClearTimer(T2);//重置计时器T2this(time1T2< 10000) /开始计时器10秒blinkLED (RedLED1);//运行函数blinkLED
  • 避免毫无意义的评论。您的评论应该说明程序正在做什么,以便另一个C程序员能够知道它所做的事情。您应该假设您的评论的读者了解C语言。一个好评论的例子是badContestant2 = true; //locks other contestants out。它将告诉读者此代码行将导致什么。一个坏评论的例子是blinkLED(redLED1); //runs function blinkLED。每个C程序员都知道代码是一个函数调用,所以注释是没有意义的。此外,如果代码本身就说明了问题,则不需要对其进行注释。例如,ClearTimer(T2);自言自语。//resets timer[T2]的评论是多余的。
  • 不要在代码中使用“魔术数字”。如果你有像while (time1[T1]<6000)这样的东西,那么除非有评论,否则没有人能知道6000是什么意思。相反,将数值常量放入#defineconst变量中。例如,您可以编写while (time1[T1]< 6*SECOND)。然后突然之间,您也可以删除注释,因为代码本身就说明了这一点。
票数 5
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/35401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档