[codesyntax lang="cpp" lines="normal"]
#define LED 13 #define PIN 2 #define TEST 3 volatile int state = LOW; void setup() { Serial.begin(9600); pinMode(LED, OUTPUT); pinMode(TEST, OUTPUT); attachInterrupt(0, stateChange, CHANGE); } void loop() { int v = digitalRead(PIN); Serial.println(v); delay(500); } void stateChange() { Serial.println("======="); state = !state; digitalWrite(LED, state); if(state) { analogWrite(TEST, 10); } else { analogWrite(TEST, 0); } }
[/codesyntax]
备注:
- 根据本人实验,MJDuino UNO版本有时回因为中断函数的调用导致程序停止运行。