sending HEX bytes only once in code

Hello everyone. I am here with new problem kindly help me in resolving.
I have attached my arduino UNO with NEO6M GPS module via UART.
I made connections like
GND OF UNO ------> GND OF NEO6M
5V OF UNO ------------->VCC of NEO6M
PIN 6 AS RX-------------->TX OF GPS MODULE
PIN7 AS TX--------------> RX OF GPS MODULE
Now I want to send some bytes to GPS module i want to change settings via arduino. Once I place these bytes in a loop so they are transmitted again and again and if i put them in setup then they are transmitted at the very start. How can I send these bytes in a loop (for while or any loop) such that it is transmitted only once in complete program and are not repeatedly sent again and again even if loop is repated again and again. I am not good at these codes kindly do help me. I have attached hex bytes which I want to send.

uint8_t ubx_cfg_gnss[] = {
    0xB5,0x62,0x06,0x3E,0x2C,0x00, 
    0x00,0x00,0x10,0x05, 
    0x00,0x08,0x10,0x00,0x01,0x00,0x01,0x01, 
    0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
    0x03,0x08,0x10,0x00,0x00,0x00,0x01,0x01, 
    0x05,0x00,0x03,0x00,0x00,0x00,0x01,0x01, 
    0x06,0x08,0x0E,0x00,0x00,0x00,0x01,0x01, 
    0xE8,0xFE }; 
 
  Serial.write(ubx_cfg_gnss,sizeof(ubx_cfg_gnss));

How can I send these bytes in a loop (for while or any loop) such that it is transmitted only once in complete program and are not repeatedly sent again and again

Only send the bytes when a boolean is true. Once the bytes have been sent set to boolean to false to prevent them being sent again.

if (okToSend)

{
//code to send bytes goes here
okToSend = false;
}

then it would be like this i think

if (okToSend)
{uint8_t ubx_cfg_gnss[] = {[color=#222222][/color]
    0xB5,0x62,0x06,0x3E,0x2C,0x00,[color=#222222][/color]
    0x00,0x00,0x10,0x05,[color=#222222][/color]
    0x00,0x08,0x10,0x00,0x01,0x00,0x01,0x01,[color=#222222][/color]
    0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x01,[color=#222222][/color]
    0x03,0x08,0x10,0x00,0x00,0x00,0x01,0x01,[color=#222222][/color]
    0x05,0x00,0x03,0x00,0x00,0x00,0x01,0x01,[color=#222222][/color]
    0x06,0x08,0x0E,0x00,0x00,0x00,0x01,0x01,[color=#222222][/color]
    0xE8,0xFE };[color=#222222][/color]
 [color=#222222][/color]
  Serial.write(ubx_cfg_gnss,sizeof(ubx_cfg_gnss));
okToSend = false;
}

Should I send it like this?
If I show you rest of my code can you guide me according to that?

Basically I am doing project on GPS enabling. So in that I have to code such that GPOS signals are not there so you should jump into GLONASS. i have made code which is showing GPS is ok on LCD when gps is giving signals. And when GPS signals are not there it is showing GPS: invalid. This process is automatic and decide itself once signals route through module. Now inbetween part is that time when It shows GPS invalid, at that moment I want to send these bytes and it would enable other system to route signals. Once I insert this bytes in loop upon condition which is if (token==‘V’)
These bytes are sent and sent again and again because of which my GPS module wont work. Thts why I want to make this code reliable in way such that If It enables then it wont repeat this step again.
Kindly help me I have no other solution

#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// Choose two Arduino pins to use for software serial
int RXPin = 6;
int TXPin = 7;

//Default baud of NEO-6M is 9600
int GPSBaud = 9600;

// Create a software serial port called "gpsSerial"
SoftwareSerial gpsSerial(RXPin, TXPin);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define MAX_NMEA 200 // Max characters in NMEA Sentence to accumulate

float gpsSpeed;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void processGGA(char *nmea)
{
    // TODO
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void processGLL(char *nmea)
{
    // TODO
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// $GPRMC,144326.00,A,5107.0017737,N,11402.3291611,W,0.080,323.3,210307,0.0,E,A*20
//  $GPRMC,
// 0  144326.00     Time
// 1  A             Fix A=OK, V=INVALID
// 2  5107.0017737  Lat
// 3  N
// 4  11402.3291611 Lon
// 5  W
// 6  0.080         Speed
// 7  323.3         Course
// 8  210307        Date
// 9  0.0
// 10 E
// 11 A
//  *20

void processRMC(char *nmea)
{
  char *token = strtok(nmea, ",");  // Split NMEA string with ',' delimiter
  if (token) // Is Token NULL?
  {
    int i;
    for(i=0; i<7; i++) // Process Fields, truncated
    {
      token = strtok(NULL, (i < 11) ? "," : "*"); // delimit based on expected termination
      if (!token) break; // NULL Token
      if (i == 1) // Field 1 Fix
      {
        
        if (token[0] == 'A')
         { lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("GPS:OK");
        digitalWrite(9, HIGH);
        digitalWrite(8, LOW);
        }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        else if (token[0] == 'V')
          {
          lcd.setCursor(0,0);
            lcd.print("GPS:INVALID");
          
    digitalWrite(8, HIGH);
    delay(7000);
    
    
  uint8_t ubx_cfg_gnss[] = {
    0xB5,0x62,0x06,0x3E,0x2C,0x00, // UBX-CFG-GNSS
    0x00,0x00,0x10,0x05, // 16-Channels, 5-Constellations in list
    0x00,0x08,0x10,0x00,0x01,0x00,0x01,0x01, // GPS
    0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x01, // SBAS
    0x03,0x08,0x10,0x00,0x00,0x00,0x01,0x01, // GALILEO
    0x05,0x00,0x03,0x00,0x00,0x00,0x01,0x01, // BEIDOU
    0x06,0x08,0x0E,0x00,0x00,0x00,0x01,0x01, // GLONASS
    0xE8,0xFE }; // compute checksum
 
  Serial.write(ubx_cfg_gnss,sizeof(ubx_cfg_gnss));
  digitalWrite(8, LOW);
  lcd.setCursor(0,0);
            lcd.print("GPS:gone");
  delay(100000);
         
          }
         
 
      if (i == 6) // Field 6 Speed Knots (Basically a Nautical Mile, or One Arc Minute)
       { gpsSpeed = atof(token) / (3600.0 / 1852.0); // Pull as a floating point (in knots, convert to m/s)
    }
  }
    }
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void readSerialGPS()
{
  static char receivedGPSChars[MAX_NMEA]; // buffer for received GPS data
  static int ndx = 0; // buffer index
  static boolean newGPSData = false;

  while((gpsSerial.available() > 0) && (newGPSData == false)) // Drop out at each complete line
  {
    char rc = gpsSerial.read();

    Serial.write(rc); // Feed byte from GPS to the console

    if (rc != '\r') { // Terminate line, CR,LF pair
      if (rc == '

) // Resync on leading ’



        ndx = 0;
      if (rc != '\n') // Don't copy LF
        receivedGPSChars[ndx++] = rc;
      if (ndx >= MAX_NMEA) // Constrain length
        ndx = MAX_NMEA - 1;
    }
    else { // '\r'
      receivedGPSChars[ndx] = '\0'; // Terminate the string
      ndx = 0;
      newGPSData = true;
    }
  } // while

  if (newGPSData) // One new line available
  {
    static boolean gpsAlert = false;

    if (gpsAlert == false)
    {
      lcd.setCursor(0,0);
      lcd.print("GPS:ENABLED");
      

      gpsAlert = true;
    }

    if (strncmp(receivedGPSChars, "$GPGGA", 6) == 0)
      processGGA(receivedGPSChars);
    else if (strncmp(receivedGPSChars, "$GPGLL", 6) == 0)
      processGLL(receivedGPSChars);
    else if (strncmp(receivedGPSChars, "$GPRMC", 6) == 0)
      processRMC(receivedGPSChars);

    newGPSData = false;
  }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void setup()
{
  // Start the Arduino hardware serial port at 9600 baud
  Serial.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  pinMode(9, OUTPUT);
  // Start the software serial port at the GPS's default baud
  gpsSerial.begin(GPSBaud);

    
            
          
    
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void loop()
{
  readSerialGPS(); // Consume data, process sentences
}

The code in post #3 does not compile. Fix that first

As a matter of interest, exactly how did you post the code in reply #2 as I think it has been mangled by the forum software adding color tags unless you did it for some reason. Have you by any chance got the WYSIWYG option enabled in your forum settings ?

Sorry I copied the code from my post so it got color tag.
No i didnt got that chance to enable setting you said.

Kindly compile it I have done correction in code. :slight_smile:

i didnt got that chance to enable setting you said.

Please do not enable it causes problems with code tags, which is why I asked if you already had it enabled

I copied the code from my post so it got color tag.

How exactly did you copy it ?
I am trying to track down a bug in the forum software so that it can be reported and fixed but need to know exactly how to cause the problem

OK. It compiles now

Where are you stuck in adding the conditional code controlled by a boolean ?

Where are you stuck in adding the conditional code controlled by a boolean ?

In the following part of code LCD shows GPS:OK when signals are got by receiver.

if (token[0] == 'A')
         { lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("GPS:OK");
        digitalWrite(9, HIGH);
        digitalWrite(8, LOW);
        }

In this part once NO signals found lCD show GPS:INVALID

else if (token[0] == 'V')
          {
          lcd.setCursor(0,0);
            lcd.print("GPS:INVALID");
          
    digitalWrite(8, HIGH);
    delay(7000);
          }

I want that once it show GPS invalid then after some time (delay) it should automatically show GLONASS ENable ON LCD. This would be done by sending these bytes via arduino to receiver.

uint8_t ubx_cfg_gnss[] = {
    0xB5,0x62,0x06,0x3E,0x2C,0x00, // UBX-CFG-GNSS
    0x00,0x00,0x10,0x05, // 16-Channels, 5-Constellations in list
    0x00,0x08,0x10,0x00,0x01,0x00,0x01,0x01, // GPS
    0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x01, // SBAS
    0x03,0x08,0x10,0x00,0x00,0x00,0x01,0x01, // GALILEO
    0x05,0x00,0x03,0x00,0x00,0x00,0x01,0x01, // BEIDOU
    0x06,0x08,0x0E,0x00,0x00,0x00,0x01,0x01, // GLONASS
    0xE8,0xFE }; // compute checksum
 
  Serial.write(ubx_cfg_gnss,sizeof(ubx_cfg_gnss));

These bytes will just enable other system. I want to code in such a way that These bytes are sent just once. Once sent, then GLONASS ENABLE should stay on LCD until signals are getting.
Please help in this part.

OK. You need to detect when the GPS signal becomes invalid rather than when it is invalid. Note the subtle difference. To do that you need to detect the change of state not the current state. Take a look at the StateChangeDetection example in the IDE to see the principle in action. The example uses a digital input rather than the value of a character but the principle is the same.

To detect the change you save the current value of token[0] as the previous value before reading it again. Then you compare the previous value with the current value and if it has changed you act on it. What you do depends on what you want to achieve.

I want that once it show GPS invalid then after some time (delay) it should automatically show GLONASS ENable ON LCD.

That is a new requirement, but not difficult. Do NOT consider using the delay() function as it blocks program execution. Instead use millis() for the timing

When the GPS becomes invalid (see above) save the value of millis() for use later as the start time and set a boolean, let's name it validGPS, to false. Later in loop(), if validGPS is false and millis() - start time is greater than or equal to your required "delay" in milliseconds then send the bytes to switch to GLONASS and set validGPS to true. Update the LCD at appropriate points in the process so that you can follow what is going on. Serial prints of progress and variable values at significant points will aid debugging

Can you explain in form of code. I am new to all this. If you can explain with code placement at said points it will be better understandable :frowning:

You will learn more if you write it yourself

Start by detecting when the GPS signal becomes invalid and print a message when it occurs. Post your best effort here even if it will not compile or does not do what you want

arooj16088002:
Can you explain in form of code. I am new to all this. If you can explain with code placement at said points it will be better understandable :frowning:

Okay, but did you actually "take a look at the StateChangeDetection example in the IDE to see the principle in action"?

I have seen that code but how to amend that according to my situation thats a difficult task for me thats why I was saying if you could tell me in form of code.

if (token[0] == 'V' && previousValidFlag != 'V')
{
  //code here to respond to GPS data becoming invalid
}