Storing Lotta Strings in an Arduino

Hello folks,

I bought myself an Adafruit Printer and cut a little desktop case for it. For the record, it is a wicked-fun little gadget. 

I want to have the unit spit-out fortune cookies after a button press. I have found a website with a large list of fortunes to use . This website has the fortunes in a nice bullet-point format and when I cut (including the bullet "dot") and then paste each fortune into a "print command" the bullet is ignored and not printed. The text printed looks great. Basically, it looks as if I could cut and paste the whole darn list without having to manually "remove" the bullets by copying each line individually.

This "whole list" could then be dumped in a text file, or the like, and the Arduino could simply look for CR's etc. to go line-by-line as to pull up just one line at a time, thus one fortune at a time. We shove this text into a string and send it off to the printer.



The most obvious answer is indeed to use a text file and put the data on an SD card. Its pretty easy to read from such a card, and it might be nice to remove the card to update new/more fortunes.  That said, my code is so small to run the printer, I have almost 30,000 bytes left of room on the Arduino. --If I wanted to skip the SD card and throw all of this text directly on the chip, what storage/ retrival methods do you think would be best? (In the past, for similar things, I have used "progmem", but this would not allow me to cut and paste the "whole list" at once.

Any thoughts?

That’s just what I needed, sir

Thanks, man. I didn’t know/ think of the fact that I could dump the whole thing into one big string via program space. I use “progmem” when ever I I do say, scrolling menus on an LCD. Piece of cake.

Looks like this is going to be as easy as looking for a zero-termination or a CR/LF within the text. Awesome Possum.


Alright boys, here’s the after-action report…

I started with bdk’s suggestion and ran the code using one giant string containing all the text. As it turns out, each fortune ends in a period so it was pretty easy to parse strings. The issue however, was the fact that the entire chunk of text had to be run-through each time I wanted something, I.e. if I wanted the very last fortune, the sketch had to find 285 periods before it found the last fortune (#286). I started thinking about ways I could conserve some “thinking space/ power”…

In the end, I wrote a quick Processing sketch to divide everything up for me. I dropped all the text into a .txt doc and had Processing do the following:

  • Pull text from the .txt file
  • Parse Data (Split up all the fortunes)
  • Add  <char string_X[]  PROGMEM =">  to the front of each fortune (where X was increased with each fortune)
  • Add <";> to the end of each fortune
  • Print these new strings in the little window at the bottom of the IDE

From there, I simply had to cut and paste into my Arduino sketch. --Automatic code writing!!

At this point, I am simply keeping 286 strings in an array (via the progMEM command) and well, Bob’s your uncle. Now that I don’t have to parse on-the-fly and everything is in a nice, simple array, I can run code as easy as just using a “random” command and then “print string”. Easy peasy.


Thanks guys, I will get the final project posted soon.

off topic: source of fortune

Every *nix system has a fortune command, or can have one after installing a single package fom the distro.
This goes back decades and ties in tot the whole Unix subculture. Typical use was to print a fortune cookie every time a user logs on.

I just checked Ubuntu on my lappie here: I needed to install “fortune-mod”.
rik@riktop:~$ fortune
Your true value depends entirely on what you are compared with.

The fortunes are not all as compact as you will need them to be. Some are a dozen lines or more.
The program retrieves wisdom from plain textfiles in /usr/share/games/fortunes/. There appear to be categories “fortunes”, “literature” and “riddles”. A bit of preprocessing in your lingo of choice will weed out the long ones.