Hello,
I've defined a variable Host_001 in flash memory.
with the bool PING(Host_001); - I call the function "bool PING(char Host)"
but the function will not be executed (no print output)
Where is my mistake?
char Host[30];
bool internet = false;
const char Host_001[]PROGMEM = ("www.google.com");
int pingResult;
void setup() {
Serial.begin(115200);
while ((!Serial & millis()< 5000)) {
; // wait for serial port
}
bool PING(Host_001);
if (PING == true) {internet= true; Serial.println("o.k.");} else {internet= false;Serial.println("not o.k.");}
}
void loop(){
delay(20000);
}
//**************************************************
bool PING(char Host) {
pingResult = 50; // dummy result
if (pingResult >= 0) {
Serial.print(F("erfolgreich! RTT = "));
Serial.print(pingResult);
Serial.println(" ms");
return true;
} else {
Serial.print(F("FAILED! Error code: "));
Serial.println(pingResult);
return false;
}
}
I use a MKR1000.
while ((!Serial & millis()< 5000))
Should be &&
bool PING(Host_001);
You didn't say what your sketch did output, but I'm guessing "o.k.". You are using a (non-zero) pointer value to initialise a variable PING of type bool . The variable PING with therefore always be true .
something like bool mypingresult = PING(Host_001); might be closer to what you want.
bool PING(char Host) {
The parameter doesn't have the correct pointer type.
I not sure, how should I changed the sketch? Sorry.
I changed the sketch: e.g. I 've added a '&' etc.
char Host[30];
bool internet = false;
const char Host_001[]PROGMEM = ("www.google.com");
int pingResult; // RTT of the PING
void setup() {
Serial.begin(115200);
while ((!Serial && millis()< 5000)) {
; // wait for serial port to connect. Needed for native USB port only
}
bool mypingresult = PING(Host_001);
if (mypingresult) {internet= true; Serial.println("o.k.");} else {internet= false;Serial.println("not o.k.");}
}
void loop(){
delay(20000);
}
//***********************************************************************************************
bool PING(char Host) { //
pingResult = 50; // dummy result
if (pingResult >= 0) {
Serial.println(F("output 1"));
return true;
} else {
Serial.println(F("output 2"));
return false;
}
}
I ve got the following error messages:
error: invalid conversion from 'const char*' to 'char' [-fpermissive]
bool mypingresult = PING(Host_001);
initializing argument 1 of 'bool PING(char)' [-fpermissive]
bool PING(char Host) {
Correct pointer type?
system
April 18, 2018, 2:04pm
4
Correct pointer type?
You are passing a string in PROGMEM to a function that expects a char in SRAM. Why?
Hoffmakl:
I ve got the following error messages:
error: invalid conversion from 'const char*' to 'char' [-fpermissive]
bool mypingresult = PING(Host_001);
initializing argument 1 of 'bool PING(char)' [-fpermissive]
bool PING(char Host) {
Correct pointer type?
The clue is in the error message...
PaulS:
You are passing a string in PROGMEM to a function that expects a char in SRAM. Why?
If I define "char Host_001[]" and assign a string to Host_001, then Host_001 is a string
and an array with multiple characters.
conclusion: Host is a string.
Thank you - the function is excecuted.