Please HELP SHCOOL PROJECT DUE SOON RFID and Parallax Ping

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);

}

}

}

}

I’ve looked at your code. I have a few questions and comments. Let me add that putting your code inside of the code tags (use full editor) helps a lot wrt the code being readable.

This is inside of code tags
  1. Why don’t you flush the serial buffer before reading it ? What happens if the RFID is presented and read before the ping sensor indicates the RFID is in range ? Could you have a end-of-message CR “left over” in this case ? One that causes you to exit the “RFID portion” of the loop ?

  2. You toggle pin 9 high then low when you’ve read in 10 bytes. Why ? What does pin 9 indicate ?

  3. Pin 2 only ever gets set high once looping. What’s pin 2 do ? Just an indication that an RFID message was ever ever ever received ?

  4. If the LED (indicating a good RFID message ?) is on pin 8, I don’t see that your code ever sets it to indicate a good (or any) RFID message was recieved. Was pin 9 (above) supposed to be pin 8 ? Is this the problem ?

Here’s your code, with the indentations restored, in the code tags, so others can peruse it easily.

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);
      }
    }
  }
}