TX/RX problems between Arduinos

Hi

I have got two Arduino Uno boards connected together via the TX and RX pins (TX -> RX on one boards and RX->TX on the other) with the aim of trying to operate a switch on one board which will operate a switch on the other board, at the moment the boards are being hard wired together as my RF link to replace the wires hasnt turned up yet and i am testing the programme work and fuctions how it should.

I am powering the Arduino board from a single USB cable, with the boards sharing a common ground and the +5v out from one board connected to the Vin of the other,

The programme ive got is as follows;

Arduino 1;

const int Switch1 = 2; // Number of Pin for Switch 1
const int Switch2 = 3; // Number of pin for Switch 2
int SW1 = 0; //Variable input from Switch 1
int SW2 = 0; //Variable input from Switch 2

void setup() {
  Serial.begin(9600);
  pinMode(Switch1, INPUT); // Setting Switch 1 as an input
  digitalWrite(Switch1, HIGH); //Sets Switch 1 as High
  pinMode(Switch2, INPUT); // Setting Switch 2 as an input
  digitalWrite(Switch2, HIGH); // Sets Switch 2 as High
}

void loop(){
  SW1 = digitalRead(Switch1); // Reads whether switch 1 is high or low 
  SW2 = digitalRead(Switch2); // Reads whether switch 1 is high or low
    
  if (SW1 == HIGH) {
    Serial.print('A'); // if Switch 1 is pressed outputs A
  }
  else {
    Serial.print('B'); // if Switch 1 is not pressed outputs B
  }
  if (SW2 == HIGH) {
    Serial.print('C'); // if Switch 2 is pressed outputs C
  }
  else{
    Serial.print('D'); // if Switch 2 is not pressed 0utputs D
  }
}

Arduino 2;

const int Switch1Out = 2; //Number of pin for Switch1Out
const int Switch2Out = 3; //Number of Pin for Switch2Out
int variable = 0; //Variable is 0 unless data recieved

void setup(){
  Serial.begin(9600);
  pinMode(Switch1Out, OUTPUT); //Sets Switch1Out as an output
  pinMode(Switch2Out, OUTPUT); //Sets Switch2Out as an Output
  char variable; //Variable is a character
}

void loop(){
  if( Serial.available() >0 ){ 
    variable = Serial.read(); //Read available Serial
   
  if(variable =='A'){
    digitalWrite(2,HIGH); //If A Recieved Output2 High
  }
  if(variable =='B'){
    digitalWrite(2,LOW); //If B Recieved Output2 Low
  }
  if(variable =='C'){ 
    digitalWrite(3,HIGH); //If C Recieved Output3 High
  }
  if(variable =='D'){ 
    digitalWrite(3,LOW); //If D Recieved Output2 Low
  }
}
}

The issue that i have got is that no data seems to be being sent across from one board to the other, Is this because i have the board connected to my Laptop to supply power, or is there an issue with my code?

Many thanks

Tris

How do you know that no data is being sent? It could be that the wrong data is being sent. Add two LEDs with resistors to the sender. Turn them on or off to match the switches. When the LEDs go on and off right, then, if there is still a problem, we can work on why.

Tris_C:
I am powering the Arduino board from a single USB cable, with the boards sharing a common ground and the +5v out from one board connected to the Vin of the other,

The +5V of one should be connected to the +5V of the other. The Vin pin is for 7-12V because it goes to the input of the 5V regulator.

    Serial.print('A')

This may be the problem. The constant 'A' is an integer constant. I think Serial.print() is going to send it as a decimal number. You probably want Serial.print('A', BYTE); or Serial.print('A', CHAR); to specify you want a single character sent.

PaulS:
How do you know that no data is being sent? It could be that the wrong data is being sent. Add two LEDs with resistors to the sender. Turn them on or off to match the switches. When the LEDs go on and off right, then, if there is still a problem, we can work on why.

I assume that data that no data is being recieved, if i monitor the serial data from the TX board it is printing - A B C or D depending on which switch is high or low.

I have tried chaning the Serial.print ('A') to digitalWrite(4, HIGH) with an LED and resistor attached to pin 4, the LED comes on when the switch is High and the same works for the other switch.

johnwasser:
This may be the problem. The constant 'A' is an integer constant. I think Serial.print() is going to send it as a decimal number. You probably want Serial.print('A', BYTE); or Serial.print('A', CHAR); to specify you want a single character sent.

I seem to be having problems changing the Serial.print('A'); to Serial.print('A', char);

my code is ;

void loop(){
  SW1 = digitalRead(Switch1); // Reads whether switch 1 is high or low 
  SW2 = digitalRead(Switch2); // Reads whether switch 1 is high or low
    
  if (SW1 == HIGH) {
    Serial.print('A', char); // if Switch 1 is pressed outputs A
  }
  else {
    Serial.print('B', char); // if Switch 1 is not pressed outputs B
  }
  if (SW2 == HIGH) {
    Serial.print('C', char); // if Switch 2 is pressed outputs C
  }
  else{
    Serial.print('D', char); // if Switch 2 is not pressed 0utputs D
  }
}

The error code i keep on getting is as follows;

Switching_High_Low_TX.cpp: In function 'void loop()':
Switching_High_Low_TX:18: error: expected primary-expression before 'char'
Switching_High_Low_TX:21: error: expected primary-expression before 'char'
Switching_High_Low_TX:24: error: expected primary-expression before 'char'
Switching_High_Low_TX:27: error: expected primary-expression before 'char'

What am i doing wrong? :frowning:

Simpler might be to send it as a string:

Serial.print("A"); //note double quotes

Let me in this discusion. Can you try this code...

And please, try one Arduino at a time to test the progam ( on one of the Arduino) using serial monitor, if everything work fine, test with both Arduino's.

---> Me thinking... That sound like a nice project, using 2 Arduino, stand alone, using a RS-232 line to communicate... Interesting

// in your variable declarations...
char id1='A';
char id2='B';
char id3='C';
char id4='D';


void loop()
{

// your program here
if (SW1==HIGH)
{
   Serial.print(id1);  // Output of the serial monitor : A
}

}

wildbill:
Simpler might be to send it as a string:

Serial.print("A"); //note double quotes

Thats done the job, Thank you! :slight_smile:

Techone - for reference i will try your code tomorrow and see if it works.

Cheers

Tris_C:
I seem to be having problems changing the Serial.print('A'); to Serial.print('A', char);

That's because you changed 'CHAR' to 'char'.