友情提示点击顶部放大镜 可以使用站内搜索 记住我们的地址 www.hainabaike.com
用 Arduino 和轻触开关实现最简单的“拨动开关”(可以保持和切换开关状态),为了便于理解,没有多余的元件,这里只实现一个最简单的开关来控制LED。
效果如上图。下面是接线图和源代码。
/*********************
Simple toggle switch
Created by: P.Agiakatsikas
*********************/
int button = 8;
int led = 13
int status = false;
void setup(){
pinMode(led, OUTPUT);
pinMode(button, INPUT_PULLUP); // set the internal pull up resistor, unpressed button is HIGH
}
void loop(){
//a) if the button is not pressed the false status is reversed by !status and the LED turns on
//b) if the button is pressed the true status is reveresed by !status and the LED turns off
if (digitalRead(button) == true) {
status = !status;
digitalWrite(led, status);
} while(digitalRead(button) == true);
delay(50); // keeps a small delay
}
标签: level2教程arduino开关按键开led按键开关轻触开关拨动开关
文章来源:
Arduino 实验室
版权声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除。



评论列表