Arduino - 在'{'标记之前不允许在此处进行函数定义
Arduino - Function-definition is not allowed here before '{' token
我正在尝试用 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)
if (state == 1)
if (state == 0)
arduino IDE 给了我一个错误:
Arduino: 1.6.5 (Windows 7), Board: "Arduino Uno"
sketch_sep07d.ino: In function 'void loop()':
sketch_sep07d:158: error: a function-definition is not allowed here before '{' token
sketch_sep07d:181: error: expected '}' at end of input
sketch_sep07d:181: error: expected '}' at end of input
sketch_sep07d:181: error: expected '}' at end of input
a function-definition is not allowed here before '{' token
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
我研究了函数的语法,它似乎是正确的,我不知道发生了什么。
如果有人可以提供帮助,请提供帮助。
干杯
您在 loop()
中缺少一个或多个大括号。确认所有左大括号在预期位置都有相应的右大括号。
我正在尝试用 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)
if (state == 1)
if (state == 0)
arduino IDE 给了我一个错误:
Arduino: 1.6.5 (Windows 7), Board: "Arduino Uno"
sketch_sep07d.ino: In function 'void loop()':
sketch_sep07d:158: error: a function-definition is not allowed here before '{' token
sketch_sep07d:181: error: expected '}' at end of input
sketch_sep07d:181: error: expected '}' at end of input
sketch_sep07d:181: error: expected '}' at end of input
a function-definition is not allowed here before '{' token
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
我研究了函数的语法,它似乎是正确的,我不知道发生了什么。
如果有人可以提供帮助,请提供帮助。
干杯
您在 loop()
中缺少一个或多个大括号。确认所有左大括号在预期位置都有相应的右大括号。