一条随手的Arduino sketch优化 以Examples-02.Digital-Debounce为例

时间:2021-08-08
本文章向大家介绍一条随手的Arduino sketch优化 以Examples-02.Digital-Debounce为例,主要包括一条随手的Arduino sketch优化 以Examples-02.Digital-Debounce为例使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 1 const int buttonPin = 2;
 2 const int ledPin = 13;
 3 
 4 int ledState = HIGH;
 5 int buttonState;
 6 int lastButtonState = LOW;
 7 
 8 unsigned long lastDebounceTime = 0;
 9 unsigned long debounceDelay = 50;
10 
11 void setup() {
12   pinMode(buttonPin, INPUT);
13   pinMode(ledPin, OUTPUT);
14   digitalWrite(ledPin, ledState);
15 }
16 
17 void loop() {
18   int reading = digitalRead(buttonPin);
19   if (reading != lastButtonState) {
20     lastDebounceTime = millis();
21   }
22   if ((millis() - lastDebounceTime) > debounceDelay) {
23     if (reading != buttonState) {
24       buttonState = reading;
25       if (buttonState == HIGH) {
26         ledState = !ledState;
27       }
28     }
29   }
30   digitalWrite(ledPin, ledState);
31   lastButtonState = reading;
32 }

原文的代码 暂时去掉注释,便于显示重点。

编译结果:

Sketch uses 1116 bytes (3%) of program storage space. Maximum is 30720 bytes.
Global variables use 19 bytes (0%) of dynamic memory, leaving 2029 bytes for local variables. Maximum is 2048 bytes.

优化手段1:选用够用的数据类型,这里面的int对象,都是小于255的,可以换成byte:

const byte buttonPin = 2;
const byte ledPin    = 13;
byte ledState = HIGH;
byte buttonState;
byte lastButtonState = LOW;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay    = 50;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin,    OUTPUT);
  digitalWrite(ledPin, ledState);
}

void loop() {
  byte reading = digitalRead(buttonPin);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
  digitalWrite(ledPin, ledState);
  lastButtonState = reading;
}

  

编译结果:

Sketch uses 1074 bytes (3%) of program storage space. Maximum is 30720 bytes.
Global variables use 17 bytes (0%) of dynamic memory, leaving 2031 bytes for local variables. Maximum is 2048 bytes.

效果:

程序空间减少42字节;SRAM减少2字节。

继续淦:

里面有一个函数在相近的位置,用了两遍,且貌似挺复杂的,就是:millis();

将其合二为一,但是又要增加一个全局变量,试试效果:

const byte buttonPin = 2;
const byte ledPin    = 13;
byte ledState = HIGH;
byte buttonState;
byte lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay    = 50;
unsigned long ml = 0;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin,    OUTPUT);
  digitalWrite(ledPin, ledState);
}
void loop() {
  byte reading = digitalRead(buttonPin);
  ml = millis();
  if (reading != lastButtonState) {
    lastDebounceTime = ml;
  }
  if ((ml - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
  digitalWrite(ledPin, ledState);
  lastButtonState = reading;
}

  编译结果:

Sketch uses 1052 bytes (3%) of program storage space. Maximum is 30720 bytes.
Global variables use 17 bytes (0%) of dynamic memory, leaving 2031 bytes for local variables. Maximum is 2048 bytes.

又干掉22字节的程序空间,SRAM竟然没变?

(1166-1052)/1166 = 104/1166 = 9.8%

将近10%的优化成果,这还没使用骨灰级的寄存器大法来优化pinMode和digitalRead/Write。

后续再试:const byte debounceDelay    = 50; 结果还是1052、17,看来是编译器自动将其优化了。

原文地址:https://www.cnblogs.com/qdsy/p/15114506.html