Skip to main content
added 28 characters in body
Source Link
roaibrain
  • 211
  • 1
  • 10
#include <SoftwareSerial.h>
SoftwareSerial s(10, 11);
void setup() 
{
  delay(200);
  Serial.begin(9600);
  Serial1.begin(9600);  
  s.begin(9600);
  while (!Serial) ;  // wait for Arduino Serial Monitor  
}
void loop() 
{  
  if(s.available()>0)
  {
     Serial.print("\nrec\n");
     delay(100);
     String b = s.readStringUntil('\n');
     delay(100);
     Serial.print(b);
     delay(40);
  }
#include <SoftwareSerial.h>
SoftwareSerial s(10, 11);
void setup() 
{
  delay(200);
  Serial.begin(9600);  
  s.begin(9600);
  while (!Serial) ;  // wait for Arduino Serial Monitor  
}
void loop() 
{  
  if(s.available()>0)
  {
     Serial.print("\nrec\n");
     delay(100);
     String b = s.readStringUntil('\n');
     delay(100);
     Serial.print(b);
     delay(40);
  }
#include <SoftwareSerial.h>
SoftwareSerial s(10, 11);
void setup() 
{
  delay(200);
  Serial.begin(9600);
  Serial1.begin(9600);  
  s.begin(9600);
  while (!Serial) ;  // wait for Arduino Serial Monitor  
}
void loop() 
{  
  if(s.available()>0)
  {
     Serial.print("\nrec\n");
     delay(100);
     String b = s.readStringUntil('\n');
     delay(100);
     Serial.print(b);
     delay(40);
  }
Source Link
roaibrain
  • 211
  • 1
  • 10

Serial communication between 2 arduino's doesn't works when Sending string in a program with a lot of functions

Note :

This question is completely different from the questions that I have asked before although the code snippets might be the same, So please do not waive it as a duplicate right when you see it.

In my setup I have connected two arduino's Serially. An Arduino Mega is the one that is sending the string and a Arduino Uno is the one that's receiving it.

  • Arduino Mega : TX1(19),RX1(18) - Serial1
  • Arduino Uno : RX(10),TX(11) - using SoftwareSerial
  • Gnd is connecetd on both boards

Arduino UNO - code is same for both cases

#include <SoftwareSerial.h>
SoftwareSerial s(10, 11);
void setup() 
{
  delay(200);
  Serial.begin(9600);  
  s.begin(9600);
  while (!Serial) ;  // wait for Arduino Serial Monitor  
}
void loop() 
{  
  if(s.available()>0)
  {
     Serial.print("\nrec\n");
     delay(100);
     String b = s.readStringUntil('\n');
     delay(100);
     Serial.print(b);
     delay(40);
  }

}

When the code in my arduino mega has only a print statement to send a string an infinite no. of times to the Uno, The Uno is receiving the string properly.

Arduino Mega

void setup()
{
  Serial1.begin(9600);
}
void loop() 
{
  Serial1.println("e2e4");   
  delay(100);
}

Now when I send the string an infinite no. of times from the Arduino mega but this time the mega has a lot more functions included in the program as well. In this case the Uno does not receive the string sent.

Arduino Mega - code containing a lot of functions and sedning a string to Uno

int SensorValStore_Prev[8][8] = { {0,0,0,0,0,0,0,0}, \
                                  {0,0,0,0,0,0,0,0}, \
                                  {1,1,1,1,1,1,1,1}, \
                                  {1,1,1,1,1,1,1,1}, \
                                  {1,1,1,1,1,1,1,1}, \
                                  {1,1,1,1,1,1,1,1}, \
                                  {0,0,0,0,0,0,0,0}, \
                                  {0,0,0,0,0,0,0,0} };

int SensorValStore_Curr[8][8]={ {0,0,0,0,0,0,0,0}, \
                                {0,0,0,0,0,0,0,0}, \
                                {1,1,1,1,1,1,1,1}, \
                                {1,1,1,1,1,1,1,1}, \
                                {1,1,1,1,1,1,1,1}, \
                                {1,1,0,1,1,1,1,1}, \
                                {0,0,0,0,0,0,0,0}, \
                                {0,1,0,0,0,0,0,0} };
//int SensorValStore_Curr[8][8];
int sum1[8],sum2[8],temp[8][8],k,l,i,j,rank,nextRow;
String file,moveSan,Temp,bestMove;
char Buffer[20];
void doAction();
void ReadSensor();
void Difference();
void binToIntCalc();
void check();
void getMovePlayed();
void SendMove();
void getMove();

void setup()
{
  Serial.begin(9600);
  lcd.begin(16,2);
  pinMode(22,INPUT_PULLUP); // Reed switch - Row1
  pinMode(24,INPUT_PULLUP); // Reed switch - Row2
  pinMode(26,INPUT_PULLUP); // Reed switch - Row3
  pinMode(28,INPUT_PULLUP); // Reed switch - Row4
  pinMode(30,INPUT_PULLUP); // Reed switch - Row5 
  pinMode(32,INPUT_PULLUP); // Reed switch - Row6 
  pinMode(34,INPUT_PULLUP); // Reed switch - Row7 
  pinMode(36,INPUT_PULLUP); // Reed switch - Row8 
  pinMode(2,INPUT_PULLUP);// Push Button
  pinMode(38,OUTPUT);
  pinMode(40,OUTPUT);
  pinMode(42,OUTPUT);
  pinMode(44,OUTPUT);
  pinMode(46,OUTPUT);
  pinMode(48,OUTPUT);
  pinMode(50,OUTPUT);
  pinMode(52,OUTPUT);
  pinMode(3,OUTPUT);
  digitalWrite(38,HIGH);
  digitalWrite(40,HIGH);
  digitalWrite(42,HIGH);
  digitalWrite(44,HIGH);
  digitalWrite(46,HIGH);
  digitalWrite(48,HIGH);
  digitalWrite(50,HIGH);
  digitalWrite(52,HIGH);
  digitalWrite(3,LOW);
}

void loop()
{
  
  // doAction();
  delay(40);
  Difference();
  delay(40);
  binToIntCalc();
  delay(40);
  check();
  delay(40);
  getMovePlayed();
  delay(40);
  getMove();
  delay(40);
  //}
}

void Print(int matrix[8][8])
{
  for(i=0;i<8;i++)
  {
    for(j=0;j<8;j++)
    {
      Serial.print(matrix[i][j]);
      Serial.print(" ");
    }
    Serial.println();
  }
}

void doAction()
{
  nextRow = 0;
  for(i=0;i<8;i++)
  {
    digitalWrite(38 + nextRow,LOW);
    delay(40);
    ReadSensor(i);
    digitalWrite(38 + nextRow,HIGH);
    delay(40);
    nextRow +=2;
  }
}

void ReadSensor(int iter)  
{
  delay(10);
  for(j=0;j<8;j++)
    SensorValStore_Curr[iter][j] = digitalRead(j+2);
}

void Difference()
{
  for(i=0;i<8;i++)
    for(j=0;j<8;j++)
      temp[i][j] = abs(SensorValStore_Curr[i][j] - SensorValStore_Prev[i[j]);
 
}

void binToIntCalc()
{
  for(i=0;i<8;i++)
    for(j=0;j<8;j++)
    {
      sum1[i] += pow(2,j)*SensorValStore_Prev[i][j];
      sum2[i] += pow(2,j)*SensorValStore_Curr[i][j];
    }
 }

void check()
{
  k=0;
  l=0;
  for(i=0;i<8;i++)
  {   
    if(sum2[i]>sum1[i])
      k = i;
    if(sum2[i]<sum1[i])
      l = i;
    delay(40);
  }
}

void getMovePlayed()
{
  for(j=0;j<8;j++)
  {
    if(temp[k][j] == 1)
    {
      rank =k+1;
      file = String(char(97+j));
      delay(40);
      moveSan = file + rank;
      Temp = moveSan;
    }
  }
  for(j=0;j<8;j++)
  { 
    if( temp[l][j] == 1)
    {
      rank =l+1;
      file = String(char(97+j));
      delay(40);
      moveSan = file + rank;
      Temp.concat(moveSan);
    }
  }
}

void SendMove(String movePlayed)
{
  Serial.println("start");
  Serial.print(movePlayed);
  Serial.println();
}

void getMove() // SENDING STRING TO THE ARDUINO UNO
{
 while(1){
  Serial1.println("e2e4");   
  delay(100);
  }
}

Note: getMove() is the function that sends the string to the arduino through Serial1