loading

Arduino alarm with LDR and RFID disable feature?

Hello.
I understand that this sounds like an unbelievably simple idea, and so I thought it would be. I have successfully written this first code, that detects an RFID tag and sounds a beep, but I am struggling with getting the second to work. Any ideas?

First Code -- this one WORKS!

Code:
/*------------------------------------------------------------------
This is a sample code for RDM630 RFID reader by Spekel(Spekel.se)
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
http://creativecommons.org/licenses/by-nc-sa/3.0/
-------------------------------------------------------------------*/
#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3
#define SPKR 7
char code[20];
int val = 0;
int bytesread = 0;
//------------------------------------
//create a Serial object RFID
SoftwareSerial RFID= SoftwareSerial(rxPin, txPin);

void setup()
{
  Serial.begin(9600);
  Serial.println("Serial Port Activated");
  RFID.begin(9600);
  Serial.println("RFID Reader Activated");
  Serial.println("Scanning...");
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(SPKR, OUTPUT);
}
void loop()
{
  val = 0;
  bytesread = 0;

  while(bytesread < 12)
  {
    // read 12 digit code
    val = RFID.read();
    if(val == 3)
    { // if header or stop bytes before the 10 digit reading
      break; // stop reading
    }

    if(val != 2)
    {
      code[bytesread] = val; // add the digit
      bytesread++; // ready to read next digit
      code[bytesread] = '\0'; // add the NULL
    }
  }

  if(bytesread >= 12)
  { // if 12 digit read is complete
    Serial.print("RFID Detected - Tag: [");
    for(int i=0; code[i]!='\0' ; i++)
    {
      Serial.print(code[i]);
    }
    Serial.println("]"); //print the whole 13 bytes
    for (int i=0; i<200; i++) {  // generate a 1KHz tone for 1/2 second
  digitalWrite(SPKR, HIGH);
  delayMicroseconds(300);
  digitalWrite(SPKR, LOW);
  delayMicroseconds(300);
  }
    Serial.println("Scanning...");
  }
}



--- You didn't even need that but I assume you understand the setup now ---
The second code is exactly the same, apart from it contains and LDR that decides whether or not the value it receives has changed enough to sound the alarm and does so, allowing the alarm to be deactivated by an RFID.
This does NOT check to see if the RFID tag is the correct one, that is to be written in later. At the moment I get a constant beeping whether or not the RFID tag is used to dissarm, or there is no beep at all. Any ideas??

^^ Please actually read this before answering my Question!!

Code:

#include <SoftwareSerial.h>
#define SPKR 7
#define rxPin 2
#define txPin 3
char code[20];
int val = 0;
int bytesread = 0;
int lightPin = 1;
int variable = 0;
int secondVariable = 0;
int differenceVariable = 0;
int ledPin = 13;
SoftwareSerial RFID= SoftwareSerial(rxPin, txPin);

// --- Initiation Complete ---

void setup() {
  Serial.begin(9600);
  RFID.begin(9600);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(SPKR, OUTPUT);
  variable = analogRead(lightPin);
}

// --- Setup Complete ---

void loop() {
  variable = analogRead(lightPin);
  Serial.println(variable);
  delay(50);
  secondVariable = analogRead(lightPin);
  differenceVariable = variable - secondVariable;
  if ((differenceVariable^2) > 200) {
   
      val = 0;
      bytesread = 0;

      while(bytesread < 12) { // read 12 digit code
          val = RFID.read();
          if(val == 3) { // if header or stop bytes before the 10 digit reading
              break; // stop reading
          }
          if(val != 2) {
              code[bytesread] = val; // add the digit
              bytesread++; // ready to read next digit
              code[bytesread] = '\0'; // add the NULL
          }
          for (int i=0; i<200; i++) {  // generate a 1KHz tone for 1/2 second
              digitalWrite(SPKR, HIGH);
              delayMicroseconds(300);
              digitalWrite(SPKR, LOW);
              delayMicroseconds(300);
          }
 
      }

      if(bytesread >= 12) { // if 12 digit read is complete
          Serial.print("RFID Detected - Tag: [");
          for(int i=0; code[i]!='\0' ; i++) {
              Serial.print(code[i]);
          }
          Serial.println("]"); //print the whole 13 bytes
          for (int i=0; i<200; i++) {  // generate a 1KHz tone for 1/2 second
              digitalWrite(SPKR, HIGH);
              delayMicroseconds(300);
              digitalWrite(SPKR, LOW);
              delayMicroseconds(300);
          }
          Serial.println("Scanning...");
      }
   }
}


Thank you very much for any help that you can give,
AMouse197

(I accidentally posted this in the wrong forum so have moved it)

What does your serial output say is happening ? Add some more println commands to monitor the thing.
anonymouse197 (author)  steveastrouk5 years ago
I get the serial out from the LDR, so I will try stopping writing this to serial and give it another try, and copy the results out here.