和12歲小同志搞創客開發:手撕代碼,做一款火焰報警器

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"機緣巧合在網上認識一位12歲小同志,從零開始系統輔導其創客開發思維和技巧。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a3/a31f0ecd94d020de4655296fefd705cb.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"項目專欄:","attrs":{}},{"type":"link","attrs":{"href":"https://blog.csdn.net/m0_38106923/category_11097422.html","title":null,"type":null},"content":[{"type":"text","text":"https://blog.csdn.net/m0_38106923/category_11097422.html","attrs":{}}]}]},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在廚房安裝一個火焰報警器應該是非常管用的,如果不小心忘關煤氣的話,只要有一點點的火苗,就能觸發火焰報警器,探測距離可達20cm。一個小小的報警器,講不定就能避免一場不必要的意外發生!","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a4/a4a1d9539424e6bdad213fed754373c8.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 項目連線如下所示:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e6/e6431b452ae8710dd3352a2292790612.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實現代碼如下所示:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"float sinVal; \nint toneVal; \n \nvoid setup(){ \n pinMode(8, OUTPUT); // 蜂鳴器引腳設置\n Serial.begin(9600); //設置波特率爲9600 bps\n}\n \nvoid loop(){ \n int sensorValue = analogRead(0); //火焰傳感器連到模擬口,並從模擬口讀值\n Serial.println(sensorValue); \n delay(1);\n if(sensorValue < 1023){ // 如果數據小於1023,說明有火源,蜂鳴器響 \n for(int x=0; x<180; x++){\n //將sin函數角度轉化爲弧度\n sinVal = (sin(x*(3.1412/180)));\n //用sin函數值產生聲音的頻率\n toneVal = 2000+(int(sinVal*1000));\n //給引腳8一個\n tone(8, toneVal);\n delay(2); \n } \n } else { // 如果數據大於等於1023,沒有火源,關閉蜂鳴器\n noTone(8); //關閉蜂鳴器 \n }\n} ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"程序中,首先,定義兩個變量:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"float sinVal; \nint toneVal; ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"浮點型變量sinVal用來存儲正弦值,正弦波呈現一個波浪形的變化,變化比較均勻,所以我們選用正弦波的變化來作爲我們聲音頻率的變換,toneVal從sinVal變量中獲得數值,並把它轉換爲所需要的頻率。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏用的是sin()函數,一個數學函數,可以算出一個角度的正弦值,這個函數採用弧度單位。因爲我們不想讓函數值出現負數,所以設置for循環在0~179之間,也就是0~180度之間。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"for(int x=0; x<180; x++){\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"函數sin()用的弧度單位,不是角度單位。要通過公式:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"3.1412/180","attrs":{}},{"type":"text","text":"將角度轉爲弧度:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"sinVal = (sin(x*(3.1412/180)));","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"之後,將這個值轉變成相應的報警聲音的頻率:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" toneVal = 2000+(int(sinVal*1000));","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏有個知識點:浮點型值轉換爲整型。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"sinVal是個浮點型變量,也就是含小數點的值,而我們不希望頻率出現小數點的,所以需要有一個浮點值轉換爲整型值得過程,也就是下面這句語句就完成了這件事:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" int(sinVal*1000)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"把sinVal乘以1000,轉換爲整型後再加上2000賦值給變量toneVal,現在toneVal就是一個適合聲音頻率了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"之後,我們用tone()函數把生成的這個頻率給我們的蜂鳴器。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" tone(8, toneVal);","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面我們來介紹一下tone相關的三個函數:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1、tone(pin,frequency):pin是指連接到蜂鳴器的數字引腳,frequency是以Hz爲單位的頻率值。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2、tone(pin,frequency,duration):第二個函數,有個duration參數,它是以毫秒爲單位,表示聲音長度的參數。像第一個函數,如果沒有指定duration,聲音將一直持續直到輸出一個不同頻率的聲音產生。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"3、noTone(pin):noTone(pin)函數,結束該指定引腳上產生的聲音。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"運行程序,將打火機慢慢靠近火焰傳感器,看看蜂鳴器會不會報警。","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章