0

I got an Arduino Uno to play around with, and started to actually dive in. During a program that I am writing for a friend, it suddenly stopped outputting to the serial monitor. When I comment out the part I added (the for loop for changing the names), it still doesn't respond.

#include <TrueRandom.h>
#include <EEPROM.h>
#define numPokemon 151
String 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;
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");
  }
  for (int i = 0 ; i < 152 ; i++){
      if (currentID = i){
        pokemonName = nameArray[i];
        Serial.print(pokemonName);
        break;
      }
   }
  //nameChange(currentID);
  //Serial.println(pokemonName);
}
void loop(){

}
void nameChange(int currentID){
   for (int i = 0 ; i < 152 ; i++){
      if (currentID = i){
        pokemonName = nameArray[i];
        break;
      }
   }
}

What is the problem with this code?

  • Some of the problems include (as mentioned elsewhere) some simple bugs like using the assignment versus equality operator, and treating an Arduino Uno like a full-blown computer with gobs of memory. Funny thing is, had you done this with an Arduino Mega 2560, it would probably have worked for a longer period of time. – WineSoaked Mar 16 '14 at 20:03
2

The primary reason that this isn't working is that you are using more RAM than the ATmega328 has (2Kbytes).

The array nameArray is huge. Each one of those characters is 1 byte, it's ~1.1k in itself. Add in Serial, TrueRandom and String, and you are over the limit. It could be you are right on the bounds and calling a single function pushes the stack over it's limit.

To save size, you can place the strings in flash. There is functionality to do this in Arduino, namely PROGMEM directive from underlying AVR-GCC and F() from Arduino. Making arrays of string is quite painful though, and using the Flash library helps here.

Personally I would avoid String for anything like this. The insides of String use dynamic memory allocation and when you are near the limits of RAM, it really isn't helpful. You lose a lot of ease of use by moving to arrays of char though.

You are also using assignment instead of comparison in your code:

if (currentID = i){
   pokemonName = nameArray[i];
   Serial.print(pokemonName);
   break;
}

This needs to be currentID == i.

I'm not confident what you are doing here though. If you want to get the i th element from the array, then just do

pokemonName = nameArray[currentID]

You also have an odd conditional at the beginning where you check for an EEPROM value being non zero, and then the else reads out the EEPROM value which by definition must be zero.

Also, you have a define for numPokemon but you aren't using it for the bounds on your for loops.

| improve this answer | |
1

You have mixed up your comparison and assignment operators in two instances:

First is towards the end of setup.

  for (int i = 0 ; i < 152 ; i++){
      //if (currentID = i){ //This is assignment, not comparison
      //This is a comparison operation
        if (currentID == i){
        pokemonName = nameArray[i];
        Serial.print(pokemonName);
        break;
      }
   }

Second, is in the function definition of nameChange.

void nameChange(int currentID){
   for (int i = 0 ; i < 152 ; i++){
      //if (currentID = i){ //This is an assignment operation, not comparison.
      //This is a comparison operation
        if (currentID == i){
        pokemonName = nameArray[i];
        break;
      }
   }

Also, you have defined a constant numPokemon but are not using it at any point.

| improve this answer | |
0

the string array is too big for ram, place it in flash with the fucntion F()

Serial.println(F("This string will be stored in flash memory"));

also a detailed example with array with explanation cn be found here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy