Communicating Arduinos

Hello,

Im working on a project whereas an ultrasonic sensor will detect someone walking into the bathroom, send the distance variable from the detecting Arduino to another Arduino, which will display “TrIG” on a 4 7 segment, set a timer, and when the timer runs out (aprox. 10 mins.), the displaying Arduino will send back a message telling the detecting Arduino to turn a servo. I’ve connected the 4 7 segment successfully, attached the detecting Arduino (of whom will be nicknamed “Bob” for now) to the displaying Arduino (of whom will be nicknamed “Joe” for now), connected servo and ultrasonic sensor to Bob, but I don’t know how to get them to communicate to one another.

Bob’s Code:

/*
Welp this is Part 2... Part 1 is here:
https://create.arduino.cc/editor/itsfoxy87/6366fdf0-89c5-4fb3-8dfe-0def763fa54c
I used two Arduino UNOs: One for Part 1 and another for Part 2 (so voltage is equal for 
both parts AND so i can use both at the same time)

    --------
    | Servo|_______Red wire to 5v on Arduino
   ----⨷  |_______Yellow or Brown wire (or whatever colour wire that is in the middle) to Pin 10 on Arduino
    |(kind |_______Black Wire to GND on Arduino
    | of)  |
    --------
*/
int pos = 0;    // variable to store the servo position
#include <Servo.h>
Servo myservo;  //Tells Arduino that a servo exists

void setup() {
 myservo.attach(11); //Tells Arduino where the servo is so it can be controlled 
 int pos = 0;
}

void loop() {
  for (pos = 90; pos <= 0; pos += 1) { // goes from Spray to Idle Mode
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to set position above
    delay(3600000);                       // waits 1 hour until servo moves to dispense liquid
  }
  for (pos = 0; pos >= 90; pos -= 1) { // goes from Idle to Spray Mode
    myservo.write(pos);              // tell servo to go to set position above
    delay(1000);                       //Keeps Spray Mode on (pushes the trigger) for 1 second before returning to Idle Mode
  }
}

Joe’s code will be found in the link at the top of the code.

Don't force us to cut and paste a link from code. Please just append the second sketch.

itsfoxy87:
but I don't know how to get them to communicate to one another.

You have not told us how you want to communicate (wires, or wireless) or over what distance the communication needs to happen.

And I think the description would be easier to follow if you call them sensorArduino and displayArduino rather than Bob and Joe.

...R

aarg:
Don't force us to cut and paste a link from code. Please just append the second sketch.

Aight, here ya go:

/*
   A
   ---
F |   | B
  | G |
   ---
E |   | C
  |   |
   ---
    D

 Part 1 of Air Freshener Dispenser code. Part 2 is here:
 https://create.arduino.cc/editor/itsfoxy87/3b9c30fd-a8b6-4ae8-918e-67fbcd937c2b
 Made by ItsFoxy87 (code base by Anything Arduino, I just re-edited the code ¯\__(ツ)__/¯ )
*/
int pinA = 2;
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;
int D1 = 9;
int D2 = 10;
int D3 = 11;
int D4 = 12;
/*Pin layout: 
☻Pin 1 on LED to Pin 6 on Arduino (i used UNO R3)
☻Pin 2 on LED to Pin 5 on Arduino
☻Pin 3 on LED is unconnected
☻Pin 4 on LED to Pin 4 on Arduino (how convenient!)
☻Pin 5 on LED to Pin 8 on Arduino
☻Pin 6 on LED to Pin 10 on Arduino
☻Pin 7 on LED to Pin 3 on Arduino
☻Pin 8 on LED to Pin 12 on Arduino (as high as it goes)
☻Pin 9 on LED to Pin 11 on Arduino
☻Pin 10 on LED to Pin 7 on Arduino
☻Pin 11 on LED to Pin 2 on Arduino
☻Pin 12 on LED to Pin 9 on Arduino

Aight we're done finally.
*/

int pos = 0;    // variable to store the servo position

void setup() {
  
  pinMode(pinA, OUTPUT);     
  pinMode(pinB, OUTPUT);     
  pinMode(pinC, OUTPUT);     
  pinMode(pinD, OUTPUT);     
  pinMode(pinE, OUTPUT);     
  pinMode(pinF, OUTPUT);     
  pinMode(pinG, OUTPUT);   
  pinMode(D1, OUTPUT);  
  pinMode(D2, OUTPUT);  
  pinMode(D3, OUTPUT);  
  pinMode(D4, OUTPUT); 
}

void loop() {
  digitalWrite(D1, LOW);
  digitalWrite(D2, LOW);
  digitalWrite(D3, LOW);
  digitalWrite(D4, HIGH); 
  
  digitalWrite(pinA, LOW);   
  digitalWrite(pinB, LOW);   
  digitalWrite(pinC, LOW);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, LOW);   
  digitalWrite(pinG, HIGH);
  delay(1);
  
  digitalWrite(D1, LOW);
  digitalWrite(D2, HIGH);
  digitalWrite(D3, LOW);
  digitalWrite(D4, LOW); 
  
  digitalWrite(pinA, HIGH);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, LOW);   
  digitalWrite(pinD, HIGH);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, LOW);
  delay(1);
  
  
  
  }


[code/]

What about the first question in reply #2? That's rather important.

Also,

Robin2:
You have not told us how you want to communicate (wires, or wireless) or over what distance the communication needs to happen.

And I think the description would be easier to follow if you call them sensorArduino and displayArduino rather than Bob and Joe.

…R

I want a RX-TX connection in between my UNO’s. Sorry im a total noob at this ¯_(ツ)_/¯

itsfoxy87:
Also,I want a RX-TX connection in between my UNO's. Sorry im a total noob at this ¯_(ツ)_/¯

Being a noob doesn't matter - what does matter is how your system is designed. Saying you want an RX-TX connection without any hint as to what hardware you're using is like me asking an architect to "build an office building". Is it supposed to be a skyscraper? Is it supposed to accommodate 100 people? Is it supposed to be built in Italy? Nobody knows if you don't tell us.

In short: What hardware will be used to connect your Arduinos? We can't answer your question accurately until you tell us this!

IM SORRY!!!!

Anyway, Ill use Jumper Cables to connect RX on one Arduino to TX on the other, and vice versa. This will help me help them "communicate".

There's no way I can think of to make that any more specific at all.

Sorry I guess I came off a little strong. Just wanted to make sure you help us help you since you've already been asked once before by Robin2.

Jumper cables will work just fine up to a certain distance. But then again, are you using the cable to connect them via Serial, SPI, I2C, etc? Are you planning on connecting grounds? How long is the cable?

itsfoxy87:
IM SORRY!!!!

Anyway, Ill use Jumper Cables to connect RX on one Arduino to TX on the other, and vice versa. This will help me help them "communicate".

There's no way I can think of to make that any more specific at all.

"serial via UART" serial input basics

itsfoxy87:
… but I don’t know how to get them to communicate to one another.

“Communicate” is a remarkably broad term. This might explain why you get the responses you do because off the top of my head I can think of half-dozen different topologies. You need to define the concept of “communicate” a bit more precisely.

Start with wired or wireless.

Without context (other than one of those sketches mentioning air freshener) it seems a bit of a weird thing for sensorArduino to do nothing other than send a trigger to displayArduino, which then does the timing and sends a signal back to sensorArduino to run the servo to squirt the airfreshener.

Why not just do the timing on sensorArduino?

Why have displayArduino at all?- or is it important for someone some (unspecified) distance away to know there's someone in the loo? If so, it would surely be worthwhile investigating how far away the 7-seg display can be from the (now single) Arduino?

(Best air fresheners I've seen in loos are the one's where there's a cam mechanism on the door that presses the aerosol button every time the door opens.)

"I want a RX-TX connection in between my UNO's."

What you can do for testing is get a cheap USB hub to plug into your computer. Plug your two UNOs into this hub. In Windows each UNO will be assigned its own com port. Then you can separately open two instances of the arduino IDE. Appropriately wire the tx/rx/gnd wires between the two UNOs and load your test code on the UNOs. Then you can open a serial monitor for each UNO and send test commands between them and see the results in the serial monitors of the IDEs.

(Grrrr just hit Post and my lovingly typed post disappeared and I went to New Topic.)

itsfoxy87:
connect RX on one Arduino to TX on the other, and vice versa.

If all you need is for sensorArduino to tell displayArduino there's been a trigger, and for displayArduino to tell sensorArduino the time's up, I doubt you need the (relative) complexity of serial comms. Why not just have each side make a pin high to signify trigger and time up respectively, wire those two pins on the other side, and poll them?

(Assuming it needs two Arduino in the first place. Perhaps OP could describe why he or she wants to or needs to use two?)

(Remembering to ctrl-A ctrl-C this time.)

:Tutorial for OP:
Possible hardware setup (Fig-1) for the project of OP.
4digit.png
Figure-1:

1. Testing the functionality of Ultrasonic Senor
(1) Connect only the ultrasonic sensor (HC-SR04) with UNOB as per Fig-1.
(2) Upload the following sketch (taken from web and untested) in UNOB.

int triggerPin = A0; //triggering on pin A0
int echoPin = A1;    //echo on pin A1

void setup() 
{
  Serial.begin(9600);  //we'll start serial comunication
  pinMode(triggerPin, OUTPUT); //defining pins
  pinMode(echoPin, INPUT);
}

void loop() 
{
  int duration, distance;    //Adding duration and distance
  
  digitalWrite(triggerPin, HIGH); //triggering the wave
  delay(10);
  digitalWrite(triggerPin, LOW);
  
  duration = pulseIn(echoPin, HIGH); //function to wait/listen for echo
  distance = (duration/2) / 29.1; //transforming the number to cm(
  
  Serial.print(distance);    //printing the numbers
  Serial.print("cm");       //and the unit
  Serial.println(" ");      //just printing to a new line
  delay(2000);
}

(2) Bring Serial Monitor of UNOB (SMB).
(3) Bring in and bring out an object in front of the ultrasonic sensor. Check that the Serial Monitor shows reasonable value for the measured distance of the object.

2. Functional Testing of software UART Port based communication between UNOB and UNOJ.
(1) Leave the ultrasonic sensor connected as it is.
(2) Connect UNOB and UNOJ using software UART Ports (SUARTB and SUARTJ) as per Fig-1.
(3) Upload the following sketch (untested) in UNOB.

#include<SoftwareSerial.h>  //needed for the operation of SUARTB Port
SoftwareSerial SUARTB(A2, A3);  //SRX = DPin-A2, STX = DPin-A3

int triggerPin = A0; //triggering on pin A0
int echoPin = A1;    //echo on pin A1
int duration, distance;    //Adding duration and distance


void setup()
{
  Serial.begin(9600);  //we'll start serial comunication
  SUARTB.begin(9600);
  pinMode(triggerPin, OUTPUT); //defining pins
  pinMode(echoPin, INPUT);
}

void loop()
{
  untrasonic();    //get distance of passing by object
  SUARTB.print(distance);    //send distance information to UNOJ
  SUARTB.println();             //sending newline character
  delay(2000);
}

void ultrasoic()
{
  digitalWrite(triggerPin, HIGH); //triggering the wave
  delay(10);
  digitalWrite(triggerPin, LOW);

  duration = pulseIn(echoPin, HIGH); //function to wait/listen for echo
  distance = (duration / 2) / 29.1; //transforming the number to cm(

  Serial.print(distance);    //printing the numbers
  Serial.print("cm");       //and the unit
  Serial.println(" ");      //just printing to a new line
}

(4) Upload the following sketch in UNOJ.

#include<SoftwareSerial.h>  //needed for the operation of SUARTB Port
SoftwareSerial SUARTJ(A2, A3);  //SRX = DPin-A2, STX = DPin-A3

int triggerPin = A0; //triggering on pin A0
int echoPin = A1;    //echo on pin A1
int duration, distance;    //Adding duration and distance


void setup()
{
  Serial.begin(9600);  //we'll start serial comunication
  SUARTJ.begin(9600);
}

void loop()
{
  byte n = SUARTJ.available(); //check that a charcater has arrived/stored in Buffer
  if(n != 0)
  {
    char x = SUARTJ.read();    //read charcater from Buffer 
    Serial.print(x);   //show the charcater on Serial Monitor of UNOJ (SMJ)      
  }
}

(5) Bring in SMB and SMJ.

(6) Press RESET Buttons of both UNOs.

(7) Bring in and bring out an object in front of the ultrasonic sensor. Check that SMB and SMJ show identical values for the measured distance of the object.

3. Functional check of Servo Motor once message is received from UNOJ.
OP may try and post the codes that could be modified by the forum readers.

4digit.png

I honestly don’t know what to reply to… so ill start from the top.
#1:

Power_Broker:
Jumper cables will work just fine up to a certain distance. But then again, are you using the cable to connect them via Serial, SPI, I2C, etc? Are you planning on connecting grounds? How long is the cable?

I don’t know the difference. again, im a noob at Arduino. I THINK I will use serial… again, not 100% sure.
#2:

aarg:
“serial via UART” serial input basics

Yeah, I guess maybe im using Serial via UART.

The technique in the 3rd example in Serial Input Basics will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);    
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

If you are using two Unos it would probably be easiest if you use SoftwareSerial to create an extra serial port on each of them so you can leave pins 0 and 1 free for communication with the PC and for uploading the programs. The techniques in my Tutorial work equally well with SoftwareSerial.

…R

itsfoxy87:
#2:Yeah, I guess maybe im using Serial via UART.

You can use SerialTransfer.h to automatically communicate between two Arduinos via UART. The library is installable through the Arduino IDE and includes many examples.

Here are the library’s features:

This library:

  • can be downloaded via the Arduino IDE’s Libraries Manager (search “SerialTransfer.h”)
  • works with “software-serial” libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 255 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';
  
  myTransfer.sendData(3);
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

I'm dead keen to know the role of any comms in this project.

blomcrestlight:
I'm dead keen to know the role of any comms in this project.

comms?