Arduino include problems

What exactly is your project, what hardware are you trying to use?

Unless we know your goals and aims, we cannot give sound enough advice.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

Hi T,

As mentioned earlier, there are 3x Heltec v3 modules (radio transceivers) plus 2x gps modules.

In this first case1x one be carried when out 'getting lost' or living dangerously, and if stuck, switch it on, and the home device would know where you are and where to send the helicopter.

C

Deleted

Please make it easy to provide help without the need to download the code by posting it here in the forum, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post full error messages in code tags as it makes it easier to scroll through them and copy them for examination

To post the error message, click the "COPY ERROR MESSAGES" button in the IDE, paste the copied messages in a reply here, select all of it and click the < CODE > icon above the reply editor to add the code tags and then save the reply

Hi B,

Is this correct?

#include <heltec_unofficial.h>

#include <TinyGPSPlus.h>

// ------------------ IDENTIFIERS ------------------

#define NODE_ID "BASE"

#define PEER_ID "REMOTE"

// ------------------ FREQUENCIES ------------------

#define TX_FREQUENCY 863.8 // BASE β†’ REPEATER

#define RX_FREQUENCY 865.8 // REPEATER β†’ BASE

// ------------------ RADIO SETTINGS ------------------

#define BANDWIDTH 125

#define SPREADING_FACTOR 9

#define TRANSMIT_POWER 0

// ------------------ TIMING ------------------

#define GPS_UPDATE_MS 2000

#define GPS_TIMEOUT_MS 30000

// ------------------ GPS POWER ------------------

#define VGNSS_CTRL 3

TinyGPSPlus gps;

HardwareSerial GPSSerial(1); // GPS UART

HardwareSerial DebugSerial(2); // UART2 β†’ pin 47 TX

volatile bool rxFlag = false;

String rxData, txData, lastRxMsg;

enum ScreenMode { SCREEN_OWN,

              SCREEN_RX };

ScreenMode screenMode = SCREEN_OWN;

uint32_t lastValidFix = 0;

uint32_t lastOwnDraw = 0;

// ------------------ HELPERS ------------------

String fmtDate() {

if (!gps.date.isValid()) return "NA";

char b[11];

sprintf(b, "%02d-%02d-%04d", gps.date.day(), gps.date.month(), gps.date.year());

return String(b);

}

String fmtTime() {

if (!gps.time.isValid()) return "NA";

char b[9];

sprintf(b, "%02d:%02d:%02d", gps.time.hour(), gps.time.minute(), gps.time.second());

return String(b);

}

String buildOwnGps() {

bool fresh = gps.location.isValid() && (millis() - lastValidFix < GPS_TIMEOUT_MS);

if (!fresh) return String(NODE_ID) + ",NOFIX";

return String(NODE_ID) + "," + String(gps.location.lat(), 6) + "," + String(gps.location.lng(), 6) + "," + fmtDate() + "," + fmtTime();

}

bool parseGps(const String& msg, float& lat, float& lon, String& date, String& time) {

int c1 = msg.indexOf(',');

int c2 = msg.indexOf(',', c1 + 1);

int c3 = msg.indexOf(',', c2 + 1);

int c4 = msg.indexOf(',', c3 + 1);

if (c1 > 0 && c2 > c1 && c3 > c2 && c4 > c3) {

lat = msg.substring(c1 + 1, c2).toFloat();

lon = msg.substring(c2 + 1, c3).toFloat();

date = msg.substring(c3 + 1, c4);

time = msg.substring(c4 + 1);

return true;

}

return false;

}

// ------------------ DISPLAY ------------------

void showOwn() {

display.clear();

display.drawString(0, 0, "BASE OWN");

bool fresh = gps.location.isValid() && (millis() - lastValidFix < GPS_TIMEOUT_MS);

if (fresh) {

display.drawString(0, 10, "LAT: " + String(gps.location.lat(), 6));

display.drawString(0, 20, "LON: " + String(gps.location.lng(), 6));

display.drawString(0, 30, "DATE: " + fmtDate());

display.drawString(0, 40, "TIME: " + fmtTime());

} else {

display.drawString(0, 10, "NO GPS FIX");

}

display.display();

}

void showRemote(const String& msg) {

display.clear();

display.drawString(0, 0, "REMOTE DATA");

if (msg == "") {

display.drawString(0, 10, "NO RX YET");

} else {

float lat, lon;

String date, time;



if (parseGps(msg, lat, lon, date, time)) {

  display.drawString(0, 10, "LAT: " + String(lat, 6));

  display.drawString(0, 20, "LON: " + String(lon, 6));

  display.drawString(0, 30, "DATE: " + date);

  display.drawString(0, 40, "TIME: " + time);

} else {

  display.drawString(0, 10, "PARSE ERROR");

}

}

display.display();

}

// ------------------ RADIO ------------------

void rxISR() {

rxFlag = true;

}

void setRadioTx() {

radio.standby();

RADIOLIB_OR_HALT(radio.setFrequency(TX_FREQUENCY));

}

void setRadioRx() {

radio.standby();

RADIOLIB_OR_HALT(radio.setFrequency(RX_FREQUENCY));

delay(10);

RADIOLIB_OR_HALT(radio.startReceive(RADIOLIB_SX126X_RX_TIMEOUT_INF));

}

// ------------------ SETUP ------------------

void setup() {

heltec_setup();

Serial.begin(9600); // USB output

// ---- UART2 β†’ pin 47 TX ----

DebugSerial.begin(9600, SERIAL_8N1, -1, 47);

pinMode(VGNSS_CTRL, OUTPUT);

digitalWrite(VGNSS_CTRL, HIGH);

GPSSerial.begin(38400, SERIAL_8N1, 33, 34);

RADIOLIB_OR_HALT(radio.begin());

radio.setDio1Action(rxISR);

RADIOLIB_OR_HALT(radio.setBandwidth(BANDWIDTH));

RADIOLIB_OR_HALT(radio.setSpreadingFactor(SPREADING_FACTOR));

RADIOLIB_OR_HALT(radio.setOutputPower((int8_t)TRANSMIT_POWER));

setRadioRx();

lastValidFix = millis();

screenMode = SCREEN_OWN;

showOwn();

}

// ------------------ LOOP ------------------

void loop() {

heltec_loop();

// ---- GPS ----

while (GPSSerial.available()) {

gps.encode(GPSSerial.read());

if (gps.location.isValid()) {

  lastValidFix = millis();

  if (screenMode == SCREEN_OWN && millis() - lastOwnDraw >= GPS_UPDATE_MS) {

    showOwn();

    lastOwnDraw = millis();

  }

}

}

uint32_t nowMs = millis();

uint32_t nowSec = nowMs / 1000;

uint16_t frame30 = nowSec % 30;

uint16_t frame15 = nowSec % 15;

// ---- SCREEN MODE ----

if (frame30 < 15 && screenMode != SCREEN_OWN) {

screenMode = SCREEN_OWN;

showOwn();

}

if (frame30 >= 15 && screenMode != SCREEN_RX) {

screenMode = SCREEN_RX;

showRemote(lastRxMsg);

}

// ---- TX SLOTS ----

static uint32_t lastTick = 0;

if (nowSec != lastTick) {

lastTick = nowSec;



bool shouldTx =

  (frame30 < 15) && (frame15 == 0 || frame15 == 3 || frame15 == 6 || frame15 == 9 || frame15 == 12);



if (shouldTx) {

  txData = buildOwnGps();



  Serial.print("TX: ");

  Serial.println(txData);



  // ---- PARSED BASE GPS OUT PIN 47 ----

  float lat, lon;

  String date, time;

  if (parseGps(txData, lat, lon, date, time)) {

    DebugSerial.print("BASE_PARSED,");

    DebugSerial.print(lat, 6);

    DebugSerial.print(",");

    DebugSerial.print(lon, 6);

    DebugSerial.print(",");

    DebugSerial.print(date);

    DebugSerial.print(",");

    DebugSerial.println(time);

  }



  setRadioTx();

  radio.transmit(txData.c_str());

  radio.finishTransmit();

  setRadioRx();

}

}

// ---- RX ----

if (rxFlag) {

rxFlag = false;

rxData = "";



RADIOLIB(radio.readData(rxData));



if (\_radiolib_status == RADIOLIB_ERR_NONE && rxData.startsWith(PEER_ID)) {



  Serial.print("RX: ");

  Serial.println(rxData);



  lastRxMsg = rxData;



  // ---- PARSED REMOTE GPS OUT PIN 47 ----

  float lat, lon;

  String date, time;

  if (parseGps(rxData, lat, lon, date, time)) {

    DebugSerial.print("REMOTE_PARSED,");

    DebugSerial.print(lat, 6);

    DebugSerial.print(",");

    DebugSerial.print(lon, 6);

    DebugSerial.print(",");

    DebugSerial.print(date);

    DebugSerial.print(",");

    DebugSerial.println(time);

  }



  if (screenMode == SCREEN_RX) {

    showRemote(lastRxMsg);

  }

}



setRadioRx();

}

}

C

No

You did not follow the instructions in my previous post

Hi B,

I did the Tools/Auto Format, then Edit/ copy, but there was nothing on the clipboard, so I selected all before edit/copy.

C

As I said, you did not follow my instructions


```cpp

      setRadioRx();
```

Tell me if I get it right:


```cpp
#include <heltec_unofficial.h>
#include <TinyGPSPlus.h>

// ------------------ IDENTIFIERS ------------------
#define NODE_ID "BASE"
#define PEER_ID "REMOTE"

// ------------------ FREQUENCIES ------------------
#define TX_FREQUENCY 863.8  // BASE β†’ REPEATER
#define RX_FREQUENCY 865.8  // REPEATER β†’ BASE

// ------------------ RADIO SETTINGS ------------------
#define BANDWIDTH 125
#define SPREADING_FACTOR 9
#define TRANSMIT_POWER 0

// ------------------ TIMING ------------------
#define GPS_UPDATE_MS 2000
#define GPS_TIMEOUT_MS 30000

// ------------------ GPS POWER ------------------
#define VGNSS_CTRL 3

TinyGPSPlus gps;
HardwareSerial GPSSerial(1);    // GPS UART
HardwareSerial DebugSerial(2);  // UART2 β†’ pin 47 TX

volatile bool rxFlag = false;
String rxData, txData, lastRxMsg;

enum ScreenMode { SCREEN_OWN,
                  SCREEN_RX };
ScreenMode screenMode = SCREEN_OWN;

uint32_t lastValidFix = 0;
uint32_t lastOwnDraw = 0;

// ------------------ HELPERS ------------------

String fmtDate() {
  if (!gps.date.isValid()) return "NA";
  char b[11];
  sprintf(b, "%02d-%02d-%04d", gps.date.day(), gps.date.month(), gps.date.year());
  return String(b);
}

String fmtTime() {
  if (!gps.time.isValid()) return "NA";
  char b[9];
  sprintf(b, "%02d:%02d:%02d", gps.time.hour(), gps.time.minute(), gps.time.second());
  return String(b);
}

String buildOwnGps() {
  bool fresh = gps.location.isValid() && (millis() - lastValidFix < GPS_TIMEOUT_MS);
  if (!fresh) return String(NODE_ID) + ",NOFIX";

  return String(NODE_ID) + "," + String(gps.location.lat(), 6) + "," + String(gps.location.lng(), 6) + "," + fmtDate() + "," + fmtTime();
}

bool parseGps(const String& msg, float& lat, float& lon, String& date, String& time) {
  int c1 = msg.indexOf(',');
  int c2 = msg.indexOf(',', c1 + 1);
  int c3 = msg.indexOf(',', c2 + 1);
  int c4 = msg.indexOf(',', c3 + 1);

  if (c1 > 0 && c2 > c1 && c3 > c2 && c4 > c3) {
    lat = msg.substring(c1 + 1, c2).toFloat();
    lon = msg.substring(c2 + 1, c3).toFloat();
    date = msg.substring(c3 + 1, c4);
    time = msg.substring(c4 + 1);
    return true;
  }
  return false;
}

// ------------------ DISPLAY ------------------

void showOwn() {
  display.clear();
  display.drawString(0, 0, "BASE OWN");

  bool fresh = gps.location.isValid() && (millis() - lastValidFix < GPS_TIMEOUT_MS);
  if (fresh) {
    display.drawString(0, 10, "LAT: " + String(gps.location.lat(), 6));
    display.drawString(0, 20, "LON: " + String(gps.location.lng(), 6));
    display.drawString(0, 30, "DATE: " + fmtDate());
    display.drawString(0, 40, "TIME: " + fmtTime());
  } else {
    display.drawString(0, 10, "NO GPS FIX");
  }

  display.display();
}

void showRemote(const String& msg) {
  display.clear();
  display.drawString(0, 0, "REMOTE DATA");

  if (msg == "") {
    display.drawString(0, 10, "NO RX YET");
  } else {
    float lat, lon;
    String date, time;

    if (parseGps(msg, lat, lon, date, time)) {
      display.drawString(0, 10, "LAT: " + String(lat, 6));
      display.drawString(0, 20, "LON: " + String(lon, 6));
      display.drawString(0, 30, "DATE: " + date);
      display.drawString(0, 40, "TIME: " + time);
    } else {
      display.drawString(0, 10, "PARSE ERROR");
    }
  }

  display.display();
}

// ------------------ RADIO ------------------

void rxISR() {
  rxFlag = true;
}

void setRadioTx() {
  radio.standby();
  RADIOLIB_OR_HALT(radio.setFrequency(TX_FREQUENCY));
}

void setRadioRx() {
  radio.standby();
  RADIOLIB_OR_HALT(radio.setFrequency(RX_FREQUENCY));
  delay(10);
  RADIOLIB_OR_HALT(radio.startReceive(RADIOLIB_SX126X_RX_TIMEOUT_INF));
}

// ------------------ SETUP ------------------

void setup() {
  heltec_setup();

  Serial.begin(9600);  // USB output

  // ---- UART2 β†’ pin 47 TX ----
  DebugSerial.begin(9600, SERIAL_8N1, -1, 47);

  pinMode(VGNSS_CTRL, OUTPUT);
  digitalWrite(VGNSS_CTRL, HIGH);

  GPSSerial.begin(38400, SERIAL_8N1, 33, 34);

  RADIOLIB_OR_HALT(radio.begin());
  radio.setDio1Action(rxISR);
  RADIOLIB_OR_HALT(radio.setBandwidth(BANDWIDTH));
  RADIOLIB_OR_HALT(radio.setSpreadingFactor(SPREADING_FACTOR));
  RADIOLIB_OR_HALT(radio.setOutputPower((int8_t)TRANSMIT_POWER));

  setRadioRx();

  lastValidFix = millis();
  screenMode = SCREEN_OWN;
  showOwn();
}

// ------------------ LOOP ------------------

void loop() {
  heltec_loop();

  // ---- GPS ----
  while (GPSSerial.available()) {
    gps.encode(GPSSerial.read());
    if (gps.location.isValid()) {
      lastValidFix = millis();
      if (screenMode == SCREEN_OWN && millis() - lastOwnDraw >= GPS_UPDATE_MS) {
        showOwn();
        lastOwnDraw = millis();
      }
    }
  }

  uint32_t nowMs = millis();
  uint32_t nowSec = nowMs / 1000;
  uint16_t frame30 = nowSec % 30;
  uint16_t frame15 = nowSec % 15;

  // ---- SCREEN MODE ----
  if (frame30 < 15 && screenMode != SCREEN_OWN) {
    screenMode = SCREEN_OWN;
    showOwn();
  }
  if (frame30 >= 15 && screenMode != SCREEN_RX) {
    screenMode = SCREEN_RX;
    showRemote(lastRxMsg);
  }

  // ---- TX SLOTS ----
  static uint32_t lastTick = 0;

  if (nowSec != lastTick) {
    lastTick = nowSec;

    bool shouldTx =
      (frame30 < 15) && (frame15 == 0 || frame15 == 3 || frame15 == 6 || frame15 == 9 || frame15 == 12);

    if (shouldTx) {
      txData = buildOwnGps();

      Serial.print("TX: ");
      Serial.println(txData);

      // ---- PARSED BASE GPS OUT PIN 47 ----
      float lat, lon;
      String date, time;
      if (parseGps(txData, lat, lon, date, time)) {
        DebugSerial.print("BASE_PARSED,");
        DebugSerial.print(lat, 6);
        DebugSerial.print(",");
        DebugSerial.print(lon, 6);
        DebugSerial.print(",");
        DebugSerial.print(date);
        DebugSerial.print(",");
        DebugSerial.println(time);
      }

      setRadioTx();
      radio.transmit(txData.c_str());
      radio.finishTransmit();
      setRadioRx();
    }
  }

  // ---- RX ----
  if (rxFlag) {
    rxFlag = false;
    rxData = "";

    RADIOLIB(radio.readData(rxData));

    if (_radiolib_status == RADIOLIB_ERR_NONE && rxData.startsWith(PEER_ID)) {

      Serial.print("RX: ");
      Serial.println(rxData);

      lastRxMsg = rxData;

      // ---- PARSED REMOTE GPS OUT PIN 47 ----
      float lat, lon;
      String date, time;
      if (parseGps(rxData, lat, lon, date, time)) {
        DebugSerial.print("REMOTE_PARSED,");
        DebugSerial.print(lat, 6);
        DebugSerial.print(",");
        DebugSerial.print(lon, 6);
        DebugSerial.print(",");
        DebugSerial.print(date);
        DebugSerial.print(",");
        DebugSerial.println(time);
      }

      if (screenMode == SCREEN_RX) {
        showRemote(lastRxMsg);
      }
    }

    setRadioRx();
  }
}
```

I think it's very naive to expect AI to write something like this, if you don't understand the program yourself.
And I certainly wouldn't trust such a system to save me in an emergency.

Hi B,

I am unable to code, and I've been trying since my sons 1982 spectrum.

AI wrote all of those 3x codes, and with time they'll work, and I won't know how. Brilliant isn't it?

C

Nearly. It looks like you followed my instructions, which added the code tags, then you added them yourself when posting the code, which you don't need to do, but al least it is readable

If you don't know how it work - how you can trust it?

Hi B,

I can test it with the onboard screens, an SDR program, plus a serial readout.

Some of us have to get round our difficulties and carry on.

C

Hi B,

Let me know how I added them myself, and I'll stop doing it.

C

If you followed my instructions then the code tags would not be visible inside the code tags of post #30

I can't help feeling that you are trolling here

Hi B,

I'm slightly dyslexic, and earlier, I said, only reply if you have a patient disposition, so I'm working as hard as I can, I assure you.

Your instructions were: Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply.

I added CTRL+A to select it, was this wrong?

C

Delted

No problem using CTRL+A but you don't actually need to do it if you use Copy for Forum as it assumes that you want to copy the entire sketch if nothing is selected, or whatever you have selected if you only want to copy a portion of it