How do I send Serial data from Arduino to Arduino. Using two Xbee's. I would like to use one switch, as High and low, input, to one Arduino. The other Arduino would have resister and Led.
Well, to get started, you can leave out the XBees and just use the serial ports. Connect the Arduino grounds together and connect RX to TX and vice versa. Use digitalread to check the switch on one and send 'H' or 'L' (or whatever else you fancy) out of the serial port depending on the state of the switch.
Have the receiving Arduino read the serial port and when it sees 'H' light the led, else turn it off.
When it's all working, replace the wires connecting the serial ports with XBees. If you're lucky, you won't even need to configure them.
Using the suggestion above, you can write either of the sketches and test them independently using the IDE's serial terminal. What have you tried so far?
Is the way to send tx and rx data, to one (Arduino) to the other (Arduino).
With Button input of High, to Arduino to tx to rx to Arduino to led. And The Other Way Around
Button input of High, to Arduino to tx to rx to Arduino to led.
Send and input data....
int LedPin = 13;
const int buttonPin = 10;
int TxPin = 0;
int RxPin = 1;
int buttonState = 0;
void setup ()
{
Serial.begin(9600);
pinMode (LedPin, OUTPUT);
pinMode (buttonPin, INPUT);
pinMode (TxPin, INPUT);
pinMode (RxPin, OUTPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
Serial.println(TxPin == 1);
}
else {
Serial.println(TxPin == 0);
}
{
while(Serial.available() == 0);
int Rx = Serial.read() - '0';
if (Rx == 1)
{
Serial.println("Led is on");
digitalWrite(LedPin,HIGH);
}
else if (Rx == 0)
{
Serial.println("Led is off");
digitalWrite(LedPin,LOW);
}
else
{
Serial.println("Invalid!");
}
Serial.flush() ;
}
}
You send messages via the serial port. If you have TX of one Arduino connected to RX of the other, and vice versa, with grounds connected, then:
void loop()
{ // Down here where it belongs
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
Serial.print('1');
}
else
{
Serial.print('0');
}
Then
int Rx = Serial.read() - '0';
if (Rx == 1)
{
Serial.println("Led is on");
digitalWrite(LedPin,HIGH);
}
else if (Rx == 0)
{
Serial.println("Led is off");
digitalWrite(LedPin,LOW);
}
else
{
Serial.println("Invalid!");
}
But no Serial.flush(). Throwing away random amounts of unread data, or blocking until one character is sent, depending on the version of the IDE you are using, makes no sense.
I hooked up this program to two Arduino's. It works ok, but is there A faster way to send the serial data.
I have to hold down the switch, to make the led go on. I would like it more like Momentary Push Button switch (High when pushed, Low when off).
int LedPin = 13;
int buttonPin = 10;
int TxPin = 0;
int RxPin = 1;
int buttonState = 0;
void setup ()
{
Serial.begin(9600);
pinMode (LedPin, OUTPUT);
pinMode (buttonPin, INPUT);
pinMode (TxPin, INPUT);
pinMode (RxPin, OUTPUT);
}
void loop()
{ // Down here where it belongs
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
Serial.print('1');
}
else
{
Serial.print('0');
}
int Rx = Serial.read() - '0';
if (Rx == 1)
{
Serial.println("Led is on");
digitalWrite(LedPin,HIGH);
}
else if (Rx == 0)
{
Serial.println("Led is off");
digitalWrite(LedPin,LOW);
}
else
{
Serial.println("Invalid!");
}
}
You need to read up on switch handling. Right now, as long as the switch is pressed, or released, you are hammering the serial port with data. You need to send a value only when the switch transitions from pressed to released and when it transitions from released to pressed. To do this:
int currState;
int prevState = HIGH;
void loop()
{
currState = digitalRead(buttonPin);
if(currState != prevState)
{
// A transition occurred...
if(currState == HIGH)
Serial.print('1');
else
Serial.print('0');
}
prevState = currState;
// Other code
}
int currState;
int prevState = HIGH;
void loop()
{
currState = digitalRead(buttonPin);
if(currState != prevState)
{
// A transition occurred...
if(currState == HIGH)
Serial.print('1');
else
Serial.print('0');
}
prevState = currState;
// Other code
And I did try the ( Serial.begin(115200)
It was just as slow, as the old way. All I am looking to do is send (high or low), over two xbee's.
I would be happy with serial, but line passing on the xbee's is faster, and I don't know why.
I thought it would be easy using tx and rx to send the data faster.
I would like to know where I can go and look to see how. To take the (Tx Code) program and add the (TxRx11 code). Take the (Rx code) and add it to the (TxRx code99).
I am looking to send the serial data with the xbee's using tx and rx.
((RxTx11))
//TxRx11 Code..........
#include <SoftwareSerial.h>
#define Rx 6 // DOUT to pin 6
#define Tx 7 // DIN to pin 7
SoftwareSerial Xbee (Rx, Tx);
void setup() {
Serial.begin(9600); // Set to No line ending;
Xbee.begin(9600); // type a char, then hit enter
delay(500);
}
void loop() {
if(Serial.available()) { // Is serial data available?
char outgoing = Serial.read(); // Read character, send to XBee
Xbee.print(outgoing);
}
if(Xbee.available()) { // Is data available from XBee?
char incoming = Xbee.read(); // Read character,
Serial.println(incoming); // send to Serial Monitor
}
delay(50);
}
((RxTx99))
//TxRx99 Code..........
#include <SoftwareSerial.h>
#define Rx 6 // DOUT to pin 6
#define Tx 7 // DIN to pin 7
SoftwareSerial Xbee (Rx, Tx);
void setup() {
Serial.begin(9600); // Set to No line ending;
Xbee.begin(9600); // type a char, then hit enter
delay(500);
}
void loop() {
if(Serial.available()) { // Is serial data available?
char outgoing = Serial.read(); // Read character, send to XBee
Xbee.print(outgoing);
}
if(Xbee.available()) { // Is data available from XBee?
char incoming = Xbee.read(); // Read character,
Serial.println(incoming); // send to Serial Monitor
}
delay(50);
}
How do I make this program send Serial data, using one xbee's, (tx and rx). To this other program, Led on or off.
There is no reason that code should not work. If it doesn't, you have not connected the XBees to the Arduinos correctly, or you have not configured the XBees correctly. Or, you haven't wired the switch correctly.