Pages: [1]   Go Down
Author Topic: Problem with two Serial communication  (Read 528 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i have these two sketches in my arduino. one is sketch is for reading serial numbers sent by an rfid reader and the other is turning an LED on by sending a letter in my serial monitor/PC application.
The RFID code came from Seedstudio
Code:
#include <SoftwareSerial.h>

String msg;
char inChar;
int x;
SoftwareSerial Rfid(2, 3);    // RX as pin 2

void setup()
{
 Serial.begin(9600);
 Rfid.begin(9600);
}

void loop()

{
 
while (inChar = Rfid.read() != 2)    // Wait for start byte from Rfid reader when tag is swiped
{
}

msg = "";
for (x = 0; x < 12; x++)
{
    delay(10); 
    inChar = Rfid.read();
    msg += inChar;
}

Serial.print(msg);    // Sends the ID plus the checksum byte (12 bytes)
}
2nd sketch LED on:
Code:
int ledpin = 9;
byte byteread
void setup()
{
 pinMode (9,OUTPUT);
 Serial.begin(9600);

}

void loop() {

  if (Serial.available()) {
    byteread = Serial.read();
   if (byteRead == '1'){
     digitalWrite(9,HIGH);
    delay(3000);
   digitalWrite(9,LOW);} 
  }
}

Problem: when i combine the two codes together, i only get the reading from the rfid reader, and when I send the number 1, it doesnt turn on the led,, Can anyone help me with these serial events?

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's a pretty lousy way to read serial data. You know what the start of the packet marker is. There is an end of packet marker, too, You can't just cross your fingers and hope all the data arrives while you are diddling around.

I can't see your attempt to combine the two sketches. I'd guess that you left something important out.
Logged

Pages: [1]   Go Up
Jump to: