issues with array indexes

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”).

#include "Adafruit_Keypad.h"

/**********************/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(OLED_RESET);

/*******************/

const byte ROWS = 6; 
const byte COLS = 3;

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'n', '0', 'y'},
  {'a', 'b', 'c'},
  {'d', 'e', 'f'}
};

byte rowPins[ROWS] = {5, 6, 7, 8, 9, 10}; 
byte colPins[COLS] = {2, 3, 4}; 

Adafruit_Keypad customKeypad = Adafruit_Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

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

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }

  display.display();
  delay(200);

}
  
void loop(){
  customKeypad.tick();
  //updateScreen();

  while(customKeypad.available()){
    keypadEvent e = customKeypad.read();
    char c = (char)e.bit.KEY;
    //c = c - 48;
    if(e.bit.EVENT == KEY_JUST_PRESSED){
      dialout(c);
      //updateScreen(c);
    }
  }
}

void dialout(char chr){
  Serial.println(chr);
  char my_str[7];
  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;

  int i = 0;
  my_str[i] = chr;
  
  Serial.println(my_str);
  updateScreen(my_str);
}

void updateScreen(char myArray[]) {
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println(myArray);
  display.display();
  delay(20);
}

Let's take another look at that array:

  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).

Umm...I was told that the last zero was a null terminator

 my_str[7] = 0;

this is what happens when I remove that code

5000000Rz 
123456789n0yabcdef
8

Can you please clarify what a null terminator is?

Yes, a null terminator is a zero byte.

You problem is...

char my_str[7];

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);
}
  int i = 0;

a) This is setting i to zero every time.

  my_str[i] = chr;  //This changes the first digit to whatever I press on the keypad

b) Yes, that is correct.

  i++;              //This isnt changing anything

c) Yes it is. It’s adding 1 to i.
However, please see a).

Hi I’ve changed my code to fix quite a few issues I had. but now something strange is happening with the last digit

0

0
1
@
01
2

012
3

0123
4

01234
5

012345
6

0123456
7

01234567
8

012345678 
9

0123456789
0

012345678:
0

012345678;
0

012345678<

here’s what the code looks like now

#include "Adafruit_Keypad.h"

/**********************/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(OLED_RESET);

/*******************/

const byte ROWS = 6; 
const byte COLS = 3;

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'n', '0', 'y'},
  {'a', 'b', 'c'},
  {'d', 'e', 'f'}
};

byte rowPins[ROWS] = {5, 6, 7, 8, 9, 10}; 
byte colPins[COLS] = {2, 3, 4}; 

Adafruit_Keypad customKeypad = Adafruit_Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

/***********************/

  int digitCount = 0;
  int currentDigit;              //I added this section
  char my_str[9];

/**********************/

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

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }

  display.display();
  delay(200);

}
  
void loop(){
  customKeypad.tick();
  //updateScreen();

  while(customKeypad.available()){
    keypadEvent e = customKeypad.read();
    char c = (char)e.bit.KEY;
    //c = c - 48;
    if(e.bit.EVENT == KEY_JUST_PRESSED){
      dialout(c);
      buttonCheck(c);
    }
  }
}

void buttonCheck(char chr){

  if(chr == 'n'){
    //display.clearDisplay();      //I haven't fully implemented this yet
    currentDigit == 0;
    digitCount = 0;
    
  }

}

void dialout(char chr){
  Serial.println(chr);                 
  
  if(currentDigit < 9){
    int currentDigit = digitCount++; //I added this too
    my_str[currentDigit] = chr;
    delay(100);
  }
//  if(currentDigit > 9){
//    my_str[currentDigit] = ' ';       //not sure if this part makes any difference
//    currentDigit == 11;
//    digitCount = 11;
//  }

  Serial.println(currentDigit + " " + digitCount);
  Serial.println(my_str);
  updateScreen(my_str);
}

void updateScreen(char myArray[]) {
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println(myArray);
  display.display();
  delay(20);
}