Halllo,
i want sent data sensor from arduino nano to arduino promini
code Nano
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
int sensorValue3 = analogRead(A2);
int sensorValue4 = analogRead(A3);
Serial.print("V1=");
Serial.println(sensorValue1);
delay(1);
Serial.print("V2=");
Serial.println(sensorValue2);
delay(1);
Serial.print("V3=");
Serial.println(sensorValue3);
delay(1);
Serial.print("V3=");
Serial.println(sensorValue4);
delay(1);
}
What is the code for the receiver / Arduino Pormini?
is there an example?
Thank you very much
Send your four variables from Transmitter using comma (,) as separators among the variables, and > as the message terminator. If you do so, then it will be convenient to write receiver codes and separating your four variables. Also, use soft UART Port (mySerial) for communication as the hard UART Port is engaged with IDE/Serial Monitor for sketch uploading and debugging.
Transmitter Codes: (partial)
mySerial.print("V1="); mySerial.print(sensorValue1); mySerial.print(',');
mySerial.print("V2="); mySerial.print(sensorValue2); mySerial.print(',');
mySerial.print("V3="); mySerial.print(sensorValue3); mySerial.print(',');
mySerial.print("V4="); mySerial.print(sensorValue4); mySerial.print(',');
mySerial.print('>');
Receiver Codes: (untested)
#include<SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //SRX = DPIN-10 of UNO; STX = DPin-11 of UNO
char myData[100] = "";
char mySenso1[25];
int i=0, j=0;
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
}
void loop()
{
byte n = mySerial.available(); //check that a charcater has come from TX
if(n != 0)
{
byte m = mySerial.readBytesUntil('>', myData, 100); //save message in array except >
myData[m] = '\0'; //add null character
}
//----separate the variables----------------
do
{
mySensor1[i] = myData[j];
i++;
j++;
}
while(myData[j-1] != ',';
mySensor1[j-1] = '\0' ; //null character
Serial.println(mySensor1); //should show: V1=xx
//------ do for the rest-----------------------------
}
You can use SerialTransfer.h to automatically packetize and parse your data for inter-Arduino communication without the headace. 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 254 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()
{
char buff[] = "hi";
myTransfer.txObj(buff, sizeof(buff));
myTransfer.sendData(sizeof(buff));
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())
{
char buff[40];
myTransfer.rxObj(buff, sizeof(buff));
Serial.println("New Data: ");
Serial.write(buff, sizeof(buff));
Serial.println();
}
else if(myTransfer.status < 0)
{
Serial.print("ERROR: ");
if(myTransfer.status == -1)
Serial.println(F("CRC_ERROR"));
else if(myTransfer.status == -2)
Serial.println(F("PAYLOAD_ERROR"));
else if(myTransfer.status == -3)
Serial.println(F("STOP_BYTE_ERROR"));
}
}
For theory behind robust serial communication, check out the tutorials Serial Input Basics and Serial Input Advanced.
I tried this code
#include<SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //SRX = DPIN-10 of UNO; STX = DPin-11 of UNO
char myData[100] = "";
char mySenso1[25];
int i=0, j=0;
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
}
void loop()
{
byte n = mySerial.available(); //check that a charcater has come from TX
if(n != 0)
{
byte m = mySerial.readBytesUntil('>', myData, 100); //save message in array except >
myData[m] = '\0'; //add null character
}
//----separate the variables----------------
do
{
mySensor1[i] = myData[j];
i++;
j++;
}
while(myData[j-1] != ',';
mySensor1[j-1] = '\0' ; //null character
Serial.println(mySensor1); //should show: V1=xx
//------ do for the rest-----------------------------
}
however there is an error message
mySensor1 was not declared
zoekowalski:
mySensor1 was not declared
Because you made a typo in your array declaration. Did you see my previous post?