Go Down

Topic: SOLVED: conversion from '__FlashStringHelper*' to non-scalar type 'String' (Read 2573 times) previous topic - next topic

AgeingHippy

Hi All

I am having trouble using strings and F() and PROGMEM etc. Probably from a shortage of knowledge...

The call to debugWrite below gives me the following error
error: conversion from '__FlashStringHelper*' to non-scalar type 'String' requested

Code: [Select]

void someMethod() {
 ...doStuff...
 debugWrite(F("BotTesting v1.0 29/04/2012"));
 ..doMoreStuff
}

void debugWrite(String data) {
 if ( debugWriteFlag == 1 || debugWriteFlag == 3 ) {
   lcd.print(data);
 }
 if ( debugWriteFlag == 2 || debugWriteFlag == 3 ) {
   Serial.println(data);
 }
}



Please help. I wish to have flexibility in whether I write to Serial or my onboard LCD display.

Thanks

johnwasser

F() only works for functions that take a PROGMEM pointer of type __FlashStringHelper.  Serial.print() is such a function.  Apparently debugWrite() isn't.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

AgeingHippy

Hi John.

I was hoping to get an answer as what to actually do to sort out my problem, but after some searching I found example code suggesting I write an overloaded procedure for debugWrite. So now I have debugWrite defined twice, with different parameter types.

Code: [Select]

void debugWrite(String data) {
  if ( debugWriteFlag == 1 || debugWriteFlag == 3 ) {
    lcd.print(data);
  }
  if ( debugWriteFlag == 2 || debugWriteFlag == 3 ) {
    Serial.println(data);
  }
}

void debugWrite( __FlashStringHelper* data) {
  if ( debugWriteFlag == 1 || debugWriteFlag == 3 ) {
    lcd.print(data);
  }
  if ( debugWriteFlag == 2 || debugWriteFlag == 3 ) {
    Serial.println(data);
  }
}


So both the following 2 calls will work
Code: [Select]

debugWrite(F("PROGMEM string");
debugWrite("another RAM string");


For anyone that would like to give a helpful explanation for a dabbler, I found the code would not compile until I placed the asterisk  at the end of the __FlashStringHelper type declaration as in __FlashStringHelper*

Ekkehard

The explanation is simple:

Here is the definition of the macro F():
#define F(string_literal) (reinterpret_cast<__FlashStringHelper*>(PSTR(string_literal)))

This macro gives you a pointer to the class __FlashStringHelper, so that is what your code needs to accept as a formal parameter.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy