<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ![在孫老師的啟發下](http://upload-images.jianshu.io/upload_images/1108512-6ade51cd78e0b4a5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 關于Python語言的介紹安裝請參考[廖雪峰的Python教程](http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000) Python是一門解釋型語言,雖然不能夠像c語言一樣編譯上傳到Arduino——什么你說MicroPython,我們再說Arduino呢——仍然是可以跟Arduino的玩耍的,就像scratch一樣。 ## 前言 Python玩轉arduino的方式跟mblock的在線編程模式差不多的,都是先給arduino寫入一個固件,然后操作,不同的是mblock是通過積木來向arduino下指令,這里我們用Python. ## mblock對arduino在線編程的步驟 1. 連接arduino到電腦 2. 打開mblock軟件 3. 選擇對應端口 2. 選擇合適的控制板 ![選擇合適的串口](http://upload-images.jianshu.io/upload_images/1108512-202f2e57330c2f7a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![選擇Arduino控制板](http://upload-images.jianshu.io/upload_images/1108512-9b008777b4891f5d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![安裝固件](http://upload-images.jianshu.io/upload_images/1108512-65bb916e43779fa7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![成功上傳固件](http://upload-images.jianshu.io/upload_images/1108512-a70a6806a667fd3b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 然后我們就可以愉快的編程了. ## 用Python玩轉Arduino 首先你要安裝Python,并且確保安裝了pip 然后我們要用到一個代碼塊[Python Arduino 原型API第二版](https://github.com/vascop/Python-Arduino-Proto-API-v2). >This is a project based on the original?[Python Arduino Prototyping API](https://github.com/HashNuke/Python-Arduino-Prototyping-API). I started a fork and after a while the whole thing was getting too different to make a pull request so I just put it here. The old project had wierd things going on to handle the communication, where I rely on parseInt() to do the work for me. There were also problems with analogWrite(), which is working in this version. 這個代碼庫呢是基于?[Python Arduino Prototyping API](https://github.com/HashNuke/Python-Arduino-Prototyping-API),作者復制了一份,做了很多改動,跟原始版本相差很大,于是就成了一個獨立的版本.這個版本的通訊方式跟老版本不同,主要是用到了```parseInt()```函數.當然這個版本不是很完善,```analogWrite()```用起來可能會有問題——不過凡是代碼的問題,都可以修復的不是嗎. **python玩轉arduino的方法有很多,后面我在完善介紹別的**. ![下載Python操作arduino的庫](http://upload-images.jianshu.io/upload_images/1108512-30751e6dcff1e6f0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ### 上傳固件 ![用Arduino軟件打開固件文件prototype.pde,提示點是就好](http://upload-images.jianshu.io/upload_images/1108512-e0546bd1fb723b9d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![選擇正確的開發板類型](http://upload-images.jianshu.io/upload_images/1108512-30460228e6dd9a63.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![選擇正確的串口](http://upload-images.jianshu.io/upload_images/1108512-c8b6c16031a14194.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![選擇完如圖所示](http://upload-images.jianshu.io/upload_images/1108512-a08043393c92e236.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![上傳固件代碼](http://upload-images.jianshu.io/upload_images/1108512-305e3f747c5d2469.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![固件上傳成功](http://upload-images.jianshu.io/upload_images/1108512-7ebae7d37238b794.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 這樣準備工作就已經完成了,那么讓我們開始愉快的玩耍把.這個過程跟在mblock里連接硬件,最后上傳固件本質是一樣的,形式不同而已. ### 開始玩耍把 學習編程總是從**Hello world!**開始,而學習硬件總是從點亮LED燈開始,那么今天我們就用這個庫來試著點亮一盞LED燈. ![新建文件simple_analogread.py](http://upload-images.jianshu.io/upload_images/1108512-72348abc106457e1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 然后修改代碼如下圖就可以了. ``` from arduino import Arduino import time b = Arduino('COM15') # 實例化一個板子 # 突然很好奇如果我實例多個是否可以? pin = 13 #declare output pins as a list/tuple b.output([pin]) # 用列表的形式指定哪些針腳 # 用來當做輸出針腳 for x in range(10): b.setHigh(pin) # 設定pin引腳為高電平 # b在這里就是指板子 time.sleep(1) # 利用time庫中的sleep函數 print b.getState(pin) # 這個只是用來打印輸出狀態 b.setLow(pin) # 這頂引腳pin為低電平 print b.getState(pin) time.sleep(1) b.close() ``` *快看看Arduino板子上自帶的13號led燈是不是開始閃爍了呢?* 這種方法不是很好玩,這個代碼封裝的比較麻煩,相比于樹莓派最新版的GIPO庫,差的太多,最新的```gpiozero```真的超好用的.后面介紹. 通過上面的代碼就可以跟arduino愉快的玩耍了 >**注意**: **b = Arduino('COM15')** 引號里面代表的是具體通過那個端口跟arduino進行通訊,這里我的是'COM15'端口,大家要注意. ### close()函數 函數的作用是關閉串口鏈接,釋放資源,重新運行代碼的時候不需要重新插拔串口,不然就得重新插拔,作為一個好的習慣保留把. ## 點評 這個類庫做一點小東西足夠了,驅動1602液晶屏啊,電機啊什么還不行,舵機和步進電機可以,但是在中小學教學做一些簡單的東西足夠了. 或者是在用樹莓派控制arduino的情況下,足夠了,當然還有其他方式,未完待續 在GitHub上,搜一下以arduino為關鍵字進行搜索,選擇Python語言的結果,就有很多庫可以用.慢慢找一下總有符合自己風格的. ![GitHub的搜索結果](http://upload-images.jianshu.io/upload_images/1108512-d284c6db32809d1e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 可以看到JavaScript的項目也很多啊,前端的福音啊,感覺JavaScript現在都快逆天了. 可以看到很多甚至是HTML項目,應該是網頁控制arduino,webarduino這種。 發現了一個很膩害的Arduino物聯網平臺項目[PlatformIO](http://platformio.org/) ![相關案例](http://upload-images.jianshu.io/upload_images/1108512-8990615bc10f15db.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![感覺好厲害的樣子](http://upload-images.jianshu.io/upload_images/1108512-7d70121386618b0a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ### 其他庫 發現了[Python版本的arduino命令API](https://github.com/thearn/Python-Arduino-Command-API),似乎這個要完善的多,支持舵機,不過這個庫已經三年沒有更新了,o(╯□╰)o ![python-arduino-command-api](http://upload-images.jianshu.io/upload_images/1108512-c9c7c92a999d55b6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 沒事搜一搜GitHub,總有意想不到的收貨. ## 擴展 其實這種實現模式的關鍵是在arduino寫入固件然后通訊,你可以直接用serial的方式——下次介紹——也可以用firmata這種復雜的通信協議來通信,視乎個人的開發能力而定。 對底層了解越多玩兒越多,未來我也會在深入學習的過程中不斷的分享我的經驗,歡迎大家留言討論。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看