AccelStepper causes memory corruption?

I have a project where I have to make an menu for an oled screen to control stepper motors. When I make an instance of accelstepper in one of the menus a glitch appears on the screen. (Picture attached)

#include <Adafruit_GFX.h>
#include <ClickEncoder.h>
#include <TimerOne.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <AccelStepper.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // 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(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//AccelStepper motor_1 = AccelStepper(1, 12, 11); //motorInterfaceType, stepPin, dirPin

// Variables
const int rotarySpeed = 4;
int itemsNum = 7;
const int mainNum = 7;
char *mainMenu[7] = {"PAUSE/UNPAUSE", "MOTOR_1", "MOTOR_2", "MOTOR_3", "MOTOR_4", "UNIT", "RESET"};
const int unitNum = 3;
char *unitMenu[3] = {"RPM", "STEP/MIN", "MILILITER/MIN"};
const int motorNum = 3;
char *motorMenu[3] = {"SPEED", "STOP/START", "BACK"};
const int resetNum = 2;
char *resetMenu[2] = {"NO", "YES"};
char *menuItems[7];
int selectedItem = 0;
int selectedPos = 0;
int selectedMotor = 0;
int selectedUnit = 0;
boolean paused = false;
char *title = "MAIN MENU";
boolean motorMenuSelected = false;
boolean mainMenuSelected = true;
boolean unitMenuSelected = false;
boolean resetMenuSelected = false;
int motorSpeeds[4] = {0, 0, 0, 0};
boolean motorPaused[4] = {0, 0, 0, 0};
ClickEncoder *encoder;
int16_t last, value;

void timerIsr() {
  encoder->service();
}

void setup() {
  memcpy(menuItems, mainMenu, sizeof(mainMenu));
  Serial.begin(9600);

  encoder = new ClickEncoder(3, 2, 4);
  encoder->setAccelerationEnabled(false);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();

  Timer1.initialize(1000);
  Timer1.attachInterrupt(timerIsr);

  //motor_1.setMaxSpeed(1000);
  //motor_1.setSpeed(0);

  last = encoder->getValue();
}

void loop() {
  ClickEncoder::Button b = encoder->getButton();
  if (b != ClickEncoder::Open) {
    switch (b) {
      case ClickEncoder::Clicked:
        buttonPressed();
        break;
    }
  }
  value += encoder->getValue();
  if ((int) round(value / rotarySpeed) % itemsNum < 0) {
    selectedItem = itemsNum + (value / rotarySpeed % itemsNum);
  } else {
    selectedItem = (int) round(value / rotarySpeed) % itemsNum;
  }
  if (value / rotarySpeed > last) {
    //down
    last = value / rotarySpeed;
    if (selectedPos == 0) {
      selectedPos = 1;
    } else if (selectedPos == 1) {
      selectedPos = 2;
    }

  } else   if (value / rotarySpeed < last) {
    //up
    last = value / rotarySpeed;
    if (selectedPos == 2) {
      selectedPos = 1;
    } else if (selectedPos == 1) {
      selectedPos = 0;
    }
  }
  if (motorMenuSelected || mainMenuSelected || unitMenuSelected || resetMenuSelected) {
    drawMenu();
  } else {
    drawInt();
  }
  Serial.print(motorSpeeds[0]);
  Serial.print(", ");
  Serial.print(motorMenuSelected);
  Serial.print(", ");
  Serial.print(mainMenuSelected);
  Serial.print(", ");
  Serial.print(unitMenuSelected);
  Serial.println();

  //motor_1.setSpeed(motorSpeeds[0]);
  //motor_1.runSpeed();
  delay(1);
}

void buttonPressed() {
  if (mainMenuSelected) {
    switch (selectedItem) {
      case 0:
        paused = !paused;
        break;
      case 1:
        goMotor();
        selectedMotor = 0;
        title = "MOTOR_1";
        break;
      case 2:
        goMotor();
        selectedMotor = 1;
        title = "MOTOR_2";
        break;
      case 3:
        goMotor();
        selectedMotor = 2;
        title = "MOTOR_3";
        break;
      case 4:
        goMotor();
        selectedMotor = 3;
        title = "MOTOR_4";
        break;
      case 5:
        memcpy(menuItems, unitMenu, sizeof(unitMenu));
        itemsNum = unitNum;
        unitMenuSelected = true;
        mainMenuSelected = false;
        selectedPos = 0;
        break;
      case 6:
        //reset
        memcpy(menuItems, resetMenu, sizeof(resetMenu));
        itemsNum = resetNum;
        resetMenuSelected = true;
        mainMenuSelected = false;
        selectedPos = 0;
        break;
    }
  } else if (motorMenuSelected) {
    switch (selectedItem) {
      case 0:
        //speed
        value = 0;
        motorMenuSelected = false;
        break;
      case 1:
        //start/stop
        motorPaused[selectedMotor] = !motorPaused[selectedMotor];
        break;
      case 2:
        //back
        goMain();
        break;
    }
  } else if (unitMenuSelected) {
    switch (selectedItem) {
      case 0:
        //back
        selectedUnit = 0;
        goMain();
        break;
      case 1:
        //back
        selectedUnit = 1;
        goMain();
        break;
      case 2:
        //back
        selectedUnit = 2;
        goMain();
        break;
    }
  } else if (resetMenuSelected) {
    switch (selectedItem) {
      case 0:
        //back
        goMain();
        break;
      case 1:
        //back
        goMain();
        reset();
        break;
    }
  } else {
    goMotor();
  }
}

void reset() {
  return;
}

void goMotor() {
  memcpy(menuItems, motorMenu, sizeof(motorMenu));
  itemsNum = motorNum;
  selectedPos = 0;
  motorMenuSelected = true;
  mainMenuSelected = false;
  unitMenuSelected = false;
  resetMenuSelected = false;
}
void goMain() {
  memcpy(menuItems, mainMenu, sizeof(mainMenu));
  itemsNum = mainNum;
  selectedPos = 0;
  motorMenuSelected = false;
  mainMenuSelected = true;
  unitMenuSelected = false;
  resetMenuSelected = false;
  title = "MAIN MENU";
}

void drawInt() {
  display.clearDisplay();
  display.setTextColor(BLACK, WHITE);
  display.setCursor(0, 0);
  display.setTextSize(2);
  display.print(title);
  display.setCursor(0, 32);
  display.setTextSize(3);
  display.print(motorSpeeds[selectedMotor]);
  display.setTextSize(1);
  display.setCursor(64, 32);
  display.print(unitMenu[selectedUnit]);
  display.display();
  motorSpeeds[selectedMotor] = value / rotarySpeed;
}

void drawMenu() {
  display.clearDisplay();
  display.setTextColor(BLACK, WHITE);
  display.setCursor(0, 0);
  display.setTextSize(2);
  display.print(title);
  display.setTextSize(1);
  if (selectedPos == 0) {
    display.setTextColor(BLACK, WHITE);
    display.setCursor(0, 16);
    display.print(menuItems[selectedItem]);
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0, 32);
    display.print(menuItems[(selectedItem + 1) % itemsNum]);
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0, 48);
    display.print(menuItems[(selectedItem + 2) % itemsNum]);
  }
  if (selectedPos == 1) {
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0, 16);
    if (selectedItem == 0) {
      display.print(menuItems[itemsNum - 1]);
    } else {
      display.print(menuItems[selectedItem - 1]);
    }
    display.setTextColor(BLACK, WHITE);
    display.setCursor(0, 32);
    display.print(menuItems[selectedItem]);
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0, 48);
    display.print(menuItems[(selectedItem + 1) % itemsNum]);
  }
  if (selectedPos == 2) {
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0, 16);
    if (selectedItem <= 1) {
      display.print(menuItems[itemsNum + (selectedItem - 2) % itemsNum]);
    } else {
      display.print(menuItems[selectedItem - 2]);
    }
    display.setTextColor(WHITE, BLACK);
    display.setCursor(0, 32);
    if (selectedItem == 0) {
      display.print(menuItems[itemsNum - 1]);
    } else {
      display.print(menuItems[selectedItem - 1]);
    }
    display.setTextColor(BLACK, WHITE);
    display.setCursor(0, 48);
    display.print(menuItems[selectedItem]);
  }
  display.display();
}

when I uncomment this line //AccelStepper motor_1 = AccelStepper(1, 12, 11); //motorInterfaceType, stepPin, dirPin the glitch appears.

The part of the code thats responsible for displaying that menu is this:

void drawInt() {
  display.clearDisplay();
  display.setTextColor(BLACK, WHITE);
  display.setCursor(0, 0);
  display.setTextSize(2);
  display.print(title);
  display.setCursor(0, 32);
  display.setTextSize(3);
  display.print(motorSpeeds[selectedMotor]);
  display.setTextSize(1);
  display.setCursor(64, 32);
  display.print(unitMenu[selectedUnit]);
  display.display();
  motorSpeeds[selectedMotor] = value / rotarySpeed;
}

This is the first time I have heard of the AccelStepper library causing a problem so my guess that it is not at fault and the problem lies elsewhere or there is a conflict between libraries. You are using several libraries and, unfortunately, there is no guarantee that they can work in harmony with each other.

Maybe create a program with the AccelStepper code first and get that working before adding other elements of the program.

...R

PS ... Please display your image(s) in your post so we can see it(them) without downloading it(them). See this Simple Image Posting Guide

OP's picture:

What type arduino are you using? This looks like a problem with insufficient dynamic memory.

Thank you for the replies! I made some tests and it turned out there is conflict between the adafruit GFX and AccelStepper libraries so I'm gonna write my own timer based stepper driver