【问题标题】:Arduino Uno: While-loop fails to "restart" using Button to reset Timer when second-pressing?Arduino Uno:当第二次按下时,While-loop 无法使用按钮重置计时器“重启”?
【发布时间】:2020-11-07 17:45:17
【问题描述】:

代码的作用:

  1. 代码首先显示一个简短的“闪烁动画”,其中 A-F 段一次打开和关闭一个。
  2. “闪烁动画”持续 3 到 10 秒,然后计时器开始计时。
  3. 数字 1 和 2 显示的计时器从 0 到 99 秒计数。而数字 3 和 4 分别显示第 10 和第 100 秒。
  4. 通过使用连接的按钮,您可以通过按下它来停止计时器。计时器停止并显示时间。
  5. 再次按下按钮应该会重新启动计时器并将我们送回第 1 步。

我的问题:当我进入代码中的第 5 步时,它不会重新启动。据我所知,该问题与 main 函数中的 while 循环有关,这是由函数“stopTimer”中的第二个 while 循环引起的。不能让它退出循环。至少这是我从无数次不成功的尝试中了解到的!尝试在“stopDisplay”函数中添加带有 if 语句的代码,包括 break;或返回;等等

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <avr/io.h>
#include <util/delay.h>

#define BUTTONPRESSED PINC & 1 << PC0

    /*
    - PORT = D
    Segment-A   Pin 1   PD1     0B00000010
    Segment-B   Pin 2   PD2     0B00000100
    Segment-C   Pin 3   PD3     0B00001000
    Segment-D   Pin 4   PD4     0B00010000
    Segment-E   Pin 5   PD5     0B00100000
    Segment-F   Pin 6   PD6     0B01000000
    Segment-G   Pin 7   PD7     0B10000000

    - PORT = B
    Digit 1     Pin 10  PB2     0B00000100
    Digit 2     Pin 11  PB3     0B00001000
    Digit 3     Pin 12  PB4     0B00010000
    Digit 4     Pin 13  PB5     0B00100000

    - PORT = C
    Button      Pin A0  PC0     0B00000001
    */

//Number: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9  in a array.
int segmentNumberArray[10] = {0B01111110, 0B00001100, 0B010110110, 0B10011110, 0B011001100, 0B11011010, 0B11111010, 0B00001110, 0B11111110, 0B11011110};

//Digit: 1, 2, 3, 4   in a array
int digitArrayOff[4] = {0B00000100, 0B00001000, 0B00010000, 0B00100000};
int digitArrayOn[4] = {0B11111011, 0B11110111, 0B11101111, 0B11011111};

//Blinking animation: segment-E, segment-D, segment-C, segment-G, segment-F, segment-A, segment-B, segment-G
int blinkingArray[8] = {0B00100000, 0B00010000, 0B00001000, 0B10000000, 0B01000000, 0B00000010, 0B00000100, 0B10000000};

long number = 0;
int recentTime;
int digit1, digit2, digit3, digit4;
int stopButton;

void startTimer();
void stopTimer();
void choseDigit(int x);
void showNumberOnDisplay(int x);
void blinkingAnimation();

int main()
{

  DDRC = 0;
  DDRD = 255;
  DDRB = 0B00111100; //DDRB = 28;

  srand(time(NULL));

  while (1)
  {
    if (number == 0)
    {
      int randomWaitTime = rand() % 10 + 3; //Calls random number between 3-10
      for (int i = 0; i < randomWaitTime; i++)
      {
        blinkingAnimation();
      }
    }
    startTimer();

    if (BUTTONPRESSED)
    {
      stopButton = 0;
      stopTimer();
    }
    
    if (BUTTONPRESSED)
    {
      break;
    }
  }

  return 0;
}

void startTimer()
{
  PORTD = 0B00000000;                   //Turn ALL segments off
  choseDigit(1);                        //Turn Display 1 on
  showNumberOnDisplay((number / 1000)); //Get value of thousand
  _delay_ms(1);                         //Delay 1 ms

  PORTD = 0B00000000;                         //Turn ALL segments off
  choseDigit(2);                              //Turn Display 2 on
  showNumberOnDisplay((number % 1000) / 100); //Get value of hundred
  _delay_ms(1);                               //Delay 1 ms

  PORTD = 0B00000000;                     //Turn ALL segments off
  choseDigit(3);                          //Turn Display 3 on
  showNumberOnDisplay(number % 100 / 10); //Get value of ten
  _delay_ms(1);                           //Delay 1 ms

  PORTD = 0B00000000;               //Turn ALL segments off
  choseDigit(4);                    //Turn Display 3 on
  showNumberOnDisplay(number % 10); //Get value of single digit
  _delay_ms(1);                     //Delay 1 ms

  number++;
  if (number == 9999) // Reset number count to 1;
  {
    number = 1;
  }
}

void stopTimer()
{
  recentTime = number;
  digit1 = recentTime / 1000;
  digit2 = (recentTime % 1000) / 100;
  digit3 = recentTime % 100 / 10;
  digit4 = recentTime % 10;

  while (stopButton == 0)
  {
    PORTD = 0B00000000;          //Turn ALL segments off
    choseDigit(1);               //Turn Display 1 on
    showNumberOnDisplay(digit1); //Get value of thousand
    _delay_ms(1);                //Delay 1 ms

    PORTD = 0B00000000;          //Turn ALL segments off
    choseDigit(2);               //Turn Display 2 on
    showNumberOnDisplay(digit2); //Get value of hundred
    _delay_ms(1);                //Delay 1 ms

    PORTD = 0B00000000;          //Turn ALL segments off
    choseDigit(3);               //Turn Display 3 on
    showNumberOnDisplay(digit3); //Get value of ten
    _delay_ms(1);                //Delay 1 ms

    PORTD = 0B00000000;          //Turn ALL segments off
    choseDigit(4);               //Turn Display 3 on
    showNumberOnDisplay(digit4); //Get value of single digit
    _delay_ms(1);                //Delay 1 ms
  }
}

void choseDigit(int x)
{
  PORTB = 0B11111111;
  switch (x)
  {
  case 1:
    PORTB = digitArrayOn[0]; //Display 1 on
    break;

  case 2:
    PORTB = digitArrayOn[1]; //Display 2 on
    break;

  case 3:
    PORTB = digitArrayOn[2]; //Display 3 on
    break;

  case 4:
    PORTB = digitArrayOn[3]; //Display 4 on
    break;
  }
}

void showNumberOnDisplay(int x)
{
  switch (x)
  {
  case 1:
    PORTD = segmentNumberArray[1];
    break;

  case 2:
    PORTD = segmentNumberArray[2];
    break;

  case 3:
    PORTD = segmentNumberArray[3];
    break;

  case 4:
    PORTD = segmentNumberArray[4];
    break;

  case 5:
    PORTD = segmentNumberArray[5];
    break;

  case 6:
    PORTD = segmentNumberArray[6];
    break;

  case 7:
    PORTD = segmentNumberArray[7];
    break;

  case 8:
    PORTD = segmentNumberArray[8];
    break;

  case 9:
    PORTD = segmentNumberArray[9];
    break;

  default:
    PORTD = segmentNumberArray[0];
    break;
  }
}

void blinkingAnimation()
{
  for (int i = 0; i < 8; i++)
  {
    PORTD = blinkingArray[i];
    _delay_ms(125);
  }
}

【问题讨论】:

    标签: c arduino arduino-uno


    【解决方案1】:

    变量stopButton的值还是0,结果stopButton == 0返回true。 但我认为最好用变量来表示状态。

    #define STATE_COUNT 1
    #define STATE_STOP 2
    int currentState = 1;
    while(1){
      if(currentState == STATE_COUNT && BUTTONPRESSED){
         //do something
    
         currentState = STATE_STOP;
      }
      if(currentState == STATE_STOP && BUTTONPRESSED){
         //do something
    
         currentState = STATE_COUNT;
      }
    }
    

    编辑

    所以,我的想法是使用 STATE 来定义要运行的代码。所以你可以消除第二个while循环。

    while (1) {
      if(currentState == STATE_BLINK){
        blink(); //run code 1 time and change state
        currentState = STATE_COUNT;
      }
      if(currentState == STATE_COUNT){
        startTimer(); 
      }
      if(currentState == STATE_STOP){
        stopTimer(); // while loop inside is removed
      }
    
    
      if (BUTTONPRESSED) {
        //change state here
      }
    }
    
    void stopTimer() {
      recentTime = number;
      digit1 = recentTime / 1000;
      digit2 = (recentTime % 1000) / 100;
      digit3 = recentTime % 100 / 10;
      digit4 = recentTime % 10;
    
      PORTD = 0B00000000;          //Turn ALL segments off
      choseDigit(1);               //Turn Display 1 on
      showNumberOnDisplay(digit1); //Get value of thousand
      _delay_ms(1);                //Delay 1 ms
    
      PORTD = 0B00000000;          //Turn ALL segments off
      choseDigit(2);               //Turn Display 2 on
      showNumberOnDisplay(digit2); //Get value of hundred
      _delay_ms(1);                //Delay 1 ms
    
      PORTD = 0B00000000;          //Turn ALL segments off
      choseDigit(3);               //Turn Display 3 on
      showNumberOnDisplay(digit3); //Get value of ten
      _delay_ms(1);                //Delay 1 ms
    
      PORTD = 0B00000000;          //Turn ALL segments off
      choseDigit(4);               //Turn Display 3 on
      showNumberOnDisplay(digit4); //Get value of single digit
      _delay_ms(1);                //Delay 1 ms 
    }
    
    void blink(){
     if (number == 0) {
      int randomWaitTime = rand() % 10 + 3; //Calls random number between 3-10
      for (int i = 0; i < randomWaitTime; i++) {
        blinkingAnimation();
      }
     }
    }
    

    【讨论】:

    • 我什至尝试使用 if 语句添加 stopButton = 1 来停止 while 循环,无论是否有 break 或 return 以强制它以某种方式退出。然而,这根本没有帮助,只是导致了其他不受欢迎的行为。为我缺乏知识而道歉,因为我是 C 的新手。我见过类似的东西,但我只是不知道如何实现它。我试过了,但没有成功 = 在我包含的if(currentState == STATE_COUNT &amp;&amp; BUTTONPRESSED) 语句中:stopButton = 0, stopTime();,而在第二个语句中我不知道要包含什么,只是添加了 break;。困惑,对不起伙计。
    • Hermenstian IW - 我尝试了各种方式和组合。无法让它工作:/。它只是破坏了代码,一切都变得空白。不知道如何实现上述。感谢您能否详细说明。
    • 对不起,如果我不能解释清楚,我编辑了我的答案。
    • 我试过你上面的例子还是不行......这太令人沮丧了!我想知道,这可能是问题所在的按钮吗?但是我看不到那里有什么问题,并且按钮应该可以使用 Bit-operators 来激活它并且不需要任何类型的重置?我用一个新块编辑了你的答案,这样你就可以看到我在尝试你建议的代码时做了什么。看看那里有没有什么奇怪的事情发生?
    【解决方案2】:

    这里终于解决了我上面的问题。以为我发布它以防其他人遇到类似问题:

    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <avr/io.h>
    #include <util/delay.h>
    
    #define BUTTONPRESSED PINC &(1 << PC0)
    
    /*
        - PORT = D
        Segment-A   Pin 1   PD1     0B00000010
        Segment-B   Pin 2   PD2     0B00000100
        Segment-C   Pin 3   PD3     0B00001000
        Segment-D   Pin 4   PD4     0B00010000
        Segment-E   Pin 5   PD5     0B00100000
        Segment-F   Pin 6   PD6     0B01000000
        Segment-G   Pin 7   PD7     0B10000000
    
        - PORT = B
        Digit 1     Pin 10  PB2     0B00000100
        Digit 2     Pin 11  PB3     0B00001000
        Digit 3     Pin 12  PB4     0B00010000
        Digit 4     Pin 13  PB5     0B00100000
    
        - PORT = C
        Button      Pin A0  PC0     0B00000001
        */
    
    //Number: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9  in a array
    int segmentNumberArray[10] = {0B01111110, 0B00001100, 0B010110110, 0B10011110, 0B011001100, 0B11011010, 0B11111010, 0B00001110, 0B11111110, 0B11011110};
    
    //Digit: 1, 2, 3, 4   in a array
    int digitArrayOff[4] = {0B00000100, 0B00001000, 0B00010000, 0B00100000};
    int digitArrayOn[4] = {0B11111011, 0B11110111, 0B11101111, 0B11011111};
    
    //Blinking animation: segment-E, segment-D, segment-C, segment-G, segment-F, segment-A, segment-B, segment-G
    int blinkingArray[8] = {0B00100000, 0B00010000, 0B00001000, 0B10000000, 0B01000000, 0B00000010, 0B00000100, 0B10000000};
    
    long number = 0;
    int recentTime;
    int digit1, digit2, digit3, digit4;
    int isCounting = 0;
    int isButtonDown = 0;
    
    void startTimer();
    void stopTimer();
    void choseDigit(int x);
    void showNumberOnDisplay(int x);
    void blinkingAnimation();
    
    int main()
    {
    
      DDRC = 0; // För knappen -->> alt. DDRC&= ~(1 << PC0);
      DDRD = 255;
      DDRB = 0B00111100; //DDRB = 28; -->> detta är för Displayerna
    
      srand(time(NULL));
      blinkingAnimation();
    
      while (1)
      {
        if (isCounting == 1)
        {
          startTimer();
        }
    
        if (BUTTONPRESSED)
        {
          if (isButtonDown == 0)  //Button has not been pressed before
          {
            isButtonDown = 1;
            if (isCounting == 0)
            {
              
              blinkingAnimation();
            }
            else
            {
            isCounting = 0;
            }
          }
        }
        else
        {
          isButtonDown = 0;  //Button has been released
        }
    
        stopTimer();
      }
    
      return 0;
    }
    
    void startTimer()
    {
      PORTD = 0B00000000;                   //Turn ALL segments off
      choseDigit(1);                        //Turn Display 1 on
      showNumberOnDisplay((number / 1000)); //Get value of thousand
      _delay_ms(2.3);                       //Delay 1 ms
    
      PORTD = 0B00000000;                         //Turn ALL segments off
      choseDigit(2);                              //Turn Display 2 on
      showNumberOnDisplay((number % 1000) / 100); //Get value of hundred
      _delay_ms(2.3);                             //Delay 1 ms
    
      PORTD = 0B00000000;                     //Turn ALL segments off
      choseDigit(3);                          //Turn Display 3 on
      showNumberOnDisplay(number % 100 / 10); //Get value of ten
      _delay_ms(2.3);                         //Delay 1 ms
    
      PORTD = 0B00000000;               //Turn ALL segments off
      choseDigit(4);                    //Turn Display 4 on
      showNumberOnDisplay(number % 10); //Get value of single digit
      _delay_ms(2.3);                   //Delay 1 ms
    
      number++;
      if (number > 5999) // Reset number count to 1;
      {
        number = 1;
      }
    }
    
    void stopTimer()
    {
      recentTime = number;
      digit1 = recentTime / 1000;
      digit2 = (recentTime % 1000) / 100;
      digit3 = recentTime % 100 / 10;
      digit4 = recentTime % 10;
    
        PORTD = 0B00000000;          //Turn ALL segments off
        choseDigit(1);               //Turn Display 1 on
        showNumberOnDisplay(digit1); //Get value of thousand
        _delay_ms(1);                //Delay 1 ms
    
        PORTD = 0B00000000;          //Turn ALL segments off
        choseDigit(2);               //Turn Display 2 on
        showNumberOnDisplay(digit2); //Get value of hundred
        _delay_ms(1);                //Delay 1 ms
    
        PORTD = 0B00000000;          //Turn ALL segments off
        choseDigit(3);               //Turn Display 3 on
        showNumberOnDisplay(digit3); //Get value of ten
        _delay_ms(1);                //Delay 1 ms
    
        PORTD = 0B00000000;          //Turn ALL segments off
        choseDigit(4);               //Turn Display 3 on
        showNumberOnDisplay(digit4); //Get value of single digit
        _delay_ms(1);                //Delay 1 ms
    }
    
    void choseDigit(int x)
    {
      PORTB = 0B11111111;
      switch (x)
      {
      case 1:
        PORTB = digitArrayOn[0]; //Display 1 on
        break;
    
      case 2:
        PORTB = digitArrayOn[1]; //Display 2 on
        break;
    
      case 3:
        PORTB = digitArrayOn[2]; //Display 3 on
        break;
    
      case 4:
        PORTB = digitArrayOn[3]; //Display 4 on
        break;
      }
    }
    
    void showNumberOnDisplay(int x)
    {
      switch (x)
      {
      case 1:
        PORTD = segmentNumberArray[1];
        break;
    
      case 2:
        PORTD = segmentNumberArray[2];
        break;
    
      case 3:
        PORTD = segmentNumberArray[3];
        break;
    
      case 4:
        PORTD = segmentNumberArray[4];
        break;
    
      case 5:
        PORTD = segmentNumberArray[5];
        break;
    
      case 6:
        PORTD = segmentNumberArray[6];
        break;
    
      case 7:
        PORTD = segmentNumberArray[7];
        break;
    
      case 8:
        PORTD = segmentNumberArray[8];
        break;
    
      case 9:
        PORTD = segmentNumberArray[9];
        break;
    
      default:
        PORTD = segmentNumberArray[0];
        break;
      }
    }
    
    void blinkingAnimation()
    {
      PORTB = digitArrayOn[4];
      //Random wait time:
      int randomWaitTime = rand() % 10 + 3; //Calls random number between 3-10 seconds
    
      for (int i = 0; i < randomWaitTime; i++)
      {
        //Blink animation:
        for (int i = 0; i < 8; i++)
        {
          PORTD = blinkingArray[i];
          _delay_ms(125);
        }
      }
      number = 0;
      isCounting = 1;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 2020-05-25
      • 2020-01-23
      • 1970-01-01
      相关资源
      最近更新 更多