和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":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章