Go Down

Topic: loop only runs once (Read 1 time) previous topic - next topic

Boffin1

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 ?

 
Code: [Select]
  #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

James C4S

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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Boffin1

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

   
Code: [Select]
#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

guix

#3
Sep 05, 2012, 05:53 pm Last Edit: Sep 05, 2012, 05:57 pm by guix Reason: 1
Change the <= to < in the for loop

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

Or better:

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

Boffin1

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


AWOL

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

guix

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

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

AWOL

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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Boffin1

Thanks Guys

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

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

guix


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 :smiley-roll-sweat:


Glad to help, Boffin1 :)

JoeN

#11
Sep 05, 2012, 06:30 pm Last Edit: Sep 05, 2012, 06:31 pm by JoeN Reason: 1
I have only come here seeking knowledge. Things they would not teach me of in college.

Go Up