Go Down

Topic: Running out of Memory? Else If alternative? Switch on String? (Read 532 times) previous topic - next topic

PBear

I'm using a teensy 2.0 with SD adapter and I'm not a programmer but I made a joke keyboard that switches out certain typed words. For example if they type "monkey" it replaces it with "andrew". Right now I am using a string to capture the last 20 characters typed, then I have a big else if to check for certain keywords using endsWith("keyword"). This approach works great for upto 40 keywords then it stops working, I assume its the memory getting filled from the giant else if (using 85% of storage). I read switches are more efficient but it only uses single chars/integers. Is there a way to switch on a string or have another idea to solve this?  Thanks.

Here is small part of the else if so you get the idea, I call files so I can change the words without messing with uploading another sketch, I can just pop out the SD card and change the words, but the keywords stay the same.

Code: [Select]

//monkey
  if (lasttwenletters.endsWith("monkey")) {
    TypeOutFile("MONKEY.TXT"); //types out the file to keyboard
    lasttenletters[19]='*'; //change character
  }
  //there
  else if (lasttwenletters.endsWith("there")) {
    TypeOutFile("THERE.TXT");
    lasttenletters[19]='*'; //change character
  }
  //ask
  else if (lasttwenletters.endsWith("ask")) {
    TypeOutFile("ASK.TXT");
    lasttenletters[19]='*'; //change character
  }

GoForSmoke

You might want to step back and redefine your task.. and quit using String objects on MCU's and learn to use char array strings.

You have a list of words to find and other words to replace them with and a keyboard?

You could keep the word lists (match words alpha-sorted) in PROGMEM and try and find a match based on each character as it comes in. You could probably table 20k or more in flash.

And you have to buffer the input to allow it to be replaced.

Where does the output go? What does the typist see?

You have SD. Try reading 1 file on SD and write another file with replacement words instead.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

PBear

Thanks for the response, I am going to mess with char arrays and see if I can make my giant else if work.

GoForSmoke

The giant conditional is wasted effort, but at least see if you can restrict the match/replace words to flash with the F macro that I don't use because I use tables instead. Your code as now fills RAM with match words already stored in flash.

You can loop through a word list and achieve the same result with far less code than the extreme hard-coding of the giant-if-else. That code would be able to run different lists without code change.

Keep the list on SD and it can be very large. SD has limited re-write so take care not to treat it like a PC HD. It's better to write a whole edited copy of the file and SD should have the room.

You can make and maintain lists on SD with a PC (or phone I'm sure) to use on Arduino.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Go Up