I am trying to write a code for a door lock that will only open the door lock when a signal from both a RFID and ping sensor are detected. I am currently simulating the door lock with an LED light. I can get the light to turn on when a signal from the PING sensor is detected at less then one foot or when the RFID reads a valid tag. I would like to only get the light to turn on when signals from both the RFID and PING sensor are detected.
I have tried to write an if statement that tells the LED to go high when both conditions are met. I also tried write a function for the RFID that is only called when the PING sensor detects a signal. My last approach was to write the entire code for the RFID in a if loop that is only called when the PING sensor detects a signal.
Each of these attempts has turned the LED on when RFID goes high and the PING sensor is still low. The code below is what I’m working with now. I know the code for making the LED high works independently for both the LED and RFID so I’m guessing something is wrong with my logic.
int val = 0;
char code[10];
int bytesread = 0;
const int pingPin = 7;
const int ledPin = 8;
long duration;
void setup()
{
Serial.begin(2400);
pinMode(2,OUTPUT);
digitalWrite(2, LOW);
}
void loop()
{
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
if(duration < 1770)
{
if(Serial.available() > 0)
{
if((val = Serial.read()) == 10)
{
bytesread = 0;
while(bytesread<10)
{
if( Serial.available() > 0)
{
val = Serial.read();
if((val == 10)||(val == 13))
{
break;
}
code[bytesread] = val;
bytesread++;
}
}
if(bytesread == 10)
{
digitalWrite(9, HIGH);
delay(2500);
digitalWrite(9, LOW);
Serial.print(code);
delay(2500);
}
bytesread = 0;
digitalWrite(2,HIGH);
}
}
}
}