Question

Sorry in advance for what I feel is going to be a really simple problem, but I've been stuck on it for hours and I haven't been able to work out how to fix it based on the stuff I've found here or on google.

I've got an arduino hooked up to a GPS and a radio and am trying to broadcast the GPS signal to my radio. What I am trying to do now is get the NMEA sentence from the GPS into the variable 'text', but am getting confused by this error, which I think is due to arrays.

My error is occurring on this line: sprintf(text, char(c));

I've tried a few different things but this is where I'm stuck at the moment. Any help would be really appreciated.

#define RADIOPIN 13
#include <string.h>
#include <util/crc16.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 2);

#define GPSECHO  true

Adafruit_GPS GPS(&mySerial);

char datastring[80];
char text[80];

void setup() {
  Serial.begin(115200);
  GPS.begin(9600);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_5HZ);
  delay(3000);  
  pinMode(RADIOPIN,OUTPUT);
}

void loop(){
  gpscheck();
}

void gpscheck(){
    char c = GPS.read();
    if (c) {
//      Serial.print(c); 
    sprintf(text, char*(c));
    Serial.print(text);
    }
}
Was it helpful?

Solution

You might want to read a reference of sprintf. Then you will see that the second argument is a string.

So the following would be fine:

sprintf(text, "%c", c);

To be on the safe side, you might want to use snprintf instead, to lessesn the risk of buffer overflows.

Of course, for a single character, you might as well do e.g.

text[0] = c;
text[1] = '\0';  /* Terminate string */

OTHER TIPS

sprintf(text, char*(c));

If you want to print character

simply use printf("%c",c);

if you want to copy that into text

sprintf(text,"%c", c);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top