3503| 2
|
[求助] 求大神指点迷津 |
我想做一个刷卡 充电 的程序 刷一次卡 3小时 刷3次卡是9小时 然后这个刷卡次数可以循环 这个时间时根据刷卡的次数去倒计时 现在是刷一次卡就开始计时了 刷第二次已经没有反应了 怎么解决 求指点 #include <SPI.h> #include <RFID.h> RFID rfid(10, 5); //D10--读卡器MOSI引脚、D5--读卡器RST引脚 String N = ""; //字符串转整型用的变量 long Frequency = 800;//频率 unsigned char* ID = rfid.serNum;//无符号字符型指针 unsigned long Record; //用于记录开始时间 unsigned long Now; //当前时间 unsigned long deleyTime = "";//3小时 int addNum = 0; //写入读卡次数 int redNum = 0; //读取读卡次数 int Buzzer = 5;//蜂鸣器针脚 int Green = 3; //正常启动指示灯 int Blue = 2; //读卡指示灯 int Red1 = A2; //读卡一次 int Red2 = A1; //读卡二次 int Red3 = A0; //读卡三次 int Relay = 3; //继电器 void setup() { Serial.begin(9600); SPI.begin(); rfid.init(); pinMode(Buzzer, OUTPUT); pinMode(Green,OUTPUT); pinMode(Blue, OUTPUT); pinMode(Red1, OUTPUT); pinMode(Red2, OUTPUT); pinMode(Red3, OUTPUT); pinMode(Relay, OUTPUT); digitalWrite(Green,HIGH); } void loop() { //找卡 rfid.isCard(); //读取卡序列号 if (rfid.readCardSerial()) { Serial.print("The card's number is : "); Serial.print(rfid.serNum[0], HEX); Serial.print(rfid.serNum[1], HEX); Serial.print(rfid.serNum[2], HEX); Serial.print(rfid.serNum[3], HEX); Serial.print(rfid.serNum[4], HEX); Serial.println(" "); //刷卡指示灯 digitalWrite(Blue,HIGH); delay(200); digitalWrite(Blue,LOW); delay(200); digitalWrite(Blue,HIGH); delay(200); digitalWrite(Blue,LOW); //运行检测卡程序 ShowUser(); Times(); } } void ShowUser() { //39 2C 5A 29 66 白卡 if ( ID[0] == 0x39 && ID[1] == 0x2C && ID[2] == 0x5A && ID[3] == 0x29 ) { //刷卡声音 tone(Buzzer, Frequency); delay(100); noTone(Buzzer); delay(100); tone(Buzzer, Frequency); delay(100); noTone(Buzzer); //刷卡次数增加 addNum++; //串口打印信息 Serial.println("Hello Yzp!"); Serial.println("addNum is :"); Serial.print(addNum); } //2D EA DB D9 C5 纽扣卡 else if (ID[0] == 0x2D && ID[1] == 0xEA && ID[2] == 0xDB && ID[3] == 0xD9) { //刷卡声音 tone(Buzzer, Frequency); delay(100); noTone(Buzzer); delay(100); tone(Buzzer, Frequency); delay(100); noTone(Buzzer); //刷卡次数增加 addNum++; //串口打印信息 Serial.println("Hello Yzz!"); Serial.print("addNum is :"); Serial.println(addNum); } else { //无卡声音 tone(Buzzer, Frequency); delay(1000); noTone(Buzzer); //串口打印信息 Serial.println("Hello unkown guy!"); } //刷卡次数 if (addNum > 3) { addNum = 0; } //读计数亮灯 if ( addNum == 0) { //指示灯 digitalWrite(Red1, LOW); digitalWrite(Red2, LOW); digitalWrite(Red3, LOW); //停止倒计时 关闭继电器 } else if ( addNum == 1) { digitalWrite(Red1, HIGH); digitalWrite(Red2, LOW); digitalWrite(Red3, LOW); //开始倒计时三小时 deleyTime = 180000; } else if ( addNum == 2) { digitalWrite(Red1, HIGH); digitalWrite(Red2, HIGH); digitalWrite(Red3, LOW); //开始倒计时六小时 deleyTime = 360000; } else if ( addNum == 3) { digitalWrite(Red1, HIGH); digitalWrite(Red2, HIGH); digitalWrite(Red3, HIGH); //开始倒计时九小时 deleyTime = 540000; } } void Times() { Record = millis (); //记录当前时间 Now = millis ();//现在时间 while (Now - Record < deleyTime ) { Now = millis (); } } |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed