Hello,,
i'm trying to send characters from one lilypad main board to the other,,,
(in the actual project, it's to control certain components connected to the sub board)...
the following codes are just to test the serial connection...((i'm getting a constant output of 192 from the sub board no matter what i send from the main board))..
Main board:
#include <SoftwareSerial.h>
int ledPin = 13; // LED connected to digital pin 13, only used to indicate that board is alive
char x;
//pin 5 rx, pin 6 tx
SoftwareSerial mainBserial = SoftwareSerial(5,6);
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(5, INPUT);
pinMode(6, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
mainBserial.begin(9600);
}
void loop()
{
x='1';
mainBserial.print(x);
Serial.println(x);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
}
SubBoard:
#include <SoftwareSerial.h>
int ledPin = 13; // LED connected to digital pin 13, only used to indicate that board is alive
char i;
//pin 2 rx, pin 3 tx;
SoftwareSerial subBserial = SoftwareSerial(2,3);
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
subBserial.begin(9600);
}
void loop()
{
digitalWrite(ledPin, LOW);
i=subBserial.read();
Serial.println(i);
delay(500);
digitalWrite(ledPin, HIGH);
}
it may be trivial,,,but it doesn't seem like the serial connection is working..
You don't mention anything about the wiring between the two boards. Do you have pin 5 on the main board connected to pin 3 on the sub board? Do you have pin 6 on the main board connected to pin 2 on the sub board?
yes,,they are connected that way.. (tx(3)<-->rx(5)) and (rx(2)<--->tx(6))...
oh ya,,,and it's not included in the posted code...
but pin 2 and 3 are configured as input, output...
ah,,,and i removed the delay,,,but still getting garbage(when using AFserial or softwareserial) and sometimes no garbage when using software serial.....
Don't forget that with SoftSerial, if you're not executing "read", you won't see anything, so a delay in "loop" on the receiver board is a no-no.
You didn't say if you changed your code or not.. what he's saying is that you send something... but while one is sending, the receiver may be on it's 500ms delay, so when it comes back it'll just read... nothing? (I don't think SoftwareSerial uses a buffer, I could be wrong)
Try using NewSoftSerial (google it), it's just an upgraded version of SoftwareSerial that offers faster speeds and I think buffering. (I'm too lazy to look for it)
Maybe you should try getting the Hardware serial working between the two before you try software versions?
This won't work with SoftwareSerial but it will work with the regular serial, and possibly NewSoftSerial:
if(Serial.available() > 0)
{
myValue = Serial.read();
}
Bascially, it won't read the Serial port unless there is data to be read. (So you don't just read the same thing over and over, or trying to read while nothing is happening, resulting in garbage)
if(Serial.available() > 0)
{
myValue = Serial.read();
}
Bascially, it won't read the Serial port unless there is data to be read. (So you don't just read the same thing over and over, or trying to read while nothing is happening, resulting in garbage)
According to the Serial.read documentation:
Returns
the first byte of incoming serial data available (or -1 if no data is available) int
So, it won't read the same thing over and over, nor will it return garbage.