我正在尝试使用arduino uno,步进电机,funduino rfid-rc522制作Arduino rfid门锁。这是代码:
//declare variables for the motor pins
int motorPin1 = 6; // Blue - 28BYJ48 pin 1
int motorPin2 = 7; // Pink - 28BYJ48 pin 2
int motorPin3 = 8; // Yellow - 28BYJ48 pin 3
int motorPin4 = 9; // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 (VCC)
int motorSpeed = 1500
; //variable to set stepper speed
int count = 0; // count of steps made
int countsperrev = 12; // number of steps per full revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
#include <RFID.h>
/*
* Read a card using a mfrc522 reader on your SPI interface
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS: Pin 10
* RST: Pin 5
*/
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <RFID.h>
#define SS_PIN 10
#define RST_PIN 5
RFID rfid(SS_PIN,RST_PIN);
int serNum[5];
#define I2C_ADDR 0x27 // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
int n = 1;
int a = 0;
int DA = A0;
int DO = 2;
int state = 0;
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup(){
//declare the motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600);
SPI.begin();
rfid.init();
lcd.begin (16,2); // <<----- My LCD was 16x2
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
}
void loop(){
if(rfid.isCard()){
if(rfid.readCardSerial()) {
Serial.print(rfid.serNum[0],DEC);
/*Serial.print(" ");
Serial.print(rfid.serNum[1],DEC);
Serial.print(" ");
Serial.print(rfid.serNum[2],DEC);
Serial.print(" ");
Serial.print(rfid.serNum[3],DEC);
Serial.print(" ");
Serial.print(rfid.serNum[4],DEC);
Serial.println("");
*/
if(rfid.serNum[0]==199){
if (state == 0);{
Serial.print("opennnnnn");
Serial.println("");
lcd.setCursor(3,0);
lcd.print("Door Open!");
delay(1000);
lcd.home();
while (a<200){
if(count < countsperrev )
clockwise();
else if (count == countsperrev * 2)
count = 0;
else
count++;
delay(0);
a=a+1;
}
lcd.clear();
delay(5000);
a = 0;
state = 1;
}
if (state == 1) {
Serial.print("Door already open");
lcd.print("Door already open");
}
}
else{
if (state == 1){
Serial.print("closeeee");
Serial.println("");
lcd.setCursor(2,0);
lcd.print("Door Closed!");
delay(1000);
lcd.home();
while (a<200){
if(count < countsperrev )
anticlockwise();
else if (count == countsperrev * 2)
count = 0;
else
count++;
delay(0);
a=a+1;
}
lcd.clear();
delay(5000);
a = 0;
state = 0;
}
if (state == 0) {
lcd.print("Door Already Closed");
Serial.print("Door Already Closed");
}
rfid.halt();
}
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)
void anticlockwise()
{
for(int i = 0; i < 8; i++)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}
void clockwise()
{
for(int i = 7; i >= 0; i--)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}
void setOutput(int out)
{
digitalWrite(motorPin1, bitRead(lookup[out], 0));
digitalWrite(motorPin2, bitRead(lookup[out], 1));
digitalWrite(motorPin3, bitRead(lookup[out], 2));
digitalWrite(motorPin4, bitRead(lookup[out], 3));
}
我希望它只能打开一次门,然后必须再次打开,但是当我添加if (state == 0)
state = 1;
if (state == 1)
{{1}时} if (state == 1)
arduino IDE给了我一个错误:
if (state == 0)
我研究了函数的语法,看起来是正确的,我不知道发生了什么。
如果有人可以提供帮助,请这样做。
干杯
答案 0 :(得分:2)
您在loop()
内遗漏了一个或多个近距离大括号。验证所有打开的大括号在预期位置是否具有相应的闭括号。