Arduino继电器按钮

时间:2016-08-14 00:23:04

标签: button arduino switch-statement

我的继电器开关出现问题。我有一个5V,四个继电器开关,用于Arduino。我试图做到这一点,当我按下一个按钮时,一个继电器继续,然后,当我再次按下它时,同一个继电器熄灭。

使用一个中继时,此概念适用于代码。但是,问题是我的代码仅适用于一个中继和一个中继。如果我更改代码并生成多个变量,它将无效。

仅供参考,我使用的是Arduino UNO R3 ATmega 328

请记住,第一个代码确实有效,但只适用于一个中继。当我按下按钮将其打开然后再次按下该按钮将其关闭时,它可以正常工作。

const int rl1 = 7;
const int rl2 = 12;
const int rl3 = 2;
const int rl4 = 8;
const int button1 = 11;
const int button2 = 10;
const int button3 = 3;
const int button4 = 4;

int rl1State = LOW;
int rl2State = LOW;
int rl3State = LOW;
int rl4State = LOW;
int buttonState = LOW;
int lastButtonState = HIGH;
int reading;

long lastDebounceTime=0;
long debounceDelay = 50;


void setup() {

  Serial.begin(9600);
  pinMode(rl1, OUTPUT);
  pinMode(rl2, OUTPUT);
  pinMode(rl3, OUTPUT);
  pinMode(rl3, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);

}

void loop() {

  reading = digitalRead(button1);


  if(reading != lastButtonState){
    lastDebounceTime = millis();
    lastButtonState = reading;
  }

  if((millis() - lastDebounceTime) > debounceDelay){
    if(buttonState != lastButtonState){
      buttonState = lastButtonState;
      if(buttonState == HIGH){
        rl1State = !rl1State;
        digitalWrite(rl1, rl1State);


      }
    }
  }



}

我为多个继电器尝试了这个代码:

const int rl1 = 7;
const int rl2 = 12;
const int rl3 = 2;
const int rl4 = 8;
const int button1 = 11;
const int button2 = 10;
const int button3 = 3;
const int button4 = 4;

int rl1State = LOW;
int rl2State = LOW;
int rl3State = LOW;
int rl4State = LOW;


//States
int buttonState1 = LOW;
int lastButtonState1 = HIGH;
int buttonState2 = LOW;
int lastButtonState2 = HIGH;
int buttonState3 = LOW;
int lastButtonState3 = HIGH;
int buttonState4 = LOW;
int lastButtonState4 = HIGH;

//Read State

int reading1;
int reading2;
int reading3;
int reading4;

long lastDebounceTime1=0;
long debounceDelay1 = 50;

long lastDebounceTime2=0;
long debounceDelay2 = 50;

long lastDebounceTime3=0;
long debounceDelay3= 50;

long lastDebounceTime4=0;
long debounceDelay4 = 50;


void setup() {

  Serial.begin(9600);
  pinMode(rl1, OUTPUT);
  pinMode(rl2, OUTPUT);
  pinMode(rl3, OUTPUT);
  pinMode(rl3, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);

}

void loop() {

  reading1 = digitalRead(button1);
  reading2 = digitalRead(button2);
  reading3 = digitalRead(button3);
  reading4 = digitalRead(button4);


//Relay 1
  if(reading1 != lastButtonState1){
    lastDebounceTime1 = millis();
    lastButtonState1 = reading1;
  }
    if(reading1 != lastButtonState1){
    lastDebounceTime1 = millis();
    lastButtonState1 = reading1;
  }
  if((millis() - lastDebounceTime1) > debounceDelay1){
    if(buttonState1 != lastButtonState1){
      buttonState1 = lastButtonState1;
      if(buttonState1 == HIGH){
        rl1State = !rl1State;
        digitalWrite(rl1, rl1State);


      }
    }
  }


  //Relay 2
  if(reading2 != lastButtonState2){
    lastDebounceTime2 = millis();
    lastButtonState2 = reading2;
  }

  if((millis() - lastDebounceTime2) > debounceDelay2){
    if(buttonState2 != lastButtonState2){
      buttonState2 = lastButtonState2;
      if(buttonState2 == HIGH){
        rl2State = !rl2State;
        digitalWrite(rl2, rl2State);


      }
    }
  }



}

我还试图为每个按钮和继电器重新制作所有变量,但它仍然不起作用。

另一方面,我的一个继电器在放入任何引脚时都没有工作(当所有引脚都已连接时),但只有当其中一个引脚断开时才能工作。这真的很奇怪。我测试了继电器,它很好,我改变了Arduino,但仍然有同样的问题。

1 个答案:

答案 0 :(得分:0)

AH!这将在这里工作的是所有有这个问题的人的代码和原理图(在互联网上看到很多确实有这个问题),以及谢谢你帮助解决这个问题!享受!

Circuit (Please note the relay cluster I am using was not available on Fritzing)

代码:

//Buttons

int button1 = 7;
int button2 = 6;
int button3 = 4;
int button4 = 2;


//Relays
int rl1 = 13;
int rl2 = 12;
int rl3 = 11;
int rl4 = 8;


//States for Relay and Button (1)

int state1 = HIGH;      // the current state of the output pin
int reading1;           // the current reading from the input pin
int previous1 = LOW;    // the previous reading from the input pin

//States for Relay and Button (2)

int state2 = HIGH;      // the current state of the output pin
int reading2;           // the current reading from the input pin
int previous2 = LOW;    // the previous reading from the input pin

//States for Relay and Button (3)

int state3 = HIGH;      // the current state of the output pin
int reading3;           // the current reading from the input pin
int previous3 = LOW;    // the previous reading from the input pin

//States for Relay and Button (4)

int state4 = HIGH;      // the current state of the output pin
int reading4;           // the current reading from the input pin
int previous4 = LOW;    // the previous reading from the input pin




// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time1 = 0;          // the last time the output pin was toggled
long time2 = 0;
long time3 = 0;
long time4 = 0;

long debounce1 = 200;   // the debounce time, increase if the output flickers
long debounce2 = 200;
long debounce3 = 200;
long debounce4 = 200;


void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);

  pinMode(rl1, OUTPUT);
  pinMode(rl2, OUTPUT);
  pinMode(rl3, OUTPUT);
  pinMode(rl4, OUTPUT);

}

void loop() {

  reading1 = digitalRead(button1);
  reading2 = digitalRead(button2);
  reading3 = digitalRead(button3);
  reading4 = digitalRead(button4);


  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  //Condition Relay 1
  if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce1) {
    if (state1 == HIGH)
      state1 = LOW;
    else
      state1 = HIGH;

    time1 = millis();    
  }

  //Condition Relay 2
    if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce2) {
    if (state2 == HIGH)
      state2 = LOW;
    else
      state2 = HIGH;

    time2 = millis();    
  }

  //Condition Relay 3
    if (reading3 == HIGH && previous3 == LOW && millis() - time3 > debounce3) {
    if (state3 == HIGH)
      state3 = LOW;
    else
      state3 = HIGH;

    time3 = millis();    
  }

  //Condition Relay 4
    if (reading4 == HIGH && previous4 == LOW && millis() - time4 > debounce4) {
    if (state4 == HIGH)
      state4 = LOW;
    else
      state4 = HIGH;

    time4 = millis();    
  }




  digitalWrite(rl1, state1);
  digitalWrite(rl2, state2);
  digitalWrite(rl3, state3);
  digitalWrite(rl4, state4);


  previous1 = reading1;
  previous2 = reading2;
  previous3 = reading3;
  previous4 = reading4;
}