用 1602实现进度条
本帖最后由 zoologist 于 2014-10-27 08:29 编辑在 https://www.electronicsblog.net/arduino-lcd-horizontal-progress-bar-using-custom-characters/ 这里发现比较有趣的代码:用1602LCD 实现一个进度条。根据文章指引,我也试验了一下。
弄明白了原理,程序非常简单:
//https://www.electronicsblog.net/
//Arduino LCD horizontal progress bar using custom characters
#include <Wire.h>
#include "LiquidCrystal_I2C.h"
#define lenght 16.0
double percent=100.0;
unsigned char b;
unsigned int peace;
int value=100;
// custom charaters
LiquidCrystal_I2C lcd(0x27,16,2);
//定义进度块
byte p1 = {
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10};
byte p2 = {
0x18,
0x18,
0x18,
0x18,
0x18,
0x18,
0x18,
0x18};
byte p3 = {
0x1C,
0x1C,
0x1C,
0x1C,
0x1C,
0x1C,
0x1C,
0x1C};
byte p4 = {
0x1E,
0x1E,
0x1E,
0x1E,
0x1E,
0x1E,
0x1E,
0x1E};
byte p5 = {
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F};
void setup() {
lcd.init(); //初始化LCD
lcd.backlight(); //打开背光
//将自定义的字符块发送给LCD
//P1 是第一个,P2 是第二个,以此类推
lcd.createChar(0, p1);
lcd.createChar(1, p2);
lcd.createChar(2, p3);
lcd.createChar(3, p4);
lcd.createChar(4, p5);
}
void loop()
{
//设置光标在左上角
lcd.setCursor(0, 0);
percent = value/1024.0*100.0;
//当超过100%的时候自动校正为 100%
if (percent>100) {percent=1;value=0;}
lcd.print(" ");
lcd.print(percent);
lcd.print(" % ");
//移动光标到第二行
lcd.setCursor(0,1);
double a=lenght/100*percent;
// drawing black rectangles on LCD
// 显示全黑块。
if (a>=1) {
for (int i=1;i<a;i++) {
lcd.write(4);
b=i;
} //for (int i=1;i<a;i++)
a=a-b;
}
else {b=0;}
peace=a*5;
// drawing charater's colums
// 显示除去全黑块之后的零头
switch (peace) {
case 0:
break;
case 1:
lcd.write(0);
break;
case 2:
lcd.write(1);
break;
case 3:
lcd.write(2);
break;
case 4:
lcd.write(3);
break;
} //switch (peace)
// clearing line
// 用空格填充剩下的位置
for (int i =0;i<(lenght-b);i++) {
lcd.print(" ");
}
//递增
value=value+10;
delay(300);
}
连接
大图
完整代码下载
附件已经修复
本帖最后由 xinhoujue 于 2014-10-24 13:37 编辑
好资料,谢谢分享啦~~~~~~~~~~~~晕,代码包无法下载?????? 代码无法下砸啊 本帖最后由 弘毅 于 2014-10-26 19:32 编辑
再传一次试试看 好东西,想到一个玩法,两块1602,加上三轴传感器,可以显示体感状态。谢谢分享。
zoologist 发表于 2014-10-25 07:59 static/image/common/back.gif
再传一次试试看
还是下载不了,是不是和弘毅联系一下。 求帮忙重新传一下附件。。。。论坛打了补丁后。。附件上传功能出现故障,现在已经修复了。。。 谢谢分享。 好资料,谢谢分享。 點子不錯,繼續努力
页:
[1]