Switch Input to Arduino Serial to Arduino Serial Output Led High or Low.

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.

So how do I go about making two programs?

Switch (High or Low),Arduino, and xbee.

Xbee, Arduino, 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.

I do know how to configure the xbee's, just need some tips one getting the program going.....

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?

Here's what I have =

const int buttonPin = 5;    
const int TxPin = 0;
const int RxPin = 1;

// variables will change:
int buttonState = 0;         

void setup() {
 
      
 
  pinMode(buttonPin, INPUT);    
}

void loop(){
  
  buttonState = digitalRead(buttonPin);

  
  if (buttonState == HIGH) {    
     
     Serial.write(TxPin,45;  
  }
  else {
    
    digitalWrite(TxPin, LOW);
  }
}
const int ledPin =  7;      
const int TxPin = 0;
const int RxPin = 1;

void setup() {
 
  pinMode(ledPin, OUTPUT);      
 
 
}

void loop(){
  

  
  if (Serial.println("45")LedPin == HIGH) {    
    
   
  }
  else {
    
   Serial.pintln("45")(ledPin, LOW);
  }
}

What does the compiler make of that? It's generally expected that you will have tried that before posting.

I don't know how to send data tx or rx, I have looked for Tutorials on line.

I don't know how to send data tx or rx, I have looked for Tutorials on line.

Which has nothing to do with posting code that won't even compile.

Pay attention, boy. I say, pay attention!

  • Foghorn Leghorn

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!");
 }
}

is there A faster way to send the serial data

Sure. Get out of the stone age.

  Serial.begin(9600);

should be

  Serial.begin(115200);

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
}

I did try your new way.

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):wink:
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.

So, how do I hook it up to the xbee's. Tx to rx and Rx to tx, on the xbee's and then where does the ground's go. (Arduino grounds together).

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);
}

((Tx))

//Tx Code..........

int buttonPin = 10; 

void setup ()
{
  Serial.begin(9600);
  
  pinMode (buttonPin, INPUT);

}

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;
}

((Rx))

//Rx Code........


int LedPin = 13;
 
void setup ()
{
  Serial.begin(9600);
  pinMode (LedPin, OUTPUT);

}
void loop()
{
  
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!");
 }
}

How do I make this program send Serial data, using one xbee's, (tx and rx). To this other program, Led on or off.

//Send this program to one xbee's (tx and rx)

int buttonPin = 10; 

void setup ()
{
  Serial.begin(9600);
  
  pinMode (buttonPin, INPUT);

}

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;
}
//Led program



int LedPin = 13;


void setup()
{

Serial.begin(9600);

pinMode(LedPin, OUTPUT);

}

void loop ()

{

while (Serial.available()  == 0);

int val = Serial.read() - '0';

if (val == 1)

{
Serial.println("Led is On");
digitalWrite(LedPin, HIGH);
}

else if (val == 0)
{
Serial.println("Led is off");
digitalWrite(LedPin, LOW);
}

else 
{
  Serial.println ("Invalid!");
}

}

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.