您的位置: 主页>机器人编程>ROBOLAB编程>Arduino控制LED灯条模拟电池电量显示
加盟酷吧机器人 培养孩子核心能力

Arduino控制LED灯条模拟电池电量显示

Arduino控制LED灯条显示电池电量

在大多数产品中并没有电池电量显示的功能,当我们长时间没有充电想要充电的时候又不知道电量是否还可以支持工作,这样就可能导致浪费资源和时间.

我们今天就使用Arduino和LED灯条模块模拟显示电池的电量显示。

Arduino控制LED灯条模拟电池电量显示

我们需要的材料清单如下:

Arduino uno R3 板子 *1

USB 数据线     *1

灯条模块     *1

10K电阻      *2

杜邦线若干 

首先硬件电路的连接如下:

Arduino控制LED灯条模拟电池电量显示Arduino控制LED灯条模拟电池电量显示

实验所用的代码如下:

(需要代码详细解读可观看下面链接的视频教程)

/***********************************************************Filename: _32_LEDBarModule.inoDescription: This example will show youhow to use the setBits()function of this library.Set anycombination ofLEDs using 10 bits.The setBits() function sets thecurrent state,one bit for each LED.First 10 bits from therightcontrol the 10 LEDs.eg. 0b00000jihgfedcbaa = LED 1, b = LED 2,c = LED 3, etc.dec hex binary0 = 0x0 = 0b000000000000000 = all LEDsoff5 = 0x05 = 0b000000000000101 = LEDs 1 and 3 on, all othersoff341 = 0x155 = 0b000000101010101 = LEDs 1,3,5,7,9 on, 2,4,6,8,10off1023 = 0x3ff = 0b000001111111111 = all LEDs on| |10 1.Website:www.gewbot.comDate:2017/03/17***********************************************************/#include#define PIN7 7#define PIN8 8Adeept_Bar bar(PIN7, PIN8); // Clockpin7, Data pin8double a;void setup(){bar.begin(); //initialize}void loop(){// Turn on LED1bar.setBits(0b000000000000001);// 0b000000000000001 can also bewritten as 0x1:delay(500);// Turn on LED12bar.setBits(0b000000000000011);// 0b000000000000011 can also bewritten as 0x3:delay(500);// Turn on LED123bar.setBits(0b000000000000111);// 0b000000000000111 can also bewritten as 0x7:delay(500);// Turn on LED1234bar.setBits(0b000000000001111);// 0b000000000001111 can also bewritten as 0xf:delay(500);// Turn on LED12345bar.setBits(0b000000000011111);// 0b000000000011111 can alsobe written as 0x1f:delay(500);// Turn on LED123456bar.setBits(0b000000000111111);// 0b000000000111111 can alsobe written as 0x3f:delay(500);// Turn on LED1234567bar.setBits(0b000000001111111);// 0b000000001111111 can alsobe written as 0x7f:delay(500);// Turn on LED12345678bar.setBits(0b000000011111111);// 0b000000011111111 canalso be written as 0xff:delay(500);// Turn on LED123456789bar.setBits(0b000000111111111);// 0b000000111111111 canalso be written as 0x1ff:delay(500);// Turn on allLEDsbar.setBits(0x3ff);delay(500);// Turn on LED123456789bar.setBits(0b000000111111111);// 0b000000111111111 canalso be written as 0x1ff:delay(500);// Turn on LED12345678bar.setBits(0b000000011111111);// 0b000000011111111 canalso be written as 0xff:delay(500);// Turn on LED1234567bar.setBits(0b000000001111111);// 0b000000001111111 can alsobe written as 0x7f:delay(500);// Turn on LED123456bar.setBits(0b000000000111111);// 0b000000000111111 can alsobe written as 0x3f:delay(500);// Turn on LED12345bar.setBits(0b000000000011111);// 0b000000000011111 can alsobe written as 0x1f:delay(500);// Turn on LED1234bar.setBits(0b000000000001111);// 0b000000000001111 can also bewritten as 0xf:delay(500);// Turn on LED123bar.setBits(0b000000000000111);// 0b000000000000111 can also bewritten as 0x7:delay(500);// Turn on LED12bar.setBits(0b000000000000011);// 0b000000000000011 can also bewritten as 0x3:delay(500);// Turn on LED1bar.setBits(0b000000000000001);// 0b000000000000001 can also bewritten as0x1:delay(500);a=(double)(analogRead(A0)-20)/100;//将A0输入信号转换为0-1023之间的数值//判断a点获取的电压值是否在这个区间if(a>3.0&&a<3.5){bar.setBits(0b000000000000001);//0b000000000000001 can also be written as 0x1:}else if(a>3.5&&a<4){bar.setBits(0b000000000000111);//0b000000000000001 can also be written as 0x1:}elseif(a>4&&a<4.5){bar.setBits(0b000000000011111);//0b000000000000001 can also be written as 0x1:}elseif(a>4.5&&a<5){bar.setBits(0b000000000111111);//0b000000000000001 can also be written as0x1:}else{bar.setBits(0b000000111111111);}}

将上面代码烧入Arduino板子后我们将得到以下的结果

Arduino控制LED灯条模拟电池电量显示

更多的创意等待着你挖掘哦 

后记,小编朋友公司研发了一个游戏化的少儿编程在线课程(5-12岁),游戏化教学结合scratch(一款在线少儿编程工具,类似乐高的积木拼搭),我家娃娃学了几次课,非常喜欢(超预期),16次课才200多块钱,对锻炼孩子的思维能力和动手动力很有帮助。

欧美思机器人教育加盟 赚钱好项目
上一篇:活动通知 | Arduino仿生机器人创意大赛
下一篇:用乐高积木来讲故事,这个创意很棒!
您可能喜欢
回到顶部