int c=0;
int SW=2;
int SWState;
void setup() {
Serial.begin(9600);
pinMode (SW,INPUT);
c=0;
}
void loop() {
SWState = digitalRead(SW);
if(SWState==RISING)
{ c++;
Serial.print("Count = ");
Serial.println(c);
}
}
จะให้โปรแกรมนับทุกครั้งที่ lead sw ทำงานอะคับ และ ปริ้นโชว์อะคับ if(SWstate==RISING) RISINGใช้ได้ไหมครับ เพราะมันไม่เข้าไปในif แต่ถ้าใส่HIGH LOW มันทำงานในifครับ
คำสั่ง RISING ใช้สำหรับเช็คค่าในฟังก์ชันแบบ interrupt mode สำหรับเช็คเมื่อสถานะขาเปลี่ยนจาก low เป็น high
ในโคดต้องเรียกใช้งานแบบ interrupt ตัวอย่างตามนี้ครับ
https://www.arduino.cc/en/Reference/AttachInterrupt