Communication filaire entre 2 arduinos

Bonjour toniboudi

Voilà, ça se câble côté DUE sur Serial3:
Réception
Serial3.begin(9600); // Tx 14 > UNO Rx 2 Rx 15 > UNO Tx 3

` // Recoit

void setup() {
	Serial.begin(9600);
	Serial3.begin(9600);  // Tx 14 > UNO Rx 2   Rx 15 > UNO Tx 3
}

void loop() 
{
	if (Serial3.available() > 0) {
		String receivedString = Serial3.readStringUntil('\n');
		if (receivedString.startsWith("D:")) {
			float distance = receivedString.substring(2).toFloat();
			Serial.print("Received distance: ");
			Serial.println(distance);
		}
	}
}`

Côté UNO sur mySerial:
Envoi
SoftwareSerial mySerial(2, 3); // RX2 > DUE 14, TX 3 > DUE 15

#include <SoftwareSerial.h>

const int trigPin = 11;
const int echoPin = 12;

float duration, distance;

SoftwareSerial mySerial(2, 3); // RX2 > DUE 14, TX 3 > DUE 15

void setup() 
{
	pinMode(trigPin, OUTPUT);
	pinMode(echoPin, INPUT);
	Serial.begin(9600);
	mySerial.begin(9600);
}

void loop() 
{
	digitalWrite(trigPin, LOW);
	delayMicroseconds(2);
	digitalWrite(trigPin, HIGH);
	delayMicroseconds(10);
	digitalWrite(trigPin, LOW);

	duration = pulseIn(echoPin, HIGH);
	distance = (duration * 0.0343) / 2;

	Serial.print("Distance: ");
	Serial.println(distance);
	
	mySerial.print("D:");
	mySerial.println(distance);
	
	delay(100);
}

Importsnt, n'oublies pas la remarque de @fdufnews
Cordialement
jpbbricole

Bonjour fdufnews

Ce dessin contient des erreurs, il ne correspond pas aux 2 programmes de @toniboudi, des 2 côtés, Tx et Rx sont croisés.

Cordialement
jpbbricole

ah bon! merci beaucoup :+1: :+1: :+1:

Tu es un grand garçon, tu choisis les broches qu'il faut en TX ,et RX, mais tu mets un pont diviseur sur le TX de la Uno pour passer 5V en 3.3V.

Et ça devrait marcher :+1:

Ca vaudrait le coût de rectifier ton schema, pour correspondre au broche utilisé par les programmes de @toniboudi, si c'est simple à faire?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.