Really slow program

Any suggestions to speed this up?? (Work in progress) in runs, a bit, but real slow. (Mega 2560 if I uncomment both Serial.print() marked //////: vector error!! again!! no verbose mode warnings.)

//Real Variables
const int floors = 5;                                                    //number of floor the elevator travels to
const int functButtons = 2;                                              //numer of function buttons in the cabin (all buttons except floors)
const int menuButtons = 3;                                               //number of buttons the device contains to control the menu

//Number of LEDs
const int numLEDnum = floors + 1;                                        //real number of number LEDs
const int functLEDnum = functButtons + 1;                                //real number of function LEDs
const int menuLEDnum = menuButtons;                                      //real number of menu LEDs

//Total amount of
const int totalLEDs = numLEDnum + functLEDnum + menuLEDnum;              //Total amount of LEDs
const int totalButtons = floors + functButtons + menuButtons;            //Total amount of Buttons

//Pins assigned to Buttons
const int numButton[] = {4, 5, 6, 7, 8};                                 //pins assigned to each button in ascending order (0 to 4)
const int functButton[] = {2, 3};                                        //pins assigned to each function button in order (bottom to top)
const int menuButton[] = {9, 10, 11};                                    //pins assigned to each menu button in order
int allButtons[totalButtons];

//Pins assigned to LEDs
const int numLED[] = {27, 26, 25, 24, 23, 22};                           //LED pins ordered lowest to highest (floor 0 to 4)
const int functLED[] = {30, 29, 28};                                     //LED pins ordered bottom to top
const int menuLED[] = {33, 32, 31};                                      //LED pins ordered minus, plus, ok
int allLEDs[totalLEDs];

//Arrays for LED and Button state
boolean buttonStates[totalButtons];                                      //holds state of buttons
boolean buttonMem[totalButtons];                                         //hold last state of Buttons to compare to.
boolean LEDStates[totalLEDs];                                            //holds state of LEDs

// Variables for ButtonTranslate
String buttonName;                                                       //input var containing name and num
int length;                                                              //num of chars in buttonName
int charindex;                                                           //num of char being processed
String name;                                                             //buttonName substring containing name
int num;                                                                 //int of buttonName substring containing num

//Variables for Debounce (Button has to be pressed for a certain amount of time for press to count)
const int debounceDelay = 10;                                           //miliseconds the button has to be pressed for press to count
boolean state;                                                          //actual state of button (for debounce)
boolean previousState;                                                  //last state of button  (for debounce)

//Array for buttonRequest
boolean buttonRequest[totalButtons];                                     //holds buttons request

//Vars for SetLEDMode
boolean LEDSwitch = 1;
int followMode = 1;

//Enums
enum buttonNames {cnnumButtons, cnfunctButtons, cnmenuButtons};

void setup() {
  Serial.begin (9600);                                                   //Start Serial connection 
  
  //compose allLEDs[] adding numLED[], functLED[] and menuLED[] info
  for (int i = 0; i < numLEDnum; i++) {
    allLEDs[i] = numLED[i];
  }
  for (int i = numLEDnum; i < (numLEDnum + functLEDnum); i++) {
    allLEDs[i] = functLED[i - numLEDnum];
  }
  for (int i = (numLEDnum + functLEDnum); i < (numLEDnum + functLEDnum + menuLEDnum); i++) {
    allLEDs[i] = menuLED[i - (numLEDnum + functLEDnum)];
  }
  
  //compose allButtons[] adding numButton[], functButton[] and menuButton[] info
  for (int i = 0; i < floors; i++) {
    allButtons[i] = numButton[i];
  }
  for (int i = floors; i < (floors + functButtons); i++) {
    allButtons[i] = functButton[i - floors];
  }
  for (int i = (floors + functButtons); i < (floors + functButtons + menuButtons); i++) {
    allButtons[i] = menuButton[i - (floors + functButtons)];
  }
  
  //Declare LED pins as OUTPUT
  for (int i = 0; i < totalLEDs; i++) {                                  //Declare LEDs as OUTPUT
    pinMode(allLEDs[i], OUTPUT);
  }
  
  //Declare Button pins as INPUT
  for (int i = 0; i < totalButtons; i++) {                               //Declare Buttons as INPUT
    pinMode(allButtons[i], INPUT);
  }
}

/*
 __        ______     ______   .______   
|  |      /  __  \   /  __  \  |   _  \  
|  |     |  |  |  | |  |  |  | |  |_)  | 
|  |     |  |  |  | |  |  |  | |   ___/  
|  `----.|  `--'  | |  `--'  | |  |      
|_______| \______/   \______/  | _|
*/
void loop() {
  if (ButtonChange() == HIGH) {
    //***//***//Serial.println("entered");
    FollowButtons();
  }
}

int WhatLED (int buttoni) {
  if (buttoni >= 0 && buttoni < floors) {
    return buttoni;
  }
  if (buttoni >= floors && buttoni < (floors + functButtons)) {
    return (buttoni + 1);
  }
  if (buttoni >= (floors + functButtons) && buttoni < totalButtons) {
    return (buttoni + 2);
  }
  return -1; //error code
}

void LightLEDs() {
  for (int i = 0; i < totalLEDs; i++) {
    digitalWrite(allLEDs[i], LEDStates[i]);
  }
}

void FollowButtons() {
  ScanButtons();
  //***//***//Serial.println("hello");
  for (int i = 0; i < totalButtons; i++) {
    int startLED = WhatLED(i);
    boolean state = buttonStates[i] ;
    LEDStates[startLED] = state;
    if (i < (floors + functButtons)) {
      LEDStates[(startLED + 1)] = state;
    }
  }
    LightLEDs(); // (add if)
}

boolean debounce(int pin) {                                              //returns HIGH if button if HIGH for debounceDelay in miliseconds
  previousState = digitalRead(pin);                                      //store pin state
  for (int i = 0; i < debounceDelay; i++) {                              //make sure button is pressed every milisecond debounceDelay times
    delay(1);
    state = digitalRead(pin);
    if (state != previousState) {                                        //reset if change in state
      i = 0;
      previousState = state;
    }
  }
  return state;                                                          //return stable state
}

void ScanButtons() {                                                     //reads all buttons and stores state 
  for (int i = 0; i < totalButtons; i++) {
    buttonStates[i] = debounce(allButtons[i]); 
  }
}

boolean ButtonChange() {                                                 //returns HIGH if there has been a change in button state from last scan
    for (int i = 0; i < totalButtons; i++) {
      buttonMem[i] = buttonStates[i];
    }
    ScanButtons();
    for (int i = 0; i < totalButtons; i++) {
      if (buttonMem[i] != buttonStates[i]) {
        return HIGH;
      }
    }
  return LOW;
}

Not sure but you are debouncing all your buttons is series, at 10ms per button might take a while?

To debounce read all the buttons, wait one delay, read them all again, see if any changed - in other
words debounce them in parallel.

I think MarkT is right. 10 x 10 mS debounce is 100 mS (1/10th of a second) every time you scan.

First, you only need to debounce if the state changes, second as he said, you can debounce them all at once.

I did an example here:

That shows how you can maintain a "last change" time for each switch, and debounce. Also it does not use delay at all.

That shows how you can maintain a "last change" time for each switch, and debounce. Also it does not use delay at all.

Great, thanks. Real nice.

What does this do??

(switchState [i] == LOW) ? "ON  " : "off ")

If switch state is non-zero then return "ON', else return "off"

There are many ints that can be an uint8_t that makes arrays shorter; loops faster etc as long as the range is 0..255 (e.g. pin numbers)

  for (int i = numLEDnum; i < (numLEDnum + functLEDnum); i++) {
    allLEDs[i] = functLED[i - numLEDnum];
  }

can be better written with two indices in the for loop. removes an subtraction every loop.

  for (uint8_t i = 0, uint8_t j = numLEDnum ; i < functLEDnum; i++, j++) {  // there is a comma , twice !!
    allLEDs[j] = functLED[i];
  }

int WhatLED (int buttoni) {
  if (buttoni >= 0 && buttoni < floors) {
    return buttoni;
  }
  if (buttoni >= floors && buttoni < (floors + functButtons)) {
    return (buttoni + 1);
  }
  if (buttoni >= (floors + functButtons) && buttoni < totalButtons) {
    return (buttoni + 2);
  }
  return -1; //error code
}

can be shorter

int WhatLED (int buttoni) 
{
  if ( buttoni >= 0 && buttoni < floors) return buttoni;
  if ( buttoni < (floors + functButtons)) return (buttoni + 1);  // as the first test assures that buttoni >= floors 
  if ( buttoni < totalButtons) return (buttoni + 2);
  return -1; //error code
}

boolean ButtonChange() {                                                 //returns HIGH if there has been a change in button state from last scan
    for (int i = 0; i < totalButtons; i++) {
      buttonMem[i] = buttonStates[i];
    }
    ScanButtons();
    for (int i = 0; i < totalButtons; i++) {
      if (buttonMem[i] != buttonStates[i]) {
        return HIGH;
      }
    }
  return LOW;
}

why not return the index of the button that is changed, is a flag and value in one

int ButtonChange()
{                                       
    for (uint8_t i = 0; i < totalButtons; i++) buttonMem[i] = buttonStates[i];

    ScanButtons();

    for (uint8_t i = 0; i < totalButtons; i++) 
    {
      if (buttonMem[i] != buttonStates[i]) {
        return i;  // at least button i has changed (but there might be more)
      }
    }
  return -1;   // no button 
}

your turn

// as the first test assures that buttoni >= floors

oh, right.

I changed all ints to byte now. is uint8_t better or same??

same

whats the diff in size now?

RonaldPoFo:
I changed all ints to byte now. is uint8_t better or same??

Yeah, byte and uint8_t are both unsigned char.