「Arduino」控制LED燈條模擬電池電量顯示

在大多數產品中並沒有電池電量顯示的功能,當我們長時間沒有充電想要充電的時候又不知道電量是否還可以支持工作,這樣就可能導致浪費資源和時間.
我們今天就使用Arduino和LED燈條模塊模擬顯示電池的電量顯示。
材料清單:
Arduino uno R3 板子 *1
USB 數據線 *1
燈條模塊 *1
10K電阻 *2
杜邦線若干
首先硬件電路的連接如下:

「Arduino」控制LED燈條模擬電池電量顯示

Arduino代碼:

/***********************************************************
 File name: _32_LEDBarModule.ino
 Description: This example will show you how to use the setBits()
 function of this library.Set any combination of
 LEDs using 10 bits.
 The setBits() function sets the current state,
 one bit for each LED.First 10 bits from the right
 control the 10 LEDs.
 eg. 0b00000jihgfedcba
 a = LED 1, b = LED 2, c = LED 3, etc.
 dec hex binary
 0 = 0x0 = 0b000000000000000 = all LEDs off
 5 = 0x05 = 0b000000000000101 = LEDs 1 and 3 on, all others off
 341 = 0x155 = 0b000000101010101 = LEDs 1,3,5,7,9 on, 2,4,6,8,10 off
 1023 = 0x3ff = 0b000001111111111 = all LEDs on
 | |
 10 1.
 Website: www.gewbot.com
 Date: 2017/03/17
 ***********************************************************/
 #include

#define PIN7 7
#define PIN8 8
Adeept_Bar bar(PIN7, PIN8); // Clock pin7, Data pin8
double a;
void setup()
{
bar.begin(); // initialize
}
void loop()
{

// Turn on LED 1
bar.setBits(0b000000000000001);// 0b000000000000001 can also be written as 0x1:
delay(500);
// Turn on LED 12
bar.setBits(0b000000000000011);// 0b000000000000011 can also be written as 0x3:
delay(500);
// Turn on LED 123
bar.setBits(0b000000000000111);// 0b000000000000111 can also be written as 0x7:
delay(500);
// Turn on LED 1234
bar.setBits(0b000000000001111);// 0b000000000001111 can also be written as 0xf:
delay(500);
// Turn on LED 12345
bar.setBits(0b000000000011111);// 0b000000000011111 can also be written as 0x1f:
delay(500);
// Turn on LED 123456
bar.setBits(0b000000000111111);// 0b000000000111111 can also be written as 0x3f:
delay(500);
// Turn on LED 1234567
bar.setBits(0b000000001111111);// 0b000000001111111 can also be written as 0x7f:
delay(500);
// Turn on LED 12345678
bar.setBits(0b000000011111111);// 0b000000011111111 can also be written as 0xff:
delay(500);
// Turn on LED 123456789
bar.setBits(0b000000111111111);// 0b000000111111111 can also be written as 0x1ff:
delay(500);
// Turn on all LEDs
bar.setBits(0x3ff);
delay(500);
// Turn on LED 123456789
bar.setBits(0b000000111111111);// 0b000000111111111 can also be written as 0x1ff:
delay(500);
// Turn on LED 12345678
bar.setBits(0b000000011111111);// 0b000000011111111 can also be written as 0xff:
delay(500);
// Turn on LED 1234567
bar.setBits(0b000000001111111);// 0b000000001111111 can also be written as 0x7f:
delay(500);
// Turn on LED 123456
bar.setBits(0b000000000111111);// 0b000000000111111 can also be written as 0x3f:
delay(500);
// Turn on LED 12345
bar.setBits(0b000000000011111);// 0b000000000011111 can also be written as 0x1f:
delay(500);
// Turn on LED 1234
bar.setBits(0b000000000001111);// 0b000000000001111 can also be written as 0xf:
delay(500);
// Turn on LED 123
bar.setBits(0b000000000000111);// 0b000000000000111 can also be written as 0x7:
delay(500);
// Turn on LED 12
bar.setBits(0b000000000000011);// 0b000000000000011 can also be written as 0x3:
delay(500);
// Turn on LED 1
bar.setBits(0b000000000000001);// 0b000000000000001 can also be written as 0x1:
delay(500);

a=(double)(analogRead(A0)-20)/100;//將A0輸入信號轉換為0-1023之間的數值

//判斷a點獲取的電壓值是否在這個區間
if(a>3.0&&a3.5&&a4&&a4.5&&a<5)
{
bar.setBits(0b000000000111111);// 0b000000000000001 can also be written as 0x1:
}
else
{
bar.setBits(0b000000111111111);
}

}


分享到:


相關文章: