-1

Is my syntax "SoftwareSerial uno1(0,1); // RX, TX" is right to communicate with more than two Arduinos?. How can I communicate with three Unos via single Mega?

#include <SoftwareSerial.h>
SoftwareSerial uno1(0,1); // RX, TX
SoftwareSerial uno2(0,1); // RX, TX
SoftwareSerial uno3(0,1); // RX, TX

float lt[24]={3530,1580,3880,2780,4040,11260,7935,6655,2100,5100,1450,2200,2200,5900,6180,4230,2405,3560,4535,12635,12085,3500,930,3430};
float gt[24]={0,0,0,0,0,0,6320,5496.9,5948,4124.1,3848.4,3573,3022.2,3297.6,3298.2,3573,4123.2,0,0,0,0,0,0};
float lbt[24]={3530,1580,3880,2780,4040,7960,7935,2655,900,1400,1450,1300,1000,3400,3680,1730,2405,3560,4535,9635,9885,3500,930,3430};
float lb1[24]={1580,480,1580,1580,580,1950,3125,1625,250,500,250,600,500,1450,1480,630,530,1655,1475,3225,3300,1450,280,1730};
float lb2[24]={1450,600,1700,600,3080,3005,1655,500,250,500,600,350,250,1700,1700,600,1625,250,1405,3255,4455,1800,500,1450};
float lb3[24]={500,500,600,600,380.3005,3155,530,400,400,600,350,250,250,500,500,250,1655,1655,3155,2130,250,150,250};
float lst[24]={0,0,0,0,0,3300,0,4000,1200,3700,0,900,1200,2500,2500,2500,0,0,0,3000,2200,0,0,0};
float les1[24]={0,0,0,0,0,0,0,2000,1200,0,0,0,0,2500,2500,2500,0,0,0,0,1200,0,0,0};
float les2[24]={0,0,0,0,0,3300,0,0,0,0,0,900,1200,0,0,0,0,0,0,2000,0,0,0,0};
float les3[24]={0,0,0,0,0,0,0,2000,0,3700,0,0,0,0,0,0,0,0,0,1000,1000,0,0,0};
float BatCap[24]={405.83,405.83,405.83,305.83,305.83,305.83,305.83,305.83,588.91,576.24,799.08,936.45,1050.87,811.05,570.85,493.19,590.53,637.47,397.47,397.47,397.47,397.47,397.47,397.47};

float Pvpin=41,Batpin=42,Gridpin=43;

void setup() {
// DEFINING THE MODES OF LOADS, OBEVIOUSLY OUTPUT.
  pinMode(Pvpin,OUTPUT);
  pinMode(Batpin,OUTPUT);
  pinMode(Gridpin,OUTPUT);

  Serial.begin(9600);
  uno1.begin(9600);
  uno2.begin(9600);
  uno3.begin(9600);
  while (!Serial) {
     // wait for serial port to connect. Needed for native USB
  }
}

void loop() 
{
  for (int i=0;i<24;i++)
  {
   if (gt[i]>lt[i])
   {
       uno1.write( "100");
       uno2.write( "100");
       uno3.write( "100");
       digitalWrite(Pvpin,HIGH);
   } 
   else if (gt[i]+BatCap[i]>lt[i])
   {
       uno1.write( "110");
       uno2.write( "110");
       uno3.write( "110");
       digitalWrite(Batpin,HIGH);
       digitalWrite(Pvpin,HIGH);
   }
   else 
   {
       uno1.write( "001");
       uno2.write( "001");
       uno3.write( "001");
       digitalWrite(Gridpin,HIGH);
   }
  }
}
  • 3
    Why use SoftwareSerial when you have 4 UARTs on the Mega? – Edgar Bonet Apr 28 '17 at 15:58
  • @EdgarBonet how can i access 4UARTs in Mega? i need syntax for the case of three UARTs. – Hafeez Ahmad Fii Zue Apr 28 '17 at 16:01
  • @EdgarBonet please amend my code for serial communication with more than two Unos via single Mega. tha above code is for Mega. – Hafeez Ahmad Fii Zue Apr 28 '17 at 16:02
  • 1
    Serial1.begin(9600); Serial2.begin(9600); ... – Edgar Bonet Apr 28 '17 at 16:11
  • @gre_gor yes it is. – Hafeez Ahmad Fii Zue Apr 28 '17 at 17:45
1

If you want to use 3 SoftwareSerial instances, then you need to use 3 different sets of pins. Hence, replace:

SoftwareSerial uno1(0,1); // RX, TX
SoftwareSerial uno2(0,1); // RX, TX
SoftwareSerial uno3(0,1); // RX, TX

with something like:

SoftwareSerial uno1(0,1); // RX, TX
SoftwareSerial uno2(2,3); // RX, TX
SoftwareSerial uno3(4,5); // RX, TX

Also, since SoftwareSerial and Serial (hardware) are interchangeable in code, you should take advantage of the hardware Serial of Arduino UNO:

HardwareSerial uno1 = Serial;

Note that, on UNO, HardwareSerial uses pins 0 and 1, hence you should not use these for something else:

HardwareSerial uno1 = Serial;
SoftwareSerial uno2(2,3); // RX, TX
SoftwareSerial uno3(4,5); // RX, TX

Not the answer you're looking for? Browse other questions tagged or ask your own question.