一个按钮可选择4个继电器中的一个,同一个按钮用于激活所选的继电器

时间:2015-10-13 15:26:40

标签: arduino

我希望能够用一个按钮控制四个继电器 (这适用于行动不便的残疾朋友,这将控制他的客厅躺椅。)

我希望能够按下按钮一次,两次,三次或四次以选择将激活哪个继电器。短暂停顿后,按下相同的按钮应激活所选的继电器。

因此,按下按钮两次将导致LED闪烁两次,然后短暂停顿,然后再次闪烁两次等,以指示继电器#2是"选择"。一旦满足该条件,再次按下相同的按钮将在按钮按下的持续时间内激活该继电器。释放按钮后,程序应返回循环,寻找再次按下按钮以选择不同的继电器。

我将一些代码混合在一起并使其部分工作。当我尝试按下按钮激活继电器时,问题就开始了,变量PushButtonCounter递增,并认为我选择了下一个继电器,实际上,我想激活先前选择的继电器。

我该如何解决这个问题?

(我知道我的代码很难看,并且没有遵循公认的C编码实践。 我也可能遗漏了一堆重要信息。可能有更好的方法来编码我在下面列出的代码块。)

其他信息: 我按照建议移除了unsigned long,代码仍然可以工作到突破循环并激活选定的中继。例如,按下三个按钮后,PushButtonCounter中存储的值为" 3",应该如此。这在switch语句中进行评估,并降至case 3:。 LED按我的要求闪烁,三次快速闪烁,然后暂停,三次快速闪烁,然后暂停。这正如我所期望的那样在循环中循环。但是,接下来要做的是按住按钮激活三号继电器。相反,PushButtonCounter现在被设置为" 4"并且switch语句现在评估为case 4:。我需要以某种方式将PushButtonCounter设置为"粘性"在一小段时间之后,说不超过十秒并且不改变,所以当我下次按下按钮时,它被检测为"在按钮被按住的持续时间内激活所选择的继电器&#34 ;。释放按钮后,程序应返回循环开始并等待更多按钮按下以选择要激活的新继电器。

// Constant values that won't change.
  const int Relay_1 = 2;
  const int Relay_2 = 3;
  const int Relay_3 = 4;
  const int Relay_4 = 5;
  const int LED = 6;
  const int PushButton = 7;

// Variable values that will change.
  int PushButtonCounter = 0;           // Counter for the number of push button presses.
  int CurrentButtonState = 0;          // Current state of the push button.
  int LastButtonState = 0;             // Previous state of the push button.
  unsigned long ButtonPressed;

void setup()
  {
    pinMode(Relay_1, OUTPUT);
    pinMode(Relay_2, OUTPUT);
    pinMode(Relay_3, OUTPUT);
    pinMode(Relay_4, OUTPUT);
    pinMode(LED, OUTPUT);
    pinMode(PushButton, INPUT);
    digitalWrite(Relay_1, LOW);
    digitalWrite(Relay_2, LOW);
    digitalWrite(Relay_3, LOW);
    digitalWrite(Relay_4, LOW);
    digitalWrite(LED, LOW);
  }

void loop()
  {
    CurrentButtonState = digitalRead(PushButton);        // Read the push button input pin.
    if (CurrentButtonState != LastButtonState)           // Compare the PushButtonState to its previous state.
      {
        if (CurrentButtonState == HIGH)                  // If the current state is HIGH then the button went from off to on.
          {
            ButtonPressed = millis();
            PushButtonCounter++;                         // If the state has changed, increment the counter.
          }
        delay(50);                                       // Delay a little bit to avoid bouncing.
      }
    LastButtonState = CurrentButtonState;                // Save the current state as the last state for next time through the loop.
    if (PushButtonCounter == 5) PushButtonCounter = 0;
    if (millis() > ButtonPressed + 2000)
      {
         switch (PushButtonCounter)
          {
            case 1:
              digitalWrite(LED, HIGH);
              delay(100);
              digitalWrite(LED, LOW);
            break;
            case 2:
              digitalWrite(LED, HIGH);
              delay(100);
              digitalWrite(LED, LOW);
              delay(500);
              digitalWrite(LED, HIGH);
              delay(100);
              digitalWrite(LED, LOW);
            break;  
            case 3:
              digitalWrite(LED, HIGH);
              delay(100);
              digitalWrite(LED, LOW);
              delay(500);
              digitalWrite(LED, HIGH);
              delay(100);
              digitalWrite(LED, LOW);              
              delay(500);
              digitalWrite(LED, HIGH);
              delay(100);
              digitalWrite(LED, LOW);
            break;  
            case 4:
              digitalWrite(LED, HIGH);
              delay(100);
              digitalWrite(LED, LOW);
              delay(500);
              digitalWrite(LED, HIGH);
              delay(100);
              digitalWrite(LED, LOW);
              delay(500);
              digitalWrite(LED, HIGH);
              delay(100);
              digitalWrite(LED, LOW);
              delay(500);
              digitalWrite(LED, HIGH);
              delay(100);
              digitalWrite(LED, LOW);
            break;
          }
        ButtonPressed = millis();  
      }
  }

0 个答案:

没有答案