Arduino入门篇:第五章代码及练习答案

编号:button_RYG

<br> const int RED = 8;        //红色<br> const int YELLOW = 9;     //黄色<br> const int GREEN = 10;     //绿色<br> const int BUTTON = 11;    //按键连接管脚      </p> 
<p><br> void setup() {<!-- --><br>   // put your setup code here, to run once:<br>   pinMode(RED, OUTPUT);<br>   pinMode(YELLOW, OUTPUT);<br>   pinMode(GREEN, OUTPUT);<br>   pinMode(BUTTON, INPUT_PULLUP);<br>   digitalWrite(RED, LOW);<br>   digitalWrite(YELLOW, LOW);<br>   digitalWrite(GREEN, LOW);<br> }</p> 
<p>void loop() {<!-- --><br>   // put your main code here, to run repeatedly:<br>   if(digitalRead(BUTTON) == LOW){       //低电平表示按键被按下<br>       digitalWrite(RED, HIGH);<br>       digitalWrite(YELLOW, LOW);<br>       digitalWrite(GREEN, LOW);<br>       delay(5000);<br>       digitalWrite(YELLOW, HIGH);<br>       delay(5000);<br>   }else {                   //按键没有被按下<br>       digitalWrite(RED, LOW);<br>       digitalWrite(YELLOW, LOW);<br>       digitalWrite(GREEN, HIGH);<br>   }    <br> }<br> 

=========================================================================

编号:button_RYG_2

<br> const int RED = 8;        //红色<br> const int YELLOW = 9;     //黄色<br> const int GREEN = 10;     //绿色<br> const int BUTTON = 11;    //按键连接管脚      </p> 
<p><br> void setup() {<!-- --><br>   // put your setup code here, to run once:<br>   pinMode(RED, OUTPUT);<br>   pinMode(YELLOW, OUTPUT);<br>   pinMode(GREEN, OUTPUT);<br>   pinMode(BUTTON, INPUT_PULLUP);<br>   digitalWrite(RED, LOW);<br>   digitalWrite(YELLOW, LOW);<br>   digitalWrite(GREEN, LOW);<br> }</p> 
<p>void loop() {<!-- --><br>   // put your main code here, to run repeatedly:<br>   if(digitalRead(BUTTON) == LOW){       //低电平表示按键被按下<br>       digitalWrite(YELLOW, HIGH);<br>       delay(5000);<br>       digitalWrite(RED, HIGH);<br>       digitalWrite(YELLOW, LOW);<br>       digitalWrite(GREEN, LOW);<br>       delay(5000);<br>       digitalWrite(YELLOW, HIGH);<br>       delay(5000);<br>   }else {                   //按键没有被按下<br>       digitalWrite(RED, LOW);<br>       digitalWrite(YELLOW, LOW);<br>       digitalWrite(GREEN, HIGH);<br>   }    <br> }<br> 

版权声明:本文为CSDN博主「金刚熊猫」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xyysun/article/details/122271206

编号:button_RYG

<br> const int RED = 8;        //红色<br> const int YELLOW = 9;     //黄色<br> const int GREEN = 10;     //绿色<br> const int BUTTON = 11;    //按键连接管脚      </p> 
<p><br> void setup() {<!-- --><br>   // put your setup code here, to run once:<br>   pinMode(RED, OUTPUT);<br>   pinMode(YELLOW, OUTPUT);<br>   pinMode(GREEN, OUTPUT);<br>   pinMode(BUTTON, INPUT_PULLUP);<br>   digitalWrite(RED, LOW);<br>   digitalWrite(YELLOW, LOW);<br>   digitalWrite(GREEN, LOW);<br> }</p> 
<p>void loop() {<!-- --><br>   // put your main code here, to run repeatedly:<br>   if(digitalRead(BUTTON) == LOW){       //低电平表示按键被按下<br>       digitalWrite(RED, HIGH);<br>       digitalWrite(YELLOW, LOW);<br>       digitalWrite(GREEN, LOW);<br>       delay(5000);<br>       digitalWrite(YELLOW, HIGH);<br>       delay(5000);<br>   }else {                   //按键没有被按下<br>       digitalWrite(RED, LOW);<br>       digitalWrite(YELLOW, LOW);<br>       digitalWrite(GREEN, HIGH);<br>   }    <br> }<br> 

=========================================================================

编号:button_RYG_2

<br> const int RED = 8;        //红色<br> const int YELLOW = 9;     //黄色<br> const int GREEN = 10;     //绿色<br> const int BUTTON = 11;    //按键连接管脚      </p> 
<p><br> void setup() {<!-- --><br>   // put your setup code here, to run once:<br>   pinMode(RED, OUTPUT);<br>   pinMode(YELLOW, OUTPUT);<br>   pinMode(GREEN, OUTPUT);<br>   pinMode(BUTTON, INPUT_PULLUP);<br>   digitalWrite(RED, LOW);<br>   digitalWrite(YELLOW, LOW);<br>   digitalWrite(GREEN, LOW);<br> }</p> 
<p>void loop() {<!-- --><br>   // put your main code here, to run repeatedly:<br>   if(digitalRead(BUTTON) == LOW){       //低电平表示按键被按下<br>       digitalWrite(YELLOW, HIGH);<br>       delay(5000);<br>       digitalWrite(RED, HIGH);<br>       digitalWrite(YELLOW, LOW);<br>       digitalWrite(GREEN, LOW);<br>       delay(5000);<br>       digitalWrite(YELLOW, HIGH);<br>       delay(5000);<br>   }else {                   //按键没有被按下<br>       digitalWrite(RED, LOW);<br>       digitalWrite(YELLOW, LOW);<br>       digitalWrite(GREEN, HIGH);<br>   }    <br> }<br> 

版权声明:本文为CSDN博主「金刚熊猫」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xyysun/article/details/122271206

生成海报
点赞 0

金刚熊猫

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

数码管轮播显示

硬件: 数码管10个引脚,8个控制二极管亮、灭,2个引脚为负极。1k欧电阻链接负极,接gnd。 将8个二极管引脚依次链接0-7号引脚。 软件: /*共阳极单数码管显示&