#define LED_PIN PB3 // PB_3 or D13 pin
#define BTN_PIN PB4 // PB_4 or D12 pin
#define COUNTDOWN_MAX (16)
volatile bool state_changed = false;
char sbuf[64];
void irq_handler( ) {
detachInterrupt( BTN_PIN );
state_changed = true;
}
void setup() {
pinMode( BTN_PIN, INPUT_PULLUP );
pinMode( LED_PIN, OUTPUT );
digitalWrite( LED_PIN, LOW );
Serial.begin( 115200 );
Serial.println( "Arduino-STM32 Programming..." );
attachInterrupt( BTN_PIN, irq_handler, FALLING );
}
void loop() {
if ( state_changed ) {
uint16_t bits;
for ( int i=0; i < 16; i++ ) {
bits = (bits << 1) | digitalRead( BTN_PIN );
delay(2);
}
if (bits == 0) {
int state = !digitalRead(LED_PIN);
digitalWrite( LED_PIN, state );
sprintf( sbuf, "LED state: %d @%lu", state, millis() );
Serial.println( sbuf );
Serial.flush();
}
uint32_t countdown = COUNTDOWN_MAX;
while ( countdown ) {
if ( digitalRead(BTN_PIN) ) {
countdown--;
} else {
countdown = COUNTDOWN_MAX;
}
delay(2);
}
state_changed = false;
attachInterrupt( BTN_PIN, irq_handler, FALLING );
}
else
delay(10);
}