Hi. I’m trying to make an arduino-based phone with a Nano, ssd1306, and a sim800l. I’m having issues adding chars to a string to make phone numbers. I could really use a solution because the screen gets stuck on (whatever key I press + “000000”).
char my_str[7]; // declare an array with 7 elements (indices will be 0 through 6)
my_str[0] = '0';
my_str[1] = '0';
my_str[2] = '0';
my_str[3] = '0';
my_str[4] = '0';
my_str[5] = '0';
my_str[6] = '0';
my_str[7] = 0; // OOPS, this is beyond the end of the array!
I suggest that you declare your array to have 8 elements rather than 7.
Really, though, I would make the array even longer (at least 11 or 12 elements), in order to handle phone numbers with more than 7 digits. And remember, the size of your array needs to exceed (not merely equal) the maximum number of characters you will want it to hold. This is because of the need for a null terminator (of which, I believe, you are already aware).
That is an array of 7 elements.
Indexes numbered 0 through 6 inclusive.
It is not legal to attempt to access element [7].
That memory location belongs to something else...
Try at least...
char my_str[8];
And re-read odometer’s post about how much space you might actually need.
ok Ive changed some things in the code. I’ve set a limit for 10 digits. And I think I’ve added a zero byte. But I cant seem to change anything past the first digit.
void dialout(char chr){
Serial.println(chr);
char my_str[10];
my_str[0] = '0';
my_str[1] = '0';
my_str[2] = '0';
my_str[3] = '0';
my_str[4] = '0';
my_str[5] = '0';
my_str[6] = '0';
my_str[7] = '0';
my_str[8] = '0';
my_str[9] = '0';
my_str[10] = '\0';
int i = 0;
my_str[i] = chr; //This changes the first digit to whatever I press on the keypad
i++; //This isnt changing anything
Serial.println(my_str);
updateScreen(my_str);
}