Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 DigitalOut led(D3); // LED 的腳位 00004 DigitalIn sw(D2); // 開關的腳位 00005 bool lastState = 0; 00006 // 記錄上次的開關狀態,預設為「低電位」 00007 bool toggle = 0; 00008 // 輸出給 LED 的訊號,預設為「低電位」 00009 int click = 0; 00010 // 開關訊號的改變次數,預設為 0 00011 int main() { 00012 lastState = sw.read(); 00013 // 讀取開關的初始值 00014 00015 while(1) 00016 { 00017 bool b1 = sw.read(); 00018 if (b1 != lastState) { // 如果和之前的開關值不同... 00019 wait_ms(20); // 等待 20 毫秒 00020 bool b2 = sw.read(); // 再讀取一次開關值 00021 if (b1 == b2) { // 確認兩次開關值是否一致 00022 lastState = b1; // 儲存開關的狀態 00023 click ++; // 增加訊號變化次數 00024 } 00025 } 00026 00027 if (click == 2) { // 如果開關狀態改變兩次 00028 click = 0; // 狀態次數歸零 00029 toggle = !toggle; // 取相反值 00030 led = !led;; // 輸出 00031 } 00032 } 00033 } 00034
Generated on Tue Aug 9 2022 01:22:46 by
1.7.2