if statement confusing data ( or me ) SOLVED

I am sending some data to a logger, ( all sent as bytes ) , and have just got a testing code at the receive end, with the results monitored with Terminal on the PC.

If I just send all the data and time/date with the code below it logs fine :-

 }
void checkprinter () {  //check and see if a data packet has come in.

  if(ET.receiveData()){
    int(PIN ) = mydata.PIN ;    //  PIN 33 for Radiant
    if(PIN == 33 ){ 
      Serial.print (" PIN OK = "  ); 
      Serial.println (PIN );
      int( qgps) = mydata.qgps;  

      if ( qgps == 0 ){ 
        searching = 1; 
        queuetype = 1; 
      }
      if ( qgps == 1 ){ 
        searching = 0; 
        queuetype = 0; 
      }
      if ( qgps == 2 ){ 
        searching = 1; 
        queuetype = 1; 
      }
      if ( qgps == 3 ){ 
        searching = 0; 
        queuetype = 0; 
      }

      int (number) = mydata.number;

      // if (searching == 0 ) { // only update realtime clock if gps locked      
      int( clockyear) =  mydata.clockyear ; 
      clockyear = clockyear + 2000 ;
      int( clockmonth ) = mydata.clockmonth;      
      int ( clockday) = mydata.clockday;  
      int ( clockhours) = mydata.clockhours;
      int ( clockmins) = mydata.clockmins;         
      // } //  end of if seraching - 0

      Serial.print (" qgps = "  )      ;  
      Serial.println (qgps);
      Serial.print (" number = "  )    ;  
      Serial.println (number);
      Serial.print (" searching = "  ) ;  
      Serial.println (searching);
      Serial.print (" queuetype = "  ) ;  
      Serial.println (queuetype);     
      Serial.print (" clockmins = "  ) ;  
      Serial.println (clockmins);
      Serial.print (" clockhours = "  );  
      Serial.println (clockhours);
      Serial.print (" clockday = "  )  ;  
      Serial.println (clockday);
      Serial.print (" clockmonth = "  );  
      Serial.println (clockmonth);
      Serial.print (" clockyear = "  ) ;  
      Serial.println (clockyear);

    }  //  end of if pin =33
  } //  end of if any rx

  //you should make this delay shorter then your transmit delay or else messages could be lost
  delay(250);

}

The result is PIN OK = 33
qgps = 3
number = 2
searching = 0
queuetype = 0
clockmins = 35
clockhours = 8
clockday = 15
clockmonth = 4
clockyear = 2013

If I include the if statement that I commented out , as I only want to update the loggers clock if the GPS time coming in as right ie the remote GPS is locked, I lose the time and get as below

void checkprinter () {  //check and see if a data packet has come in.

  if(ET.receiveData()){
    int(PIN ) = mydata.PIN ;    //  PIN 33 for Radiant
    if(PIN == 33 ){ 
      Serial.print (" PIN OK = "  ); 
      Serial.println (PIN );
      int( qgps) = mydata.qgps;  

      if ( qgps == 0 ){ 
        searching = 1; 
        queuetype = 1; 
      }
      if ( qgps == 1 ){ 
        searching = 0; 
        queuetype = 0; 
      }
      if ( qgps == 2 ){ 
        searching = 1; 
        queuetype = 1; 
      }
      if ( qgps == 3 ){ 
        searching = 0; 
        queuetype = 0; 
      }

      int (number) = mydata.number;

      if (searching == 0 ) { // only update realtime clock if gps locked      
      int( clockyear) =  mydata.clockyear ; 
      clockyear = clockyear + 2000 ;
      int( clockmonth ) = mydata.clockmonth;      
      int ( clockday) = mydata.clockday;  
      int ( clockhours) = mydata.clockhours;
      int ( clockmins) = mydata.clockmins;         
      } //  end of if seraching - 0

      Serial.print (" qgps = "  )      ;  
      Serial.println (qgps);
      Serial.print (" number = "  )    ;  
      Serial.println (number);
      Serial.print (" searching = "  ) ;  
      Serial.println (searching);
      Serial.print (" queuetype = "  ) ;  
      Serial.println (queuetype);     
      Serial.print (" clockmins = "  ) ;  
      Serial.println (clockmins);
      Serial.print (" clockhours = "  );  
      Serial.println (clockhours);
      Serial.print (" clockday = "  )  ;  
      Serial.println (clockday);
      Serial.print (" clockmonth = "  );  
      Serial.println (clockmonth);
      Serial.print (" clockyear = "  ) ;  
      Serial.println (clockyear);

    }  //  end of if pin =33
  } //  end of if any rx

  //you should make this delay shorter then your transmit delay or else messages could be lost
  delay(250);

}

I get the times as below

PIN OK = 33
qgps = 3
number = 2
searching = 0
queuetype = 0
clockmins = <0>
clockhours = <0>
clockday = <0>
clockmonth = <0>
clockyear = <0>

the declared variables are

 #include <EasyTransfer.h>
  
  //create object
  EasyTransfer ET;
  int ledPin = 10;
  int searching;
  int queuetype; 
   byte number; 
  byte clockyear;
   byte clockmonth;
   byte clockday;
  byte clockhours;
  byte clockmins;
   byte qgps;

  struct RECEIVE_DATA_STRUCTURE{
    //put your variable definitions here for the data you want to receive
    //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
    // int blinks;
    // int pause;
  
       
     byte PIN;  // PIN 33 for Radiant
   //  byte searching;  //  gps locked = 1
     byte number; // broken down for old system and eeprom?
  //   byte numremainder;
     byte clockyear;
     byte clockmonth;
     byte clockday;
     byte clockhours;
     byte clockmins;
     byte qgps;   // 0 for no lock normal ,1 for lock normal , 2 for no lock express, 3 for lock express
 //  byte clocksecs;
  };
  
  //give a name to the group of data
  RECEIVE_DATA_STRUCTURE mydata;
  //*************************************************************************
  void setup(){
    Serial.begin(9600);
    Serial.println("setup");
    //start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
    ET.begin(details(mydata), &Serial);
  
    pinMode(ledPin, OUTPUT);
  
  }
  void loop()  
    checkprinter ();
  }

So it would seem that searching is never == 0, thus the mydata calls are never made.

Yet qgps == 3 every pass, which your serial.print's confirm. Very odd.

Logic question for you - what is different between qgps states 0,2 and 1,3? Both set the flags the same.

    int(PIN ) = mydata.PIN ;    //  PIN 33 for Radiant

What is this? The left side is casting PIN to an int. Why?

      int( qgps) = mydata.qgps;

Same here. The usage of the int() macro is wrong.

Thanks guys, I got myself mixed up there.

How many other Great Grandfathers get confused with this sometimes ? :slight_smile:

I didn't realise that you could have "byte number" in the EasyTranfer and also declare " int byte ".

As soon as I did that I could dump all the confusing bits and now have as below and it works.

I had to trim the tx data down as the fifo in the rf modules was breaking the message into 2, which confused the ET so I made everyting bytes, and combined the gps searching with the queue type in one byte - qgps. It all goes through fine now in one burst.

( I don't have time to find the wireless specs again to change the message length )

I reverted to my orig code for reading the qgps

void checkprinter () {  //check and see if a data packet has come in.
          
        if(ET.receiveData()){
   int(PIN ) = mydata.PIN ;    //  PIN 33 for Radiant
   if(PIN == 33 ){ Serial.print (" PIN OK = "  ); Serial.println (PIN );
   int( qgps) = mydata.qgps;  

   if ( qgps == 0 || 2 ){   searching = 0 ;   }  else {   searching = 1;  }
 if ( qgps == 1 || 3 ){  queuetype = 0 ; } else {  queuetype = 1;  } 
  
   int (number) = mydata.number;         
   if (searching == 0 ) { // only update realtime clock if gps locked      
       clockyear = mydata.clockyear + 2000 ;
       clockmonth  = mydata.clockmonth;      
       clockday = mydata.clockday;  
       clockhours = mydata.clockhours;
       clockmins = mydata.clockmins;            
        } //  end of if seraching - 0       
        Serial.print (" qgps = "  )      ;  Serial.println (qgps);
        Serial.print (" number = "  )    ;  Serial.println (number);
        Serial.print (" searching = "  ) ;  Serial.println (searching);
        Serial.print (" queuetype = "  ) ;  Serial.println (queuetype);     
        Serial.print (" clockmins = "  ) ;  Serial.println (clockmins);
        Serial.print (" clockhours = "  );  Serial.println (clockhours);
        Serial.print (" clockday = "  )  ;  Serial.println (clockday);
        Serial.print (" clockmonth = "  );  Serial.println (clockmonth);
        Serial.print (" clockyear = "  ) ;  Serial.println (clockyear);     
      }  //  end of if pin =33
        } //  end of if any rx
    //you should make this delay shorter then your transmit delay or else messages could be lost
      delay(250);
    
 }