Arduino Mega:如何使用相同的经过验证的 RFID 标签并使用按钮来切换 LED 的颜色?

Arduino Mega: How do I switch an LED's colour using the same authenticated RFID tag and using a button too?

我在让我的代码在我的 Arduino Mega 上执行我想要的操作时遇到了一些问题。我的目标是让相同的 RFID 标签将 LED 颜色从红色变为绿色,并在再次显示时从绿色变为红色。我也想用一个按钮来做到这一点。这是用于门锁,以便可以从门的两侧锁定和解锁门。

下面的代码将允许 RFID 标签将颜色从红色更改为绿色,反之亦然,但是当我尝试测试经过身份验证的卡时,它就惨遭失败。此外,当我尝试向其添加按钮时,我的代码最终变得非常混乱且无法使用。我尝试了各种方法,但我认为我的主要问题是因为 loop 函数中的代码站点。我曾尝试 while(1) 尝试将 LED 设置为特定颜色,但它无法正常工作。

我认为这很容易做到,也许是,我对此还不够熟悉。

任何人都可以帮我指出正确的方向,这将帮助我在这方面取得进一步进展吗?

#include <SPI.h>
#include <MFRC522.h>

// constants won't change. They're used here to set pin numbers:
const int BUTTON_PIN = 2; // the number of the pushbutton pin
#define BLUE 7
#define GREEN 6
#define RED 3
#define RST_PIN 9     // Configurable, see typical pin layout above
#define SS_PIN  53   // Configurable, see typical pin layout above

// Variables will change:
String authKeyFob = "54321098765";
String card_ID="";

//int lastState = HIGH; // the previous state from the input pin
//int currentState = LOW;    // the current reading from the input pin

int buttonState      = 0;     // current state of the button
int lastButtonState  = 0;     // previous state of the button
int ledState         = 0;     // remember current led state
int rfidState = 0;
int lastRfidState = 0;
bool toggle = false;
bool cardValid = false;

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  while (!Serial);     // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();         // Init SPI bus
  mfrc522.PCD_Init();  // Init MFRC522 card

  // initialize the pushbutton pin as an pull-up input
  // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
  pinMode(BUTTON_PIN, INPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(RED, OUTPUT);
  pinMode(BLUE, OUTPUT);
}

void loop() {

    // read the state of the switch/button:
    buttonState = digitalRead(BUTTON_PIN);

  // Look for new cards, and select one if present
  if (mfrc522.PICC_IsNewCardPresent() ||  mfrc522.PICC_ReadCardSerial() ) {

    if(toggle == false){
      toggle = true;  
    } else {
      toggle = false;
    }

    delay(200);
  }


    if(toggle == 1){
        digitalWrite(GREEN, HIGH);
        digitalWrite(RED, LOW);  
    } else if (toggle == 0) {
        digitalWrite(GREEN, LOW);
        digitalWrite(RED, HIGH);
    }

}

您需要做的就是使用另一个 if 语句来读取您的按钮并切换您的切换变量,就像您对卡片所做的那样。看看你是否明白这里发生了什么:

void loop() {

  // read the state of the switch/button:
  oldButtonState = LOW;    //  NEED TO ADD A DECLARATION FOR THIS BEFORE SETUP
  buttonState = digitalRead(BUTTON_PIN);

  // Look for new cards, and select one if present
  if (mfrc522.PICC_IsNewCardPresent() ||  mfrc522.PICC_ReadCardSerial() ) {

    toggle = !toggle;   // A way easier way to write the if statements you had.  
  }
  //  if the button just became pressed...
  if(buttonState == HIGH && oldButtonState==LOW){
    toggle = !toggle;  // same thing, toggle our variable. 
  }
  oldButtonState = buttonState;  // save the button state for next time


  if (toggle) {
    digitalWrite(GREEN, HIGH);
    digitalWrite(RED, LOW);
  } else {
    digitalWrite(GREEN, LOW);
    digitalWrite(RED, HIGH);
  }

}

查看状态更改示例以了解我存储按钮旧状态的原因。我还更改了那组 if 语句以将切换设置为不切换,这是反转布尔值的更简单方法。我也做了最后一个 else if just an else 因为布尔值只能有两个值。如果它不是真的那么它一定是假的。

这是我的更新版本,RFID 标签与按钮和 LED 一致工作。

void loop() {

  // read the state of the switch/button:
  oldButtonState = LOW;    //  NEED TO ADD A DECLARATION FOR THIS BEFORE SETUP
  buttonState = digitalRead(BUTTON_PIN);

  // Look for new cards, and select one if present
  if (!mfrc522.PICC_IsNewCardPresent() ) {

  }
  
  if(mfrc522.PICC_ReadCardSerial() ){
    toggle = !toggle;
    delay(200);
  }

 
  //  if the button just became pressed...
  if(buttonState == HIGH && oldButtonState==LOW){
    toggle = !toggle;  // same thing, toggle our variable. 
     delay(200);
  }
  
  oldButtonState = buttonState;  // save the button state for next time


  if (toggle) {
    digitalWrite(GREEN, HIGH);
    digitalWrite(RED, LOW);
  } else {
    digitalWrite(GREEN, LOW);
    digitalWrite(RED, HIGH);
  }

}