怎样用按钮切换RGBLED的多种颜色

电子说

1.2w人已加入

描述

步骤1:工具和材料

Arduino 101或Arduino Uno

面包板

按钮开关

RGB LED

3个100Ω电阻器

跳线

步骤2:电路

将Arduino电源连接到面包板

连接3.3V引脚将Arduino连接到面包板的红色电源轨。

将Arduino的GND引脚连接到面包板的黑色电源轨。

连接按钮

将一个10KΩ电阻从按钮开关的一个引脚连接到面包板的红色电源轨。

将上面的相同引脚连接到Arduino的数字引脚3。

将同一侧的另一个引脚连接到面包板的接地导轨。

最后,将RGB LED连接到Arduino。

将RGB的最长引脚连接到面包板的公共接地导轨。

将剩余的三个引脚连接到一个100Ω电阻,串联到引脚9,10,分别为11。

第3步:代码

按钮

const int buttonPin = 3;

//RGB LED pins const int redPin = 11; const int greenPin = 10; const int bluePin = 9; //create a variable to store a counter and set it to 0 int counter = 0; void setup() { // Set up the pushbutton pins to be an input: pinMode(buttonPin, INPUT); // Set up the RGB pins to be an outputs: pinMode(redPin, OUTPUT); pinMode(greenPin,OUTPUT); pinMode(bluePin,OUTPUT); }

步骤4:演示

当我按下按钮时,它首先切换为红色,然后是绿色,然后是蓝色,最后是关闭。这只循环显示所有颜色,因为只按下了一个按钮!

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分