0

I'm using a PROGMEM char* to store a massive array. When I try to print a certain part of the array, the message comes out mangled. How do I fix this?

The output should be "Poliwag", but it comes out as "arow".

#include <TrueRandom.h>
#include <EEPROM.h>
#define numPokemon 151
PROGMEM char* nameArray[] = {"MISSINGNO","Bulbasaur","Ivysaur","Venusaur","Charmander","Charmeleon","Charizard","Squirtle",
"Wartortle","Blastoise","Caterpie","Metapod","Butterfree","Weedle","Kakuna","Beedrill","Pidgey","Pidgeotto",
"Pidgeot","Rattata","Raticate","Spearow","Fearow","Ekans","Arbok","Pikachu","Raichu","Sandshrew","Sandslash",
"Nidoran♀","Nidorina","Nidoqueen","Nidoran♂","Nidorino","Nidoking","Clefairy","Clefable","Vulpix","Ninetales","Jigglypuff",
"Wigglytuff","Zubat","Golbat","Oddish","Gloom","Vileplume","Paras","Parasect","Venonat","Venomoth","Diglett","Dugtrio","Meowth",
"Persian","Psyduck","Golduck","Mankey","Primeape","Growlithe","Arcanine","Poliwag","Poliwhirl","Poliwrath","Abra","Kadabra","Alakazam",
"Machop","Machoke","Machamp","Bellsprout","Weepinbell","Victreebel","Tentacool","Tentacruel","Geodude","Graveler","Golem","Ponyta","Rapidash",
"Slowpoke","Slowbro","Magnemite","Magneton","Farfetch`d","Doduo","Dodrio","Seel","Dewgong","Grimer","Muk","Shellder","Cloyster","Gastly","Haunter",
"Gengar","Onix","Drowzee","Hypno","Krabby","Kingler","Voltorb","Electrode","Exeggcute","Exeggutor","Cubone","Marowak","Hitmonlee","Hitmonchan","Lickitung",
"Koffing","Weezing","Rhyhorn","Rhydon","Chansey","Tangela","Kangaskhan","Horsea","Seadra","Goldeen","Seaking","Staryu","Starmie","Mr. Mime","Scyther","Jynx",
"Electabuzz","Magmar","Pinsir","Tauros","Magikarp","Gyarados","Lapras","Ditto","Eevee","Vaporeon","Jolteon","Flareon","Porygon","Omanyte","Omastar","Kabuto","Kabutops",
"Aerodactyl","Snorlax","Articuno","Zapdos","Moltres","Dratini","Dragonair","Dragonite","Mewtwo","Mew"
};
int a = 0;
int b = 0;
int value;
String pokemonName = "Missingno";
int currentID = TrueRandom.random(1,numPokemon+1);
// a = CurrentID
void setup(){
 Serial.begin(9600);
 if (EEPROM.read(a)!=0){
 currentID = EEPROM.read(a);
 Serial.print("The Stored ID is ");
 Serial.println(EEPROM.read(a));
 Serial.println("Stored = true");
 }else{
 value = EEPROM.read(a);
 Serial.println(value);
 EEPROM.write(a,currentID);
 value = EEPROM.read(a);
 Serial.println(value);
 Serial.println("Stored = false");
 }
 pokemonName = nameArray[currentID];
 Serial.println(pokemonName);
}
void loop(){
}
jfpoilpret
9,1627 gold badges38 silver badges54 bronze badges
asked Mar 16, 2014 at 18:30

1 Answer 1

5

The way you've written in, the pointer is stored in flash, but the strings are stored in SRAM. Read the appropriate documentation (also found in the Arduino reference) more closely, and do as it says.

char nmPokemon0[] PROGMEM = "MISSINGNO";
char nmPokemon1[] PROGMEM = "Bulbasaur";
 ...
PGM_P nameArray[] PROGMEM =
{
 nmPokemon0, nmPokemon1, ...
};

And don't forget to copy the string to SRAM as shown further down before outputting it, since Serial.println() can't work directly with flash.

answered Mar 16, 2014 at 18:36
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.