Arduino to Arduino Serial

Hi

I am connecting two Arduino Uno boards together to send data from one to the other, The aim is to eventually have a wireless link between the two and to have a remote handset with four switches with the high or low being sent to another Arduino board and operating LEDs as an indication.

As i am new to programming i am struggling to get the two boards to send data from one to the other. I have connected the ground to both boards so that they are sharing a common ground, the TX pin is connected to RX on the other and vise versa.

Has anybody got some sample code that they can post to help me send the switch data from one board to the other.

many thanks

Tris

Post the code you have tried so far (use the #icon)

It is a simple matter of setting the two to the same speed and doing a serial write at on end an a serial available at the other followed by a read.
How are you checking if it is working, you can’t use the serial monitor while this is going on?

I have tried using the EasyTransfer library that i found from here - http://www.billporter.info/easytransfer-arduino-library/ but it is coming up with errors after importing the library, The errors are saying ‘no matching function for call to…’ amongst other issues

#include <SoftEasyTransfer.h>

/*   For Arduino 1.0 and newer, do this:   */
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);

/*   For Arduino 22 and older, do this:   */
//#include <NewSoftSerial.h>
//NewSoftSerial mySerial(2, 3);


//create object
SoftEasyTransfer ET; 

struct RECEIVE_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to receive
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  int blinks;
  int pause;
};

//give a name to the group of data
RECEIVE_DATA_STRUCTURE mydata;

void setup(){
  mySerial.begin(9600);
  //start the library, pass in the data details and the name of the serial port.
  ET.begin(details(mydata), &mySerial);
  
  pinMode(13, OUTPUT);
  
}

void loop(){
  //check and see if a data packet has come in. 
  if(ET.receiveData()){
    //this is how you access the variables. [name of the group].[variable name]
    //since we have data, we will blink it out. 
    for(int i = mydata.blinks; i>0; i--){
      digitalWrite(13, HIGH);
      delay(mydata.pause * 100);
      digitalWrite(13, LOW);
      delay(mydata.pause * 100);
    }
  }
  //you should make this delay shorter then your transmit delay or else messages could be lost
  delay(250);
}

This is the first project that i have undertaken with programming and i am able to adapt existing code and rehash them for my needs, but when it comes to making new codes i dont have the understanding that i really should have.

The problem is that you are trying to run before you can walk. Using an obscure library <SoftEasyTransfer.h> is likely to confuse matters even further because unless people know this library they will not know where you are going wrong with it.

You need to learn to read and understand error messages:-

The errors are saying 'no matching function for call to

Means you are trying to call a function that does not exist so you have it wrong.

I would advise you to forget this library for the moment and just look at transferring the data.
This link:-

Shows you how to receive serial data and do things with it.

Tris_C: Hi

I am connecting two Arduino Uno boards together to send data from one to the other, The aim is to eventually have a wireless link between the two and to have a remote handset with four switches with the high or low being sent to another Arduino board and operating LEDs as an indication.

No reason why you shouldn't be able to get that working, but I suggest that if you splash on a couple of bucks on a pair of nRF24L01+ radio transceivers you will find that is no harder to get working, and probably easier. For the radio, all you need is to assign an address to each unit, write a message on one, read it on the other, it requires minimal code.

Grumpy_Mike: I would advise you to forget this library for the moment and just look at transferring the data. This link:- http://arduino.cc/en/Tutorial/SwitchCase2 Shows you how to receive serial data and do things with it.

If i adapted the programme in this library to send data from one board to the other, would it be a case of using Serial.print() on the TX board and then Serial.read() to RX the data on the other board?

If i have got two switches, and switch 1 is High which switch 2 is Low, would i Serial.print("Switch1 = " ), Serial.print(switch1) or am i barking up the wrong tree?

Thanks for the help so far!

I sound like an interesting project. ( I will try it ). Can you test one system and the next one using the serial monitor of the IDE ? I got the feeling that you need to re-program both Arduino. Do a Switches / data transmitted Arduino , check the outgoing data with serial monitor to see what is going on, And than, do the other Ardiuno Leds / receiveing , check the incomming data using the serial monitor. If both programs work just fine, then you can connect the Arduino's serial in/out to check every things works fine.

My 2 cent idea.

i Serial.print("Switch1 = " ), Serial.print(switch1) or am i barking up the wrong tree?

It's the right idea but it's too complex. What is all that text for? Why not send consecutive characters for each button. So for example for one switch send A if it is high and B if it is low, for the next switch use C and D and so on. In that way you can easily decode what is happening at the other end any you only have to send one byte so ther is no chance of the messages being miss aligned.

So if I were to use something along the lines of - if switch1 = high then serial.print(a), and would i be right on saying on the other board if serial.read(a) then switch led 1?

I was using the text to identify each switch, but if I cannot the high or low output letters then there is no need for this.

Thanks again,

Tris

but if I cannot the high or low output letters then there is no need for this.

Not sure what this means.

if switch1 = high then serial.print(a)

yes

if serial.read(a) then switch led 1?

yes but put the result of the read into a variable so you can compare it with several other things.

If this is too much please ignore it but what is actually happening is this:- In fact the high and low bit, is simply the least significant bit of what you are sending, the other bits are used in identifying the switch you are sending. So you are sending the switch state and it's identifier in just one single byte.

but if I cannot the high or low output letters then there is no need for this.

Not sure what this means.

I think what i meant was - 'If i can use the high or low output letters then there is no need for this' I was replying on my phone and with predictive text :roll_eyes:

if serial.read(a) then switch led 1?

yes but put the result of the read into a variable so you can compare it with several other things.

How would i put the results as a variable?

The explaination about the least significant bit and identifying the switch makes perfect sense - I think its all coming together and starting to learn somthing :)

How would i put the results as a variable?

 char variable; // define this somewhere outside the loop()
  // then in the code
  if( Serial.available() >0 ) variable = Serial.read();
  if(variable =='a') digitalWrite(13,HIGH);
  if(variable =='b') digitalWrite(13,LOW);
  if(variable =='c') digitalWrite(12,HIGH);
  if(variable =='d') digitalWrite(12,LOW);
// or what ever you want with the same variable

Ive been trying to set up a programme to send the data from two switches, with the highs and lowa coded A, B, C and D. However i seem to have an issue with some results that im not expecting... If i Serial.Print from the input switch it looks like i have a strange voltage cropping up somewhere.

With the switch pressed (high) i am getting the output i expect which is 111111111 from the serial monitor, however with the switch released - should be low the serial monitor is displaying 111111000000111111000000 .....

Do i have to force the pins that the switch is connected to down to ground? if so how would i do this?

Yes you have the pin floating. It needs to be pulled in the opsite direction to the switch. I favour putting the switch between input and ground and enabling the internal pull up resistors by a write high to the input pin in the setup.

How would i go about pulling it to the opposite direction to the switch? currently ive got the 5v supplyfrom the Arduino going to one side of the switch and the other side of the switch going to pin 2.

My code is ;

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
  pinMode(Switch2, INPUT); // Setting Switch 2 as an input
}

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

To switch from input to ground would i need to connect the ground to the side of the switch where the +5v was connected and then set the "int SW1 = 1; to set the pin high?

To switch from input to ground would i need to connect the ground to the side of the switch where the +5v was connected and then set the "int SW1 = 1; to set the pin high?

Yes.

To pull in the opposite direction you use a resistor from 1 to 10 K.

I think i have just worked it out using digitalWrite;

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
}

Hopefully it works

Edit to say - It works as i was expecting to using the internal Resistors

Im trying to programme the board that will recieve the data; Ive got two LEDs which are connected to Pin 2 and 3 and set as outputs, the code that ive got is as follows;

const int Switch1Out = 2;
const int Switch2Out = 3;
int variable = 0;

void setup(){
  Serial.begin(9600);
  pinMode(Switch1Out, OUTPUT);
  pinMode(Switch2Out, OUTPUT);
  char variable;
}

void loop(){
  if( Serial.available() >0 ){
    variable = Serial.read();
   
  if(variable ='A'){
    digitalWrite(2,HIGH);
  }
  if(variable ='B'){
    digitalWrite(2,LOW);
  }
  if(variable ='C'){ 
    digitalWrite(3,HIGH);
  }
  if(variable ='D'){ 
    digitalWrite(3,LOW);
  }
}
}

When i upload the programme both LEDs come on, Should i be able to open the Serial Monitor and simulate the A, B, C and D values? I have tried typing in the characters but i can seem to change the states of the LEDs, or is this due to my code being wrong?

An if statement needs two equals signs, == not =

Grumpy_Mike: An if statement needs two equals signs, == not =

Mike, you may be Grumpy... But your a star!