Hello All,
Im working on one of my msc project, i have adopted one of the codes and tried to change it according to my requirement.
The full code is below:
int val = 0;
int bytesread = 0;
int redPin = 13; // Connect red LED to pin 13
int grnPin = 8; // Connect green LED to pin 8
int buzPin = 7; // Connect buzzer to pin 7
int rfidPin = 2; // RFID enable pin connected to digital pin 2
long time;
long lastUpdate=0;
char code[12];
char tagArray[12]; // the array for the tags
char User[12] = {
'3', 'A', '0', '0', '0', '6', 'D', '2', 'B', '9'};
// '3', 'A', '0', '0', '0', '6', 'D', '2', 'B', '9'
void setup() {
Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
pinMode(rfidPin,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
pinMode(redPin,OUTPUT); // Set ledPin to output
pinMode(grnPin,OUTPUT);
pinMode(buzPin,OUTPUT);
digitalWrite(rfidPin, LOW); // Activate the RFID reader
}
void loop() {
digitalWrite(buzPin,0);
time = millis();
Serial.print("time (loop starts again)= ");
Serial.print(time);
Serial.println();
delay(1000);
if(Serial.available() > 0) { // if data available from reader
if((val = Serial.read()) == 10) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
if( Serial.available() > 0) {
val = Serial.read();
if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) { // if 10 digit read is complete
digitalWrite(rfidPin, HIGH); // deactivate RFID reader
if(strcmp(code,User) == 0) {
Serial.println("Kishan is in range");
Serial.print("TAG code is: "); // possibly a good TAG
Serial.println(code); // print the TAG code
lastUpdate = millis();
Serial.print("time elapsed = ");
Serial.println(time);
digitalWrite(grnPin,HIGH); // good read light green LED
digitalWrite(redPin,LOW); // turn off red LED
digitalWrite(rfidPin,HIGH); // turn off rfid LED
}
if(strcmp(code,User) !=0) {
Serial.println("Unidentified Person Alert");
Serial.println(code);
lastUpdate = millis();
Serial.print("time two = ");
Serial.println(time);
digitalWrite(redPin,HIGH); // not recognized tag light red LED
digitalWrite(grnPin,LOW); // turn off green LED
digitalWrite(rfidPin,HIGH); // turn off rfid LED
Serial.print("delay starts now");
Serial.println();
delay(1000);
}
}
bytesread = 0;
Serial.print("rfid activates now");
Serial.println();
digitalWrite(rfidPin,LOW); // turn off green LED
}
}
if(((time - lastUpdate) >= 50000) && ((time - lastUpdate) <= 60000)) {
Serial.print("checking for kishan");
Serial.println();
blinks();
}
else if ((time - lastUpdate) >= 60000){
digitalWrite(redPin, HIGH);
digitalWrite(grnPin,LOW); //turn off green led
digitalWrite(buzPin,HIGH); //play annoying buzzer :)
Serial.print("Help me !!!");
Serial.println();
delay(50);
digitalWrite(redPin, LOW);
delay(50);
}
if((strcmp(code,User) !=0) && (time >= 60000)) {
Serial.print("Im inside");
blink_fast();
}
}
void blinks() {
digitalWrite(redPin, HIGH);
digitalWrite(grnPin,LOW); //turn off green led
//digitalWrite(buzPin,HIGH); //play annoying buzzer :)
Serial.print("Kishan is not in range");
Serial.println();
delay(250);
digitalWrite(redPin, LOW);
delay(250);
}
void blink_fast() {
digitalWrite(buzPin,HIGH); //play annoying buzzer :)
Serial.println("Kishan, You left me behind!!! ");
Serial.print("Time = ");
Serial.println(time);
digitalWrite(grnPin,LOW); //turn off green led
digitalWrite(redPin, HIGH);
delay(50);
digitalWrite(redPin, LOW);
delay(50);
}
this code tries to detect rfid tags and if its the right one then green light turns on if its a wrong tag then red light turn on. it can starts count down in both cases and if the tags are not detected for certain period of time then the red light starts blinking and buzzer makes noise until you place the right tag.
My problem is with this section of the code as below:
if(((time - lastUpdate) >= 50000) && ((time - lastUpdate) <= 60000)) {
Serial.print("checking for kishan");
Serial.println();
blinks();
}
else if ((time - lastUpdate) >= 60000){
digitalWrite(redPin, HIGH);
digitalWrite(grnPin,LOW); //turn off green led
digitalWrite(buzPin,HIGH); //play annoying buzzer :)
Serial.print("Help me !!!");
Serial.println();
delay(50);
digitalWrite(redPin, LOW);
delay(50);
}
if((strcmp(code,User) !=0) && (time >= 60000)) {
Serial.print("Im inside");
blink_fast();
}
}
void blinks() {
digitalWrite(redPin, HIGH);
digitalWrite(grnPin,LOW); //turn off green led
//digitalWrite(buzPin,HIGH); //play annoying buzzer :)
Serial.print("Kishan is not in range");
Serial.println();
delay(250);
digitalWrite(redPin, LOW);
delay(250);
}
void blink_fast() {
digitalWrite(buzPin,HIGH); //play annoying buzzer :)
Serial.println("Kishan, You left me behind!!! ");
Serial.print("Time = ");
Serial.println(time);
digitalWrite(grnPin,LOW); //turn off green led
digitalWrite(redPin, HIGH);
delay(50);
digitalWrite(redPin, LOW);
delay(50);
}
the timer starts as we execute the code and enters this section of the code when no tags are presented to the reader and time reaches 50000 and less then 60000 as below:
if(((time - lastUpdate) >= 50000) && ((time - lastUpdate) <= 60000)) {
Serial.print("checking for kishan");
Serial.println();
blinks();
}
and when the time is over 60000 it should enter the second part of the code as below:
else if ((time - lastUpdate) >= 60000){
digitalWrite(redPin, HIGH);
digitalWrite(grnPin,LOW); //turn off green led
digitalWrite(buzPin,HIGH); //play annoying buzzer :)
Serial.print("Help me !!!");
Serial.println();
delay(50);
digitalWrite(redPin, LOW);
delay(50);
}
but instead it skips and goes to the another section of the code as below when i have given a condition that if the tag is wrong and time is over 60000 then only execute this section, im confused why it enters this section when it should choose the code above when the time goes above 60000:
if((strcmp(code,User) !=0) && (time >= 60000)) {
Serial.print("Im inside");
blink_fast();
}
Hope someone can help me with this issue and point me where im going wrong as i do not have much experience in coding.
Thank you.