Edward Hsu
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: embedded system, arduino --- # Arduino Labs ## Embedded System Knowledge ### 記憶體的種類 ### 開關的上拉與下拉電阻 ## Labs ### Lab 3-1 GPIO Output 將 Arduino pin 10 設為 `OUTPUT` (數位輸出)。將一顆 LED 如電路圖所示接上 pin 10 ,並寫程式讓 LED 每隔 0.5 秒閃爍一次 #### 電路圖 ![](https://i.imgur.com/SWc9M9t.png) #### 程式碼 ```c= #define LED (10) #define DELAY (500) void setup() { // put your setup code here, to run once: pinMode(LED, OUTPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(LED, HIGH); delay(DELAY); digitalWrite(LED, LOW); delay(DELAY); } ``` ### Lab 4-1 GPIO Input #### Lab 4-1-1 Pull-down Resistor ##### 電路圖 ![](https://i.imgur.com/24IrriF.png) ##### 程式碼 ```c= #define SWITCH (2) #define LED (6) void setup() { // put your setup code here, to run once: pinMode(SWITCH, INPUT); pinMode(LED, OUTPUT); } void loop() { // put your main code here, to run repeatedly: bool inputVal = digitalRead(SWITCH); digitalWrite(LED, inputVal); } ``` #### Lab 4-1-2 External Pull-up Resistor ##### 電路圖 ![](https://i.imgur.com/BmPEbw2.png) ##### 程式碼 程式碼與 [Lab 4-1-1 Pull-down Resistor](https://hackmd.io/52oGKzOWQFSW__jpLCpMkw?view#%E7%A8%8B%E5%BC%8F%E7%A2%BC) 相同。 #### Lab 4-1-3 Internal Pull-up Resistor 啟用 Arduino 數位接腳的內建上拉電阻。根據 Atmel 公司的技術文件,此內建上拉電阻值為 20K歐姆 ~ 50K歐姆 間。==內建上拉電阻預設沒有啟用,須將接腳設定為 INPUT_PULLUP ,才可啟用內建上拉電阻==。 ##### 電路圖 ![](https://i.imgur.com/dQ39Awh.png) ##### 程式碼 ```c= #define SWITCH (2) #define LED (6) void setup() { // put your setup code here, to run once: pinMode(SWITCH, INPUT_PULLUP); pinMode(LED, OUTPUT); } void loop() { // put your main code here, to run repeatedly: bool inputVal = digitalRead(SWITCH); digitalWrite(LED, inputVal); } ``` ### Lab 4-2 Toggle LED 沿用 [Lab 4-1-1 Pull-down Resistor](https://hackmd.io/52oGKzOWQFSW__jpLCpMkw?view#Lab-4-1-1-Pull-down-Resistor) 的硬體 [電路](https://hackmd.io/52oGKzOWQFSW__jpLCpMkw?view#%E9%9B%BB%E8%B7%AF%E5%9C%961) 。將程式改為: 一開始 LED 滅。之後,按一下開關 LED 亮。再按一下開關 LED 滅。 #### 程式碼 * 全域變數 `ledState` 與 `prevSWState` 為全域變數。 `ledState` 負責記錄 LED 的狀態。初始值為 `LOW` ,也就是熄滅。 `prevSWState` 負責記錄按鈕的前一個狀態。初始值為 `RELEASED ` 。 `swState` 負責記錄按鈕目前的狀態。初始值為 `RELEASED ` 。 * `setup` 將 pin 6 (`LED`) 設定為 `OUTPUT` 。 pin 2 (`SWITCH`) 設定為 `INPUT` 。並且將 LED 設定為熄滅。 * `loop` * 因為 Arduino 板子啟動並執行完 `setup` 後,會不斷地執行 `loop` ,所以 `loop` 不斷地讀入按鈕 (`SWITCH`) 的狀態 (即第 23 行的 `digitalRead(SWITCH)` )。若 `digitalRead(SWITCH)` 讀到的按鈕狀態為 `HIGH` (即按鈕被按下),則將 `swState` 為 `PRESSED` 。否則,將 `swState` 為 `RELEASED` 。 * 假設此時按鈕被按下,第 24 行的敘述會被執行。所以 `swState` 會被設為 `PRESSED` 。 * 接著第 29 行的 `if` 條件句成立, `prevSWState` 會被設定為 `swState` 的值,也就是 `PRESSED` 。 * 由於此時按鈕仍為按下的狀態,所以第 33 行的 `if` 條件不成立。 * 當按鈕被放開後,第 23 行讀到按鈕的狀態為 `LOW` ,所以第 26 行會被執行。 `swState` 被設定為 `RELEASED` 。 * 因為此時 `swState` 為 `RELEASED` ,所以第 29 行的 `if` 條件句不成立,`prevSWState` 仍維持 `PRESSED` 。 * 因為 `swState` 與 `prevSWState` 分別為 `RELEASED` 與 `PRESSED` ,所以第 33 行的 `if` 條件句成立。 * `ledState` 的值被轉換成 `HIGH` 。 (第 34 行) 。然後將新的 LED 狀態寫入 `LED` (即 pin 6) ,點亮 LED。 * 然後將 `prevSWState` 設為 `RELEASED` 。 所以下一個迴圈中 (假設按鈕沒有再被按下) ,第 33 行的 `if` 條件句不成立 (因為 `swState` 與 `prevSwState` 皆為 `RELEASED`)。進而維持 LED 被點亮的狀態。 * 由上列敘述可知,LED 的狀態會在按鈕被釋放時進行轉換。 * 當按鈕再次被按下,然後放開時,LED 就會熄滅。 ```c= #define SWITCH (2) #define LED (6) #define HIGH (1) #define LOW (0) byte ledState = LOW; byte prevSWState = LOW; byte swState = LOW; void setup() { // Setup the GPIO pins. pinMode(SWITCH, INPUT); pinMode(LED, OUTPUT); digitalWrite(LED, ledState); } void loop() { swState = digitalRead(SWITCH); if (swState) { prevSWState = swState; } if (swState != prevSWState) { ledState = !ledState; digitalWrite(LED, ledState); prevSWState = LOW; } } ``` #### 用程式消除開關的彈跳現象 用上述的程式與電路實驗後發現,有時候會發生 LED 該熄滅的時候沒有熄滅,該點亮的時候沒有點亮。這是因為開關的彈跳現象造成的誤動作。可以用程式解決這個問題。 當 Arduino 偵測到開關被按下時,延遲 20 ms ,忽略掉那段訊號不穩定的時間後,再讀一次開關的狀態。確認開關真的被按下後,在進行狀態切換。 ```c= #define SWITCH (2) #define LED (6) #define HIGH (1) #define LOW (0) byte ledState = LOW; byte prevSWState = LOW; byte swState = LOW; void setup() { // Setup the GPIO pins. pinMode(SWITCH, INPUT); pinMode(LED, OUTPUT); digitalWrite(LED, ledState); } void loop() { swState = digitalRead(SWITCH); if (swState) { // Arduino detects that the button has been pressed. // To de-bounce, delay 20 ms. delay(20); // After 20 ms, read the button status again. // If the button status is still pressed, // the status of LED will be changed. byte swTmpState = digitalRead(SWITCH); if (swState == swTmpState) { prevSWState = swState; } } if (swState != prevSWState) { ledState = !ledState; digitalWrite(LED, ledState); prevSWState = LOW; } } ``` ### Lab 4-5 LED 跑馬燈 #### 電路圖 ![](https://i.imgur.com/J9vmn5p.png) #### 程式碼 ```c= #define LED1 (2) #define LED2 (3) #define LED3 (4) #define LED4 (5) #define LED5 (6) const byte firstLed = LED1; const byte lastLed = LED5; byte currLed = firstLed; void setup() { // put your setup code here, to run once: for (int led = LED1; led <= LED5; ++led) { pinMode(led, OUTPUT); digitalWrite(led, LOW); } } void loop() { // put your main code here, to run repeatedly: digitalWrite(currLed, HIGH); delay(200); digitalWrite(currLed, LOW); if (currLed < lastLed) { ++currLed; } else { currLed = firstLed; } } ``` ### Lab 4-6 LED 來回跑馬燈 #### 電路圖 沿用 [Lab 4-5 LED 跑馬燈](https://hackmd.io/@hsuedw/arduino_labs#Lab-4-5-LED-%E8%B7%91%E9%A6%AC%E7%87%88) 的電路。 #### 程式碼 ```c= #define LED1 (2) #define LED2 (3) #define LED3 (4) #define LED4 (5) #define LED5 (6) byte leds[] = {LED1, LED2, LED3, LED4, LED5}; byte numLeds = sizeof(leds) / sizeof(leds[0]); void setup() { // put your setup code here, to run once: for (int i = 0; i < numLeds; ++i) { pinMode(leds[i], OUTPUT); digitalWrite(leds[i], LOW); } } void loop() { for (int i = 0; i < numLeds - 1; ++i) { digitalWrite(leds[i], HIGH); delay(200); digitalWrite(leds[i], LOW); } for (int i = numLeds - 1; i > 0; --i) { digitalWrite(leds[i], HIGH); delay(200); digitalWrite(leds[i], LOW); } } ``` # Reference [Block Circuit EDIT](https://www.block.tw/bce/)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully