#include <Servo.h>
uint8_t buffer[14];
uint8_t* buffer_at;
uint8_t* buffer_end = buffer + sizeof(buffer);
// digital pin 2 has a pushbutton attached to it. Give it a name:
int IRreciever = 22;
int IRtransmitter = 52;
int A;
String checksum;
boolean tagfound = false;
Servo myservo; // create servo object to control a servo
void setup()
{
Serial.begin(9600);
Serial.println("Serial Ready");
Serial1.begin(9600);
Serial.println("RFID Ready");
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(IRreciever, INPUT);
pinMode(IRtransmitter, OUTPUT);
A = digitalRead (IRreciever);
}
void loop()
{
if (Serial1.available())
{
delay(20);
buffer_at = buffer;
while ( buffer_at < buffer_end )
{
*buffer_at++ = Serial1.read();
}
tagfound = true;
Serial1.end();
Serial1.begin(9600);
}
if (tagfound)
{
buffer_at = buffer;
uint32_t result = 0;
// Skip the preamble
++buffer_at;
// Accumulate the checksum, starting with the first value
uint8_t checksum = rfid_get_next();
// We are looking for 4 more values
int i = 4;
while(i--)
{
// Grab the next value
uint8_t value = rfid_get_next();
// Add it into the result
result <<= 8;
result |= value;
// Xor it into the checksum
checksum ^= value;
}
// Pull out the checksum from the data
uint8_t data_checksum = rfid_get_next();
//
// Print the result
Serial.print("Tag: ");
Serial.print(result);
if ( checksum == data_checksum )
{
Serial.println(" OK");
close();
delay (10000);
}
else
{
Serial.println(" ERROR");
// We're done processing, so there is no current value
tagfound = false;
}
while ( digitalRead (IRreciever) == HIGH)
{
Serial.println ("welcome");
Serial.println (digitalRead (IRreciever));
}
while (digitalRead (IRreciever) == LOW)
{
Serial.println ("welcome");
Serial.println (digitalRead (IRreciever));
}
delay (1000);
open ();
delay (1000);
}
}
uint8_t rfid_get_next(void)
{
// sscanf needs a 2-byte space to put the result but we
// only need one byte.
uint16_t hexresult;
// Working space to assemble each byte
static char byte_chars[3];
// Pull out one byte from this position in the stream
snprintf(byte_chars,3,"%c%c",buffer_at[0],buffer_at[1]);
sscanf(byte_chars,"%x",&hexresult);
buffer_at += 2;
return static_cast<uint8_t>(hexresult);
}
void open()
{
myservo.write(90); // sets the servo position according to the scaled value
delay(1000); // waits for the servo to get there
}
void close()
{
myservo.write(0); // sets the servo position according to the scaled value
delay(1000); // waits for the servo to get there
}
Guys, the code above is the full code of the programming. My problem is that the RFID tag that I tap the first time is still being printed in the serial com, and that eventhough the full program is already executed, the program still executing the close command. Please help me to sort this out