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;
}