我无法在 platfromIO 中编译下面的代码。但是在arduino 1.5 IDE中成功编译
#define PRESSED LOW
#define NOT_PRESSED HIGH
const byte led = 13;
#define shortPress 100
#define longPress 500
long blinkInterval = 500;
unsigned long previousBlink=0;
bool ledState = true;
bool blinkState = true;
typedef struct Buttons {
const byte pin = 2;
const int debounce = 10;
unsigned long counter=0;
bool prevState = NOT_PRESSED;
bool currentState;
} Button;
// create a Button variable type
Button button;
void setup() {
pinMode(led, OUTPUT);
pinMode(button.pin, INPUT_PULLUP);
}
void loop() {
// check the button
button.currentState = digitalRead(button.pin);
// has it changed?
if (button.currentState != button.prevState) {
delay(button.debounce);
// update status in case of bounce
button.currentState = digitalRead(button.pin);
if (button.currentState == PRESSED) {
// a new press event occured
// record when button went down
button.counter = millis();
}
if (button.currentState == NOT_PRESSED) {
// but no longer pressed, how long was it down?
unsigned long currentMillis = millis();
//if ((currentMillis - button.counter >= shortPress) && !(currentMillis - button.counter >= longPress)) {
if ((currentMillis - button.counter >= shortPress) && !(currentMillis - button.counter >= longPress)) {
// short press detected.
handleShortPress();
}
if ((currentMillis - button.counter >= longPress)) {
// the long press was detected
handleLongPress();
}
}
// used to detect when state changes
button.prevState = button.currentState;
}
blinkLED();
}
void handleShortPress() {
blinkState = true;
ledState = true;
blinkInterval = blinkInterval / 2;
if (blinkInterval <= 50)
blinkInterval = 500;
}
void handleLongPress() {
blinkState = false;
ledState = false;
}
void blinkLED() {
// blink the LED (or don't!)
if (blinkState) {
if (millis() - previousBlink >= blinkInterval) {
// blink the LED
ledState = !ledState;
previousBlink = millis();
}
} else {
ledState = false;
}
digitalWrite(led, ledState);
}
这是platformio.ini
[env:attiny13]
platform = atmelavr
board = attiny13
framework = arduino
编译 PIO 的输出
Processing attiny13 (platform: atmelavr; board: attiny13; framework: arduino) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Verbose mode can be enabled via `-v, --verbose` option CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/attiny13.html PLATFORM: Atmel AVR 1.15.0 > Generic ATtiny13 HARDWARE: ATTINY13 1MHz, 64B RAM, 1KB Flash PACKAGES: toolchain-atmelavr 1.50400.190710 (5.4.0), framework-arduinoavr 4.1.2 LDF: Library Dependency Finder -> xxxxxx LDF Modes: Finder ~ chain, Compatibility ~ soft Found 0 compatible libraries Scanning dependencies... No dependencies Compiling .pio/build/attiny13/src/Blink.cpp.o src/Blink.cpp: In function 'void setup()': src/Blink.cpp:33:23: error: 'INPUT_PULLUP' was not declared in this scope pinMode(button.pin, INPUT_PULLUP); ^ *** [.pio/build/attiny13/src/Blink.cpp.o] Error 1
==================================================== =========================== [失败] 耗时 1.31 秒
The terminal process terminated with exit code: 1 Terminal will be reused by tasks, press any key to close it.
我需要修改 PlatromIO 的代码吗?
但是,当我评论 INPUT_PULLUP 行时。我可以在 PIO 中编译,但与 Arduino IDE 相比,输出二进制大小更高?
你能建议任何标志或配置来优化二进制大小吗?