Why Is My Code Not Working ?

Master Code :

#include<SPI.h>
int swi=0;
char buf[100];
int pos=0;
char *DataOut="Hello World !\n";
String DataIn="";int Retry=0;

void setup() {
  Serial.begin(115200);
  digitalWrite(SS,HIGH);
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV8);
}

void loop() 
{
  long int time = millis(); int Time=0;
  while(time+5000>millis())
  {
  String Vector1(DataOut);
  SendToDev(DataOut);
  NormalizeData(Vector1);
  NormalizeData(DataIn);
  if((Vector1.indexOf(DataIn)>=0)&&(!DataIn.equals("")))
  {
    Serial.print("Data Send OK : ");
    Serial.println(DataIn);
  }
  else
  {
      Serial.println();
      Serial.print("Error MisMatch Echo Data : ");
      Serial.println(DataIn);
      Serial.println("Origin Data : "+Vector1);
  }
    delay(500);
    Time=ValidDev();
  if(Time==0)
  {
    Serial.print("OOPS SomeThing Went Wrong Retrying...");
    Serial.println(Retry);
    Retry++;
  }
  else
  {
    Serial.print("Waiting For Processing To Complete....");
    Serial.println(Time);
    delay(Time+10);
    time = millis();
    Retry=0;
    GetDev("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
    Serial.print("Processed : ");
    Serial.println(DataIn);
    delay(5);
  }
 } 
  Serial.print("Sorry Something Broke Trying 2 Fix.....");
}

void NormalizeData(String S)
{
  S.replace("\n","");
  S.replace("\r","");
  S.replace("\0","");
}

void SendToDev(char *P)
{
  DataIn="";
  char C;byte R;
  digitalWrite(SS,LOW);
  for(const char *S=P;C=*S;S++)
     {
         R=SPI.transfer(C);
         C=R;
         DataIn.concat(C);
     }
  DataIn=DataIn.substring(1);
  digitalWrite(SS,HIGH);
}

int ValidDev()
{
   byte R;int T;
   digitalWrite(SS,LOW);
   R=SPI.transfer('F');
   R=SPI.transfer('F');
   T=R;
   digitalWrite(SS,HIGH);
   Serial.println(T);
   return T;
}

void GetDev(char *P)
{
  DataIn="";
  char C;byte R;
  digitalWrite(SS,LOW);
  while((pos<255)&&(C!='\n'))
     {
         R=SPI.transfer(C);
         C=R;
         DataIn.concat(C);
         if(C=='\n')
            break;
         pos++;
     }
  digitalWrite(SS,HIGH);
  pos=0;
}

Slave Code :

#include<SPI.h>
char buf[100];
volatile byte pos;
volatile boolean process_it;
String DataIn;
volatile int waitime=75,stage=0;
char P[]="0000Data Processed \n\n\n";

void setup() 
{
 Serial.begin(115200);
 pinMode(MISO,OUTPUT);
 SPCR|=_BV(SPE);
 pos=0;
 process_it=false;
 SPI.attachInterrupt();
}

ISR(SPI_STC_vect)
{
   byte C=SPDR;
   if(stage==0)
   {
       SPDR=C;
     if(pos<sizeof buf)
     {
        buf[pos++]=C;
        if(C=='\n')
        {
            process_it=true;
            stage=1;
        }
      }
    }
    else if(stage==1)
    {
      SPDR=(byte)waitime;
      pos=0;
      stage=2;
    }
    else if(stage==2)
    {
         
         if(P[pos]=='\n')
          {
            stage=0;
            pos=0;
          }
          else
          {
            SPDR=P[pos];
            pos++;
          }
    }
}

void loop() 
{
 Serial.print(stage);
 Serial.print("Value Of Pos");
 Serial.println(pos);
 if(process_it)
 {
    buf[pos]=0;
    DataIn.concat(buf);
    if(strcmp(buf,"Hello World !"))
    {
     // Serial.println("Command OK ");
    }
    pos=0; 
    process_it=false;
    DataIn="";
 }

}

For some reason code broke after few runs and now for some reason pos value seems to gotten stuck at 100.
My goal is to retrieve text string as output after waiting for specific time length of string / char may vary from 84-164 bytes

Hardware Used Are ESP8266-ESP12 And Atmega328

This pattern repeats after reset?

Use serial.print statements to output your variable values to the serial terminal. You may have to insert some delays to slow it down a little. (Yes we don’t like delay() but this is for fault finding not for a running program.)

Weedpharma

Already tried delay same result should i post the code and screenshot ?? here

At the point where your variable is supposed to change, insert

Serial.print ("the value of variableName is = "); Serial.println (variableName);

Use this in various places with the different variable names to see what the value is at a particular place in the program. This will allow you to see what is happens to the variables as the program progresses.

Go through your program and look for places that determine what is supposed to happen on particular values of the variables. Put in the prints and check what actually is happening.

Your program will probably run too fast to read easily so put a delay(1000) after the print to give time to read the code.

Remove these additions once you have seen where the problem is.

Weedpharma

String DataIn;

Strings (capital S) can cause memory corruption in the small memory of an Arduino. It is safer to use strings (small s) which are char arrays terminated with a 0.

…R

Master Code

#include<SPI.h>
char buf[100];
volatile byte pos;
volatile boolean process_it;
String dataIn;
volatile int waitime=75,stage=0;
char p[]="0000Data Processed \n\n\n";

void setup() 
{
 Serial.begin(115200);
 pinMode(MISO,OUTPUT);
 SPCR|=_BV(SPE);
 pos=0;
 process_it=false;
 SPI.attachInterrupt();
}

ISR(SPI_STC_vect)
{
   byte C=SPDR;
   if(stage==0)
   {
       SPDR=C;
     if(pos<sizeof buf)
     {
        buf[pos++]=C;
        if(C=='\n')
        {
            process_it=true;
            stage=1;
            Serial.print(stage);
            Serial.print(" Value Of Pos ");
            Serial.println(pos);
        }
      }
    }
    else if(stage==1)
    {
      SPDR=(byte)waitime;
      pos=0;
      stage=2;
    }
    else if(stage==2)
    {
         
         if((p[pos]=='\n')||(p[pos]=='\0'))
          {
            stage=0;
            pos=0;
          }
          else
          {
            SPDR=p[pos];
            pos++;
          }
    }
}

void loop() 
{
 Serial.print(stage);
 Serial.print("Value Of Pos");
 Serial.println(pos);
 if(process_it)
 {
    buf[pos]=0;
    dataIn.concat(buf);
    if(strcmp(buf,"Hello World !"))
    {
     // Serial.println("Command OK ");
    }
    pos=0; 
    process_it=false;
    dataIn="";
 }

}

Slave Code

#include<SPI.h>
char buf[100];
volatile byte pos;
volatile boolean process_it;
String dataIn;
volatile int waitime=75,stage=0;
char p[]="0000Data Processed \n\n\n";

void setup() 
{
 Serial.begin(115200);
 pinMode(MISO,OUTPUT);
 SPCR|=_BV(SPE);
 pos=0;
 process_it=false;
 SPI.attachInterrupt();
}

ISR(SPI_STC_vect)
{
   byte C=SPDR;
   if(stage==0)
   {
       SPDR=C;
     if(pos<sizeof buf)
     {
        buf[pos++]=C;
        if(C=='\n')
        {
            process_it=true;
            stage=1;
            Serial.print(stage);
            Serial.print(" Value Of Pos ");
            Serial.println(pos);
        }
      }
    }
    else if(stage==1)
    {
      SPDR=(byte)waitime;
      pos=0;
      stage=2;
    }
    else if(stage==2)
    {
         
         if((p[pos]=='\n')||(p[pos]=='\0'))
          {
            stage=0;
            pos=0;
          }
          else
          {
            SPDR=p[pos];
            pos++;
          }
    }
}

void loop() 
{
 Serial.print(stage);
 Serial.print("Value Of Pos");
 Serial.println(pos);
 if(process_it)
 {
    buf[pos]=0;
    dataIn.concat(buf);
    if(strcmp(buf,"Hello World !"))
    {
     // Serial.println("Command OK ");
    }
    pos=0; 
    process_it=false;
    dataIn="";
 }

}

Debug code

and for somereason its not sending 75 correctly but when it dose it works correctly it is being recieved as 70.
still didnt work it runs fine for the first time then on second try goes beyond whats the limit when data comes again looks like syncronization problem if it is can anyone tell me how to re sync it
can anyone run this code and confirm it.

Serial I/O in an interrupt service routine is a Bad Ideatm.

Don’t do it.

did it to debug the code as for the “Serial I/O in an interrupt service routine is a Bad Idea” i know that why slowed down the code by 200ms per character

Master Code

#include<SPI.h>
int swi=0;
char buf[100];
int pos=0;
char *DataOut="Hello World !\n";
String DataIn="";int Retry=0;

void setup() {
  Serial.begin(115200);
  digitalWrite(SS,HIGH);
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV8);
}

void loop() 
{
  long int time = millis(); int Time=0;
  while(time+5000>millis())
  {
  String Vector1(DataOut);
  SendToDev(DataOut);
  NormalizeData(Vector1);
  NormalizeData(DataIn);
  if((Vector1.indexOf(DataIn)>=0)&&(!DataIn.equals("")))
  {
    Serial.print("Data Send OK : ");
    Serial.println(DataIn);
  }
  else
  {
      Serial.println();
      Serial.print("Error MisMatch Echo Data : ");
      Serial.println(DataIn);
      Serial.println("Origin Data : "+Vector1);
  }
    delay(500);
    Time=ValidDev();
  if(Time==0)
  {
    Serial.print("OOPS SomeThing Went Wrong Retrying...");
    Serial.println(Retry);
    Retry++;
  }
  else
  {
    Serial.print("Waiting For Processing To Complete....");
    Serial.println(Time);
    delay(Time+10);
    time = millis();
    Retry=0;
    GetDev("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
    Serial.print("Processed : ");
    Serial.println(DataIn);
  }
 } 
  Serial.print("Sorry Something Broke Trying 2 Fix.....");
}

void NormalizeData(String S)
{
  S.replace("\n","");
  S.replace("\r","");
  S.replace("\0","");
}

void SendToDev(char *P)
{
  DataIn="";
  char C;byte R;
  digitalWrite(SS,LOW);
  for(const char *S=P;C=*S;S++)
     {
         R=SPI.transfer(C);
         C=R;
         DataIn.concat(C);
     }
  DataIn=DataIn.substring(1);
  digitalWrite(SS,HIGH);
}

int ValidDev()
{
   byte R;int T;
   digitalWrite(SS,LOW);
   R=SPI.transfer('F');
   R=SPI.transfer('F');
   T=R;
   digitalWrite(SS,HIGH);
   Serial.println(T);
   return T;
}

void GetDev(char *P)
{
  DataIn="";
  char C;byte R;
  digitalWrite(SS,LOW);
  while(pos<255)
     {
         R=SPI.transfer(C);
         C=R;
         DataIn.concat(C);
         if(C=='\n')
            break;
         pos++;
         delayMicroseconds(55); //added for test
     }
  digitalWrite(SS,HIGH);
  pos=0;
}

Slave Code

#include<SPI.h>
char buf[100];
volatile byte pos;
volatile boolean process_it;
String DataIn;
volatile int waitime=75,stage=0;
char P[]="Data Processed \n";
int long time=millis();

void setup() 
{
 Serial.begin(115200);
 pinMode(MISO,OUTPUT);
 SPCR|=_BV(SPE);
 pos=0;
 process_it=false;
 SPI.attachInterrupt();
}

ISR(SPI_STC_vect)
{
   byte C=SPDR;
   if(stage==0)
   {
       SPDR=C;
     if(pos<sizeof buf)
     {
        buf[pos++]=C;
        if(C=='\n')
        {
            process_it=true;
            stage=1;
            time=millis();
        }
      }
    }
    else if(stage==1)
    {
      SPDR=(byte)waitime;
      pos=0;
      stage=2;
    }
    else if(stage==2)
    {
         SPDR=P[pos];
         if(P[pos++]=='\n')
          {
            stage=0;
            pos=0;
          }
    }
}

void loop() 
{
 if(process_it)
 {
    buf[pos]=0;
    DataIn.concat(buf);
    if(strcmp(buf,"Hello World !"))
    {
      Serial.println("Command OK ");
    }
    pos=0; 
    process_it=false;
    DataIn="";
 }

}

This code works partially with only one exception still receiving 70 instead of 75
Fixed it and it broke again when i modified “GetDev(“FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF”);”

Found the error i guess but cant get it to fix everything looks fine but for some reason cant seem to get the slave to transfer waitime

    else if(stage==1)
    {
      SPDR=(byte)waitime;
      pos=0;
      stage=2;
    }

rest of the code is same

      SPDR=(byte)waitime;

Why are you casting the int to a byte? What does that do with the high order byte?