Pages: [1]   Go Down
Author Topic: Really slow program  (Read 757 times)
0 Members and 1 Guest are viewing this topic.
Barcelona
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Indeed
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//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;
}
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11694
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

[ I won't respond to messages, use the forum please ]

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Barcelona
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Indeed
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Barcelona
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Indeed
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25770
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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:
  for (uint8_t i = 0, uint8_t j = numLEDnum ; i < functLEDnum; i++, j++) {  // there is a comma , twice !!
    allLEDs[j] = functLED[i];
  }


Code:
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:
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:
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:
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
Logged

Rob Tillaart

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

Barcelona
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Indeed
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

same

whats the diff in size now?
Logged

Rob Tillaart

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

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Yeah, byte and uint8_t are both unsigned char.
Logged

Pages: [1]   Go Up
Jump to: