玩轉Mixly – 10、Arduino AVR編程 之 傳感器,執行器,通信,存儲,factory

以下內容源自Mixly官方技術文檔:

https://mixly.readthedocs.io/zh_CN/latest/Arduino/AVR/10Sensor.html

https://mixly.readthedocs.io/zh_CN/latest/Arduino/AVR/11Actuator.html

https://mixly.readthedocs.io/zh_CN/latest/Arduino/AVR/13Communicate.html

https://mixly.readthedocs.io/zh_CN/latest/Arduino/AVR/14Storage.html

https://mixly.readthedocs.io/zh_CN/latest/Arduino/AVR/16Factory.html

 

傳感器

傳感模塊:超聲波、DHT11、DS18B20(新增)

在傳感器部分,我們將一些最常見的傳感器相關指令進行編庫,比如超聲波測距模塊,學生在使用超聲波進行測距時,可直接運用該模塊,只需將Trig與Echo管腳填好即可,這大大節省了學生的編程時間,這將更有助於使學生將更多的精力集中在創意實踐上而非抓耳撓腮地編程。

執行器

執行模塊:聲音播放、舵機控制、I2C液晶模塊

 

 

通信

通信是爲了方便人與計算機交互而採用的一種特殊通信方式。具體包括:串口通信(新增串口選擇和波特率設置)、紅外通信、I2C通信、SPI通信(新增)。

 

串口通信

串口通信功能學生可在串口監視器中查看。這一功能對於學生檢查自己代碼以及監視傳感器數據提供了便利條件。 假設學生將LM35的溫度傳感器接到模擬管腳A0口,學生可通過 .. image:: images/13Communicate/commu4.png 指令觀察當前室溫。隨後可能會提出疑問:當前室溫怎麼可能是58?這將引發學生進一步的思考造成該數據的可能性。

紅外通信

上圖指令主要運用的是紅外接收管與遙控器之間的數據發射與接收功能。學生掌握了紅外通信的相關內容,便可以製作遙控風扇、遙控汽車等自主設計更強的創意電子產品。

I2C通信

I2C(Inter-Integrated Circuit)是同步通信的一種特殊形式,具有接口線少,控制方式簡單,器件封裝形式小,通信速率較高等優點。Mixly支持I2C的讀取和寫入,並且支持基於I2C協議的執行器。

SPI通信

SPI是串行外設接口(Serial Peripheral Interface)的縮寫。SPI,是一種高速的,全雙工,同步的通信總線,並且在芯片的管腳上只佔用四根線,節約了芯片的管腳,同時爲PCB的佈局上節省空間,提供方便,擁有簡單易用的特性。用戶可以使用Mixly向SPI傳輸數據。

 

存儲

存儲模塊:EEPROM讀寫,SD卡寫入

 

 

Factory

Factory中的模塊可以方便用戶編寫Mixly中不支持的傳感器功能。 .. image:: images/16Factory/factory.png

將以RTC1307庫爲例,介紹利用factory中的模塊,在Mixly快速使用Arduino標準庫。

準備工作

1.從github.com等網站下載ds1307RTC Arduino庫。 2.將庫文件解壓到Mixly/arduino-1.x.x/portable/sketchbook/libraries目錄中。 3.打開Mixly自帶的arduino IDE(路徑Mixlyarduino-1.x.xarduino.exe),在軟件中打開 庫目錄下的example範例,編譯並上傳,測試能否正常使用。如果不能正常使用,則需要更換庫或者檢查硬件問題。

重寫程序

打開範例程序Mixly/arduino-1.x.x/portable/sketchbook/libraries/DS1307RTC/examples/ReadTime/ReadTest/ReadTest.ino,先分析程序,並適當簡化程序。

源程序如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

#include <Wire.h>

#include "TimeLib.h"

#include "DS1307RTC.h"

 

void setup() {

Serial.begin(9600);

while (!Serial) ; // wait for serial

delay(200);

Serial.println("DS1307RTC Read Test");

Serial.println("-------------------");

}

 

void loop() {

tmElements_t tm;

 

if (RTC.read(tm)) {

Serial.print("Ok, Time = ");

print2digits(tm.Hour);

Serial.write(':');

print2digits(tm.Minute);

Serial.write(':');

print2digits(tm.Second);

Serial.print(", Date (D/M/Y) = ");

Serial.print(tm.Day);

Serial.write('/');

Serial.print(tm.Month);

Serial.write('/');

Serial.print(tmYearToCalendar(tm.Year));

Serial.println();

} else {

if (RTC.chipPresent()) {

Serial.println("The DS1307 is stopped. Please run the SetTime");

Serial.println("example to initialize the time and begin running.");

Serial.println();

} else {

Serial.println("DS1307 read error! Please check the circuitry.");

Serial.println();

}

delay(9000);

}

delay(1000);

}

 

void print2digits(int number) {

if (number >= 0 && number < 10) {

Serial.write('0');

}

Serial.print(number);

}

 

 

簡化程序如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#include <Wire.h>

#include "TimeLib.h"

#include "DS1307RTC.h"

 

void setup() {

Serial.begin(9600);

}

 

void loop() {

tmElements_t tm;

if (RTC.read(tm)) {

Serial.print(tm.Hour);

Serial.print(':');

Serial.print(tm.Minute);

Serial.print(':');

Serial.print(tm.Second);

Serial.print(tm.Day);

Serial.print('/');

Serial.print(tm.Month);

Serial.print('/');

Serial.print(tmYearToCalendar(tm.Year));

}

delay(1000);

}

 

 

Factory模式下模塊:

編寫完成後,編譯並上傳,進行測試。測試利用factroy編寫的程序效果是否與arduino IDE編寫相同。

微信關注圖中張十三的博客公衆號,與張十三一起探討更多mixly開發問題:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章