Pages: [1]   Go Down
Author Topic: loop only runs once  (Read 1050 times)
0 Members and 1 Guest are viewing this topic.
Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  #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
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

   
Code:
#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
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

France
Offline Offline
God Member
*****
Karma: 34
Posts: 978
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Change the <= to < in the for loop

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

Or better:

for (int n = 0; n < sizeof(tile); n++ )
« Last Edit: September 05, 2012, 10:57:26 am by guix » Logged

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25706
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

"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.

France
Offline Offline
God Member
*****
Karma: 34
Posts: 978
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Leeds, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25706
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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   :-)
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

France
Offline Offline
God Member
*****
Karma: 34
Posts: 978
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Show Your Work
Offline Offline
Edison Member
*
Karma: 14
Posts: 1072
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's bound to cause problems.



* Rimshot.gif (7.43 KB, 48x34 - viewed 117 times.)
« Last Edit: September 05, 2012, 11:31:53 am by JoeN » Logged

I have only come here seeking knowledge. Things they would not teach me of in college.

Pages: [1]   Go Up
Jump to: