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...
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.
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.
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.
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
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.
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]);
}
}
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: