Go Down

Topic: Really slow program (Read 846 times) previous topic - next topic

RonaldPoFo

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.)
Code: [Select]
//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;
}

MarkT

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 won't respond to messages, use the forum please ]

Nick Gammon

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:

http://arduino.cc/forum/index.php/topic,139805.msg1053019.html#msg1053019

That shows how you can maintain a "last change" time for each switch, and debounce. Also it does not use delay at all.
http://www.gammon.com.au/electronics

RonaldPoFo

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

RonaldPoFo

What does this do??
Code: [Select]
(switchState [i] == LOW) ? "ON  " : "off ")

AWOL

If switch state is non-zero then return "ON', else return "off"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

robtillaart

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)

Code: [Select]
  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.
Code: [Select]

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




Code: [Select]
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
Code: [Select]
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
}





Code: [Select]

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

Code: [Select]

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
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

RonaldPoFo

Quote
// as the first test assures that buttoni >= floors
oh, right.

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

robtillaart

same

whats the diff in size now?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

tobyb121


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


Yeah, byte and uint8_t are both unsigned char.

Go Up