代码:
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 50 51 52 53 54 55 56 57 58 59 60 |
<span style="color: #cc6600;">int</span> potpin=0; <span style="color: #cc6600;">int</span> ledpin_red=9; <span style="color: #cc6600;">int</span> ledpin_yellow=10; <span style="color: #cc6600;">int</span> ledpin_green=11; <span style="color: #cc6600;">int</span> val=0; <span style="color: #cc6600;">void</span> <span style="color: #cc6600;"><b>setup</b></span>() { <span style="color: #cc6600;"><b>Serial</b></span>.<span style="color: #cc6600;">begin</span>(9600);<span style="color: #7e7e7e;">//设置波特率为9600</span> <span style="color: #cc6600;">pinMode</span>(ledpin_red,<span style="color: #006699;">OUTPUT</span>); <span style="color: #cc6600;">pinMode</span>(ledpin_yellow,<span style="color: #006699;">OUTPUT</span>); <span style="color: #cc6600;">pinMode</span>(ledpin_green,<span style="color: #006699;">OUTPUT</span>); <span style="color: #7e7e7e;">//注意:模拟接口自动设置为输入</span> } <span style="color: #cc6600;">void</span> <span style="color: #cc6600;"><b>loop</b></span>() { val=<span style="color: #cc6600;">analogRead</span>(potpin);<span style="color: #7e7e7e;">// 读取传感器的模拟值并赋值给val</span> <span style="color: #cc6600;"><b>Serial</b></span>.<span style="color: #cc6600;">println</span>(val);<span style="color: #7e7e7e;">//显示val 变量</span> <span style="color: #7e7e7e;">//红灯</span> <span style="color: #cc6600;"><b>Serial</b></span>.<span style="color: #cc6600;">println</span>(<span style="color: #006699;">"Red! STOP!!!!!"</span>); <span style="color: #cc6600;">analogWrite</span>(ledpin_red,val/4);<span style="color: #7e7e7e;">//红灯亮,绿灯和黄灯灭</span> <span style="color: #cc6600;">analogWrite</span>(ledpin_green,<span style="color: #006699;">LOW</span>); <span style="color: #cc6600;">analogWrite</span>(ledpin_yellow,<span style="color: #006699;">LOW</span>); <span style="color: #cc6600;">delay</span>(5000);<span style="color: #7e7e7e;">//延时5秒</span> <span style="color: #7e7e7e;">//黄灯变换</span> <span style="color: #cc6600;"><b>Serial</b></span>.<span style="color: #cc6600;">println</span>(<span style="color: #006699;">"Notice!SLOW DOWN!!! Yellow!"</span>); <span style="color: #cc6600;">analogWrite</span>(ledpin_yellow,val/4); <span style="color: #cc6600;">delay</span>(500); <span style="color: #cc6600;">analogWrite</span>(ledpin_yellow,<span style="color: #006699;">LOW</span>); <span style="color: #cc6600;">delay</span>(500); <span style="color: #cc6600;">analogWrite</span>(ledpin_yellow,val/4); <span style="color: #cc6600;">delay</span>(500); <span style="color: #cc6600;">analogWrite</span>(ledpin_yellow,<span style="color: #006699;">LOW</span>); <span style="color: #cc6600;">delay</span>(500); <span style="color: #cc6600;">analogWrite</span>(ledpin_yellow,val/4); <span style="color: #cc6600;">delay</span>(500);<span style="color: #7e7e7e;">//黄灯闪烁三次,此时红灯仍是亮的</span> <span style="color: #7e7e7e;">//绿灯</span> <span style="color: #cc6600;"><b>Serial</b></span>.<span style="color: #cc6600;">println</span>(<span style="color: #006699;">"GREEN! GOGOGO!"</span>); <span style="color: #cc6600;">analogWrite</span>(ledpin_green,val/4);<span style="color: #cc6600;">analogWrite</span>(ledpin_red,val/4);<span style="color: #7e7e7e;">//绿灯亮,红灯和黄灯灭</span> <span style="color: #cc6600;">analogWrite</span>(ledpin_red,<span style="color: #006699;">LOW</span>); <span style="color: #cc6600;">analogWrite</span>(ledpin_yellow,<span style="color: #006699;">LOW</span>); <span style="color: #cc6600;">delay</span>(5000);<span style="color: #7e7e7e;">//延时5秒</span> <span style="color: #7e7e7e;">//变换,完全复制上面的</span> <span style="color: #cc6600;"><b>Serial</b></span>.<span style="color: #cc6600;">println</span>(<span style="color: #006699;">"Notice! Yellow!"</span>); <span style="color: #cc6600;">analogWrite</span>(ledpin_yellow,val/4); <span style="color: #cc6600;">delay</span>(500); <span style="color: #cc6600;">analogWrite</span>(ledpin_yellow,<span style="color: #006699;">LOW</span>); <span style="color: #cc6600;">delay</span>(500); <span style="color: #cc6600;">analogWrite</span>(ledpin_yellow,val/4); <span style="color: #cc6600;">delay</span>(500); <span style="color: #cc6600;">analogWrite</span>(ledpin_yellow,<span style="color: #006699;">LOW</span>); <span style="color: #cc6600;">delay</span>(500); <span style="color: #cc6600;">analogWrite</span>(ledpin_yellow,val/4); <span style="color: #cc6600;">delay</span>(500);<span style="color: #7e7e7e;">//闪烁三次</span> } |
效果: