loop only runs once

I just want to monitor an input from various remote controls, if their PIN matches it must display the 15 bytes of data.
It works fine for the first message in, it SerialPrints the PIN and 15 bytes, then nothing..

Must I flush the incoming buffer ? or is this another brain fart / senior moment ?

  #include <NewSoftSerial.h>
      #define RXPIN 14 //   
      #define TXPIN 15 //   
     #define SIMBAUD 9600  
   int n = 1;
int PIN = 40;      
       NewSoftSerial SIM(RXPIN, TXPIN);
int tile [15];

  void setup ()  {
     SIM.begin(SIMBAUD);      
     Serial.begin(9600);   }
  //***************************************************************************************
  void loop()   { 
 if (SIM.available() > 0) {   // get incoming byte:
  Serial.println(" something available ");   
    int indata = SIM.read ();
    if (indata == PIN || indata == 39){  Serial.println(indata); 
      for (int n = 1; n <=15; n++ ){        
       tile [n] =  SIM.read ();  
          Serial.println(tile [n]);
      }     }       }  // end of is serial avail  
   }// end of

tile[] is 15 elements long which means valid elements are 0 to 14.

Your for() loop accesses elements 1 to 15.

That's bound to cause problems.

Thanks James, it works in the main receivers of the project, but I tried as below and its the same ?

 #include <NewSoftSerial.h>
      #define RXPIN 14 //   
      #define TXPIN 15 //   
     #define SIMBAUD 9600  
   int n = 1;
int PIN = 40;      
       NewSoftSerial SIM(RXPIN, TXPIN);
int tile [15];

  void setup ()  {
     SIM.begin(SIMBAUD);      
     Serial.begin(9600);   }
  //***************************************************************************************
  void loop()   { 
 if (SIM.available() > 0) {   // get incoming byte:
  Serial.println(" something available ");   
        for (int n = 0; n <=15; n++ ){        
       tile [n] =  SIM.read ();  
          Serial.println(tile [n]);  
         }       
       }  // end of is serial avail  
   }// end of loop

Change the <= to < in the for loop

for (int n = 0; n < 15; n++ )

Or better:

for (int n = 0; n < sizeof(tile); n++ )

I am checking 16 bytes, the first one I was seperating for the PIN number ( address if you like ) but I am now trying to read all 16 )

I have tried for (int n = 0; n < sizeof(tile); n++ ) but doesnt work either ...

The actual system that I want to test works fine, its just this quickcheck that is messing me around.

I think I must give it a break and think afresh later

You may be checking 16 bytes but your buffer is only 15 bytes long.

Yes, I'm not sure to understand your problem but try with int tile [16]; :slight_smile:

Using sizeof (tile) will surely return the number of bytes in tile and since it is an integer array twice the value you want.

Paul

Or better still, define the size of your buffer in one place as a constant, and use that throughout.

I can also thoroughly recommend the auto format tool in the IDE for keeping your code legible.

Thanks Guys

It was a brain fart, tile [16] of course it should have been - must get some sleep :slight_smile:

The sketch snippet was scruffy as it was just a testing scribble AWOL :slight_smile:

countrypaul:
Using sizeof (tile) will surely return the number of bytes in tile and since it is an integer array twice the value you want.

Paul

Sorry my bad, I'm accustomed to typeless languages :cold_sweat:

Glad to help, Boffin1 :slight_smile:

Rimshot.gif