0

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);
  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);
  }

}

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

  • I don't see a Serial1.begin() – chrisl Jul 7 '18 at 9:19
  • damn....I can't believe that I made such a ridiculous error. @chrisl should I just delete this question . Since it will have absolutely no use to this site. – Roshan Jul 7 '18 at 10:04
1

My answer might not be what you'd expect but I'll try anyway.

Did you troobleshoot your serial communications ? Try doing a simple programm for both cards and try sending data from one to another.

Uno code

#include <SoftwareSerial.h>

#define rxPin 10
#define txPin 11

SoftwareSerial s(rxPin, txPin);

String buff = "";

void setup()
{
    s.begin(9600);
    Serial.begin(9600);
}

void loop()
{
    if(Serial.available)
    {
        while(Serial.available())
        {
            buff += char(Serial.read());
        }
        s.print(buff);
        buff="";
    }

    if(s.available)
    {
        while(s.available())
        {
            buff += char(s.read());
        }
        Serial.print(buff);
        buff = "";
    }
}

Mega code

String buff = "";

void setup()
{
    Serial1.begin(9600);
    Serial.begin(9600);
}

void loop()
{
    if(Serial.available)
    {
        while(Serial.available())
        {
            buff += char(Serial.read());
        }
        Serial1.print(buff);
        buff="";
    }

    if(Serial1.available)
    {
        while(Serial1.available())
        {
            buff += char(Serial1.read());
        }
        Serial.print(buff);
        buff = "";
    }
}

Did you double check your connections ? Tx (mega) to rx (uno) and rx (mega) to tx(uno)

In your setup() on your arduino mega

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);
}

I don't see the Serial1.begin(9600) as said by @chrisl but you mentionned it above.

I hope this helps, sorry if I've not solved your problem.

  • you are correct, I missed it out in my code. – Roshan Jul 7 '18 at 10:06

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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