Arduino优化 减少重复代码 例2
作者:互联网
Example->Blink:
void setup() { pinMode(LED_BUILTIN, OUTPUT); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(1000); digitalWrite(LED_BUILTIN, LOW); delay(1000); }
编译结果:
Sketch uses 924 bytes (3%) of program storage space. Maximum is 30720 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.
运行了两遍digitalWrite和delay,完全可以合并。
需要增加一个bool全局变量,再合并代码:
bool b = false; void setup() { pinMode(LED_BUILTIN, OUTPUT); } void loop() { digitalWrite(LED_BUILTIN, b); delay(3000); b = !b; }
编译结果:
Sketch uses 914 bytes (2%) of program storage space. Maximum is 30720 bytes.
干掉10字节的程序空间。
关键是:bool类型,true==1,false==0,使用!操作可以互换0 false和1 true。
积少成多+由浅入深
标签:LED,Arduino,重复,digitalWrite,代码,bytes,delay,BUILTIN,void 来源: https://www.cnblogs.com/qdsy/p/15114681.html