String sentence to words in an array

I am creating a program which reads inputs from buttons and puts the input inside of a string called rawinput to be later processed. I have basically created my own keyboard of sort. This will make more sense when you read the code. For now, rather than proccess the string the code just prints it on the serial monitor. For some crazy reason, PIN 8 will not print the letter d. All it does is send "" to the serial monitor. It has clearly read the button, but for some reason won't transfer the letter d to the string rawinput! Help me! I'm desperate...

String rawinput = "";
String alpha[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

int alphaState[] = {};

void setup(){
  
Serial.begin(9600);

  pinMode(4,INPUT);
  pinMode(5,INPUT);
  pinMode(6,INPUT);
  pinMode(7,INPUT);
  pinMode(8,INPUT);
  pinMode(9,INPUT);
  pinMode(10,INPUT);
  pinMode(11,INPUT);
  pinMode(12,INPUT);
  pinMode(13,INPUT);
     
}

void loop(){

  for(int i = 4; i < 14 ;i++){
  alphaState[i] = digitalRead(i);
  }
  
  
  if (digitalRead(4) == HIGH){
    Serial.println(rawinput);
    delay(250);
    rawinput = "";  
  }
  
  if (digitalRead(5) == HIGH){
    rawinput += alpha[0];
    delay(250);
  }
  
  if (digitalRead(6) == HIGH){
    rawinput += alpha[1];
    delay(250);
  }
  
  if (digitalRead(7) == HIGH){
    rawinput += alpha[2];
    delay(250);
  }
  
  if (digitalRead(8) == HIGH){
    rawinput += alpha[3];
    delay(250);
  }
  
  if (digitalRead(9) == HIGH){
    rawinput += alpha[4];
    delay(250);
  }
  
  if (digitalRead(10) == HIGH){
    rawinput += alpha[5];
    delay(250);
  }
  
  if (digitalRead(11) == HIGH){
    rawinput += alpha[6];
    delay(250);
  }
  
  if (digitalRead(12) == HIGH){
    rawinput += alpha[7];
    delay(250);
  }
  
  if (digitalRead(13) == HIGH){
    rawinput += alpha[8];
    delay(250);
  }
  
}

Ah, and I know you will probably mention that could use a ton of for loops to condense my code, apparently for loops are less reliable for some reason and simply writing it all out is the only way to make sure I get all of the input from the keyboard.

In my opinion the first error of your code is that you use the String class.

int alphaState[] = {};
 for(int i = 4; i < 14 ;i++){
  alphaState[i] = digitalRead(i);
  }

Oops.
Don't write to memory you don't own

Thank you both for your comments I have fixed both errors, yet the letter d still does not work. It still only sends "".

I have fixed both errors,

. . . but I haven't posted the code.

I turned the String array into a char array and I fixed the memory problem. Neither have solved the problem! :((((((

Really, we must see your sketch.
When you have the same problem, you are probably still overwriting memory that you don't own. Some of us need only 3 seconds to spot that problem. Writing this post takes longer.

At this moment, you have something and done something and ask us what is wrong.

String rawinput = "";

char alpha[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

int alphaState[9] = {};

void setup(){
  
Serial.begin(9600);

  pinMode(4,INPUT);
  pinMode(5,INPUT);
  pinMode(6,INPUT);
  pinMode(7,INPUT);
  pinMode(8,INPUT);
  pinMode(9,INPUT);
  pinMode(10,INPUT);
  pinMode(11,INPUT);
  pinMode(12,INPUT);
  pinMode(13,INPUT);
     
}

void loop(){

  for(int i = 4; i < 14 ;i++){
  alphaState[i] = digitalRead(i);
  }
    
  if (digitalRead(4) == HIGH){
    Serial.println(rawinput);
    delay(250);
    rawinput = "";  
  }
  
  if (digitalRead(5) == HIGH){
    rawinput += alpha[0];
    delay(250);
  }
  
  if (digitalRead(6) == HIGH){
    rawinput += alpha[1];
    delay(250);
  }
  
  if (digitalRead(7) == HIGH){
    rawinput += alpha[2];
    delay(250);
  }
  
  if (digitalRead(8) == HIGH){
    rawinput = rawinput + "d";
    delay(250);
  }
  
  if (digitalRead(9) == HIGH){
    rawinput += alpha[4];
    delay(250);
  }
  
  if (digitalRead(10) == HIGH){
    rawinput += alpha[5];
    delay(250);
  }
  
  if (digitalRead(11) == HIGH){
    rawinput += alpha[6];
    delay(250);
  }
  
  if (digitalRead(12) == HIGH){
    rawinput += alpha[7];
    delay(250);
  }
  
  if (digitalRead(13) == HIGH){
    rawinput += alpha[8];
    delay(250);
  }

}

See reply #3

void loop(){

  for(int i = 4; i < 14 ;i++){
  alphaState[i] = digitalRead(i);
  }
    
  if (digitalRead(4) == HIGH){
    Serial.println(rawinput);
    delay(250);
    rawinput = "";  
  }

You are writing to array indices 4 to 13 of alphaState[] but you have only given it 9 elements so the highest index value can be 8.

Why have you wasted your own time typing delay(250) in each of your IF statements when a single version of it at the bottom of loop() would be sufficient.

...R

You have to check your code line by line and ask yourself what the result would be of what you do. Those are typical beginners mistakes, so it's okay if you are a beginner :wink:

int alphaState[9] = {};
1 ) You don't apply initialization values, so you could do this: int alphaState[9];
2 ) With that line you have alphaState[0] up to alphaState[8]. Be sure not to use a higher number.
3 ) Why '9', when you have 10 buttons.

for(int i = 4; i < 14 ;i++){
O no, did you just use alphaState[9] up to alphaState[13] ? Those do not exist.

pinMode(13,INPUT);
Do you have a button connected to pin 13 ? That is also the pin for the system led. It depends on the Arduino board and the board version if that is okay.

(while I was writing this, Robin2 wrote the same)

Hello All,

What I am trying to do is separate the words in a string and transfer them to an array. My code is crap. Please help me.

String text = "This is a test to see if I can translate each word in this string to its own element in an array.";
String indwords[1000]; //i know, lol

void setup(){

Serial.begin(9600);

}

void loop(){

  for(int i = 0; i < text.length(); i++){
  
    indwords[i] = text.substring(0, text.indexOf(" "));
    text = text.substring(text.indexOf(" "), (text.length()-text.indexOf(" ")));
    Serial.println(indwords[i]);
  }

}

You don't make a new post just because your not getting responses on your other post.

I will request for a merge.

BTW, Look into the function strtok().

Here is how I would do it with a C string (null terminated char array):
C++ code - 29 lines - codepad (edit: added a protection)

I will not tell you how to do it with String objects, because I never used them (and you shouldn't too :wink: )

Whats wrong with String objects. Please share with me your thoughts.

Almost nobody use them, but beginners and a few others... On tiny microcontrollers with short memory, it's never a good idea to use dynamic memory allocation, I think this is the main reason as to why nobody use them.

With char arrays, you are forced to allocate memory yourself, it's not an advantage, but at least you can be sure that your program won't have this kind of memory problem:

Second reason is probably that char arrays are so widely used, that you will find examples everywhere.

Interesting. I can't find information about the strtok function. What is the syntax? What does it do exactly? Thanks!

Strings also require a lot more memory then needed, which is another reason why strings are so widely used.

I can't find information about the strtok function.

Seriously?
Found this in 2 seconds (slow internet :smiley: ) strtok()

I've merged the 2 topics.