Hello everyone, I am currently doing a project using arduino mega 2560. I am interfacing my arduino with rfid reader rdm 6300, servo motor Tower Pro MG995, and Infra red sensor DT-I/O transmitter and receiver.
while (IRreciever, HIGH)
{
Serial.println ("welcome");
Serial.println (IRstate);
while (IRreciever, LOW)
{
Serial.println ("welcome");
Serial.println (IRstate);
}
delay (10000);
open ();
loop();
}
I would like to ask for your help, because eventhough the status of the IRreceiver is 0 (LOW), the program is still calling the open() function. Is there anything wrong with this part of the programming? If not can anyone please tell me how to fix the program?
Even though its valid, I do not think its what you are intending, the loop always runs ( and is infinite ). And your nested loop will never run, no matter what the IR thingo returns.
{
Serial.println ("welcome");
Serial.println (IRstate);
while (IRreciever, LOW)
{
Serial.println ("welcome");
Serial.println (IRstate);
}
delay (10000);
open ();
loop();
}
What pYro said and why are you calling loop() from, presumably, inside loop()? Recursion isn't a good idea, as you'll soon overflow the stack and crash the program. But your code makes no sense because it prints exactly the same whether IRreceiver is HIGH or LOW.
EDIT Your second while() will never run as IRreceiver cannot be both HIGH and LOW at the same time.
#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
make the assumption that the data arrives via Serial1.read() at a rate that is slower than a byte can be incremented? It would seem to me that, at 9600 baud, this loop is blasted through pretty quickly, or am I missing something?