I am doing a wirless lock using arduinos and xbee series 2. Basically, i have to enter a passcode on the keypad and it should communicate wirelessly and should give signal so that the door opens. I wrote the code for the first arduino. It's working. Whenever i enter the passcode for the lock ,it gives signal and LED goes ON which means its working. But now it should communicate wirelessly and should tell the other xbee to unlock the door. I wrote the code for receiving arduino. My pascode is 1342D. The receiving arduino is reading character by character and it doesnot read entire passcode (1342D) and doesnot perform the operation.Any help
here is the code
const int ledPin = 10; // pin LED is attached to
char incomingString[4]; // variable to read incoming serial data into
char incomingString[4];``if (incomingString[4] == '1342') {
incoming string[4] doesn't belong to you, and is a single char, so comparing it to four chars doesn't make any sense at all.
incomingString[4] = Serial.read();
I think you're assuming you can read four characters at a time.
You cannot, but there are plenty of examples to help you correct this.
The receiving arduino is reading character by character and it doesnot read entire passcode (1342D) and doesnot perform the operation.
When does it stop reading? Clearly not when the end of packet marker arrives. Perhaps you want to try something like this:
.
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup()
{
Serial.begin(57600);
// Other stuff...
}
void loop()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}