i suck at arduino code

519 views
Skip to first unread message

kr...@sleepingplanet.com

unread,
Oct 26, 2015, 3:02:55 AM10/26/15
to Sydney Hackspace

ok,

i have an arduino sketch that combine the one wire library, the dallas temperature monitor library and u8graphics library.

 

i want the oled screen to show the temperature in the kids room

this section of code:

void draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.drawStr( 0, 10, "inside");
u8g.drawStr( 0, 28, "outside");
u8g.drawStr( 0, 46, "kids");
u8g.drawStr( 0, 64, "time");
}
 
works fine, but when i begin to use a variable (that will have the actual temperature)
 
void draw(void) {
// graphic commands to redraw the complete screen should be placed here
String line1 = "inside";
u8g.drawStr( 0, 10, line1);
u8g.drawStr( 0, 28, "outside");
u8g.drawStr( 0, 46, "kids");
u8g.drawStr( 0, 64, "time");
}
 
i get the error
Arduino/libraries/U8glib/U8glib.h:201:16: note:   no known conversion for argument 3 from 'String' to 'const __FlashStringHelper*'
no matching function for call to 'U8GLIB_SSD1306_128X64::drawStr(int, int, String&)'
 
 
what i understand is the "inside" bit i assumed is a String but is in fact a FlashStringHelper? and the two arn't compatible
what i want is to create a string like this
 
String line1 = string("inside " + printTemperature(localThermometer)));
 

kr...@sleepingplanet.com

unread,
Oct 26, 2015, 3:27:01 AM10/26/15
to sydney-h...@googlegroups.com

alrigth i think i was getting confused about the string variable, apparently i should be using char

or calling the u8g.print function when i'm using strings

--
You received this message because you are subscribed to the Google Groups "Robots & Dinosaurs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sydney-hackspa...@googlegroups.com.
To post to this group, send email to sydney-h...@googlegroups.com.
Visit this group at http://groups.google.com/group/sydney-hackspace.
For more options, visit https://groups.google.com/d/optout.

Patrick Barnes

unread,
Oct 26, 2015, 4:34:22 AM10/26/15
to sydney-h...@googlegroups.com
Yeah, be aware of the big differences between a literal string and a string that you reference or build at run-time.

In many cases they're even stored in entirely different sections of memory (program memory vs ram)

Paul

unread,
Oct 26, 2015, 10:05:52 AM10/26/15
to Robots & Dinosaurs
Would line1.c_str() do the trick? It's hard to tell what is String in this context since you've omitted the includes, but chances are it's inherited from the STL string.
Anyway, there must be an accessor to pull the underlying char* representation.

Disclaimer: I last used real C++ about 6 years ago.

Paul

unread,
Oct 26, 2015, 10:09:22 AM10/26/15
to Robots & Dinosaurs
Ah, so arduino sdk has its own String, which in turn offers c_str(): https://www.arduino.cc/en/Reference/StringObject

Ada Lim

unread,
Oct 26, 2015, 10:55:28 AM10/26/15
to sydney-h...@googlegroups.com
On Mon, Oct 26, 2015 at 7:34 PM, Patrick Barnes <mrt...@gmail.com> wrote:
> Yeah, be aware of the big differences between a literal string and a string
> that you reference or build at run-time.
> In many cases they're even stored in entirely different sections of memory
> (program memory vs ram)

Also beware of the difference between a string and a String.

u8g's print understands the same things as the arduino print, viz.
String, char*, char/uchar, int/uint, long/ulong, and double. u8g's
drawStr only understands char* (which is what most C programmers refer
to as a string). so paul's suggestion of c_str should work.

also, can someone tell me why this bit of code is legal:

void print(long, int = DEC);
void print(unsigned long, int = DEC);
void print(double, int = 2);

I didn't think you were allowed to use keywords as parameter names.

(also, int = 2 is very disgusting. Arduino Quality Software.)

-A

Matt Callow

unread,
Oct 26, 2015, 12:18:58 PM10/26/15
to Robodino

I don't see any parameter names, only parameter types.

Matt

Ada Lim

unread,
Oct 26, 2015, 12:37:36 PM10/26/15
to sydney-h...@googlegroups.com
On Tue, Oct 27, 2015 at 3:18 AM, Matt Callow <matt....@gmail.com> wrote:
>> I didn't think you were allowed to use keywords as parameter names.
> I don't see any parameter names, only parameter types.

Right. I've been pythoning for too long.

-A

kr...@sleepingplanet.com

unread,
Oct 26, 2015, 5:34:10 PM10/26/15
to sydney-h...@googlegroups.com

thanks!

i ended up using u8g print function, which as you said is the same as serial.print and i already had that working so all was good.

personally, i always find it a bit depressing discovering how much I don't know about something (in this case programming).

it's great acheiving something and i should focus on the the small wins. but yeah, just adding a variable to a string to make a new string i didn't think would take me 2 hours.

Patrick Barnes

unread,
Oct 26, 2015, 10:06:54 PM10/26/15
to sydney-h...@googlegroups.com

On 27/10/2015 1:55 AM, Ada Lim wrote:
also, can someone tell me why this bit of code is legal:

    void print(long, int = DEC);
    void print(unsigned long, int = DEC);
    void print(double, int = 2);

I didn't think you were allowed to use keywords as parameter names.

(also, int = 2 is very disgusting.  Arduino Quality Software.)

-A

Those are function definitions / prototypes.

In C, the .h file just defines the parameter and return types, eg void print(long, int);
Then the .c (or .ino) file has the function definition eg void print(long a, int b) { .... }

In Arduino-flavoured C - which I think has a few C++ish things in it - the 'type = value' in the prototype defines a default value.
So you could call print(10) or print(10, DEC) and it'd do the same thing.
From the context, I'm guessing that's the base that print uses, decimal, octal etc.

Yes using 'int = 2' is pretty terrible.

Paul

unread,
Oct 26, 2015, 11:29:50 PM10/26/15
to Robots & Dinosaurs

Also beware of the difference between a string and a String.


As in "string" doesn't actually exist in the Arduino world and using it in context of types is pretty much always ambiguous?
Reply all
Reply to author
Forward
0 new messages