Harrysome Chou
    • 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
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ###### tags: `Arduino` # Arduino 程式架構及語法 Arduino有自己的一套IDE(整合開發環境),包含了編輯器以及編譯器,且它能編譯數十種不同型號的版子(不同的微控),有需要都可以上Google找。這篇主要是講Arduino IDE中的程式部分,要看介面使用請見這篇 [Arduino IDE 2.0](/8oBFuKhKQamJBh0OKP4UpQ) ## 基本架構 ```cpp= //範例程式------ #include<...> //1. 引入函式庫 //-------------------------------------------------------------------------- const int pin = 13; //2. 腳位或變數定義 int blink = 1000; //-------------------------------------------------------------------------- void setup(){ //3. 重複執行前的定義或是只需要做一次的動作 pinMode(pin,OUTPUT); } //-------------------------------------------------------------------------- void loop(){ //4. 主程式 while(blink>0){ digitalWrite(pin,HIGH); delay(blink); digitalWrite(pin,LOW); delay(blink); blink -= 100; } blink = 1000; } //-------------------------------------------------------------------------- void OuO(){ //5. 自訂函式 } ``` ### 1. 引入函式庫 因為Arduino是個不完全的Cpp,且語法不完全相同,加上它又需要去控制及讀取Arduino的訊號,所以時不時會需要不同的函式去做操作,最常見的有< SoftwareSerial.h > < SPI.h > < Wire.h >等,除了引用寫好的函式,也可以從網上下載,詳情請見[Arduino Libraries](https://hackmd.io/LThJq7lpR-q9cm3RqXzq8g) ### 2. 腳位或變數定義 Arduino的腳位是用數字來定義,像是0到13的digital pin,其實只需要直接寫入就行,但有時為了辨識方便,所以用一個廣域變數來幫它命名,所以在主程式中打它的名字就會是你定義的腳位,有時會怕不小心動到腳位的定義,所以會在前面加上一個const。除了腳位定義,這裡也能放你需要計算的變數,在程式中的任何一個地方都能取用(全域變數)。 ### 3. 重複執行前的定義或是只需要做一次的動作 我們通常會將腳位的狀態定義放在這裡,或是像一些通訊的速率、頻段的設置,只需要做一次的事情放這就行了,不用讓它在下面的loop中一直跑。 ### 4. 主程式 顧名思義就是放要執行的程式的地方,和Cpp不同的是它本身就是一個無限迴圈,所以它會一直執行一套相同的動作,或是你用變數控制裡面的動作次數。 ### 5. 自訂函數 如果覺得寫一些要執行的動作會很複雜,又或是你要寫遞迴的式子,都可以用自訂函數來簡化loop中的結構,也能讓程式比較容易閱讀。 ## 基本函式 下面幾個常用的函式是內建在Arduino中的,雖簡單但是很重要,如果無法理解就很難融會貫通,而其它複雜的大多要進行include才能使用,但只需下面幾個就能做到很簡單的Arduino project,如需了解digital,analog的差別,請見[基本電學及基礎電料](https://hackmd.io/vQxNZbMrQtqY77zWp7jzMg) * **pinMode(腳位,狀態)** 在setup中是不可或缺的一個函式,如果沒有定義腳位是輸出或輸入,它完全不會做動(沒有預設狀態),可設的狀態有兩種,分別是++INPUT++和++OUTPUT++,而在數位腳上,Arduino有內建上拉電阻,只要用++INPUT_PULLUP++就能啟動 * **digital** 它適用於所有腳位包括類比腳位 1.digitalRead(腳位) 它讀到的數值只有1和0(高電位和低電位),多用來判斷有無觸發開關或感測器 2.digitalWrite(腳位,電位) 它和read相對,電位可以寫HIGH或是LOW(高電平及低電平) * **analog** 1.analogRead(腳位) ++只適用類比腳位++,讀入的數值會是0到1023的值(2⁰~2¹⁰) 2.analogWrite(腳位,電位) **++只適用於有寫~(pwm)符號的數位腳位++**,這是pwm的寫法,可以輸出0-255的值,或是用map轉成0-1023,輸出電壓由低電位到高電位,pwm的介紹請見[基本電學及基礎電料](/vQxNZbMrQtqY77zWp7jzMg) * **Serial** 1.Serial.begin(鮑率) 鮑率是板子和電腦之間的通訊頻率,通常會使用9600或是115200,鮑率介紹見[Aduino IDE使用與操作](/M_yqUR41RhSFhaJb2YyA0Q) 2.Serial.print(數值/字串/字元) 可以在序列埠監控視窗看到輸出,一次只能輸出一行,和cpp是一樣的 3.Serial.println(數值/字串/字元) 和print不同的是它會先換行再輸出 4.Srial.available() 通常會用if來判斷,它會讀取序列通訊介面是否有任何資料輸入,如果有則為true,反之為false。 <font color = "red">補充.</font> 習慣cpp的人在做輸入輸出時會很想打>>和<<,這可以在<Streaming.h>中找到,如要使用要上網下載函式庫 * **delay(毫秒數)** 功用是延遲程式的執行,或是製造緩衝時間,也可以用delayMicroseconds()

    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