//共用變數放這裡 int D=1000,ND=0; int mode=0,flag=0; unsigned long T,T1; void setup() { //設定區,一開始執行一次 pinMode(13, OUTPUT);// Serial.begin(115200); pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), resetD, FALLING); T=0;T1=0; } void resetD(){ D=1000; if (flag==1){ flag=0; digitalWrite(13, LOW); } else{ flag=1; digitalWrite(13, HIGH); } } void loop() { //主要程式區,此區會不斷的重複執行 if (millis()-T>D){ if (flag==0){ digitalWrite(13, HIGH); flag=1; } else { digitalWrite(13, LOW); flag=0; } T=millis(); } } void serialEvent() { while (Serial.available()) { char inc = (char)Serial.read();//將byte轉成 char if (mode==0 && inc=='S') { mode=1; ND=0; } if (mode==1 && inc=='E'){ D=ND; mode=0; ND=0; Serial.print("now D="); Serial.println(D); } if (mode==1 &&(inc>='0'&& inc<='9')){ ND=ND*10+(inc-'0'); } } }