【arduino】LCD1602屏幕动画

该博客介绍了如何使用Arduino UNO和LCD1602屏幕通过I2C接口实现动画效果。实验中详细说明了硬件连接方式,并提供了相应的代码实现两种不同的动画:一种基于字符滚动,另一种基于图形变化。代码中定义了电池电压监测和屏幕显示的函数,展示了在Arduino平台上进行嵌入式图形编程的能力。
摘要由CSDN通过智能技术生成

【arduino】LCD1602屏幕动画

一、实验器材

UNO控制板:1块
LCD1602带解码器(PCF8574)显示屏
杜邦线若干

二、连线示意图

GND接–GND
VCC接–VCC
SDA接–A4
SCL接–A5
实物接线图如下,图片是博主网上找的
在这里插入图片描述

三、代码如下

    #include <Wire.h>  //加载Wire通讯库
    #include <LiquidCrystal_I2C.h>  //加载液晶屏库
    LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // 设 LCD的地址为0x27,16列2行

    const int batteries = 3;
    const int batteryPin[] = {A0, A2, A6, A4};
    const int basePin[] = {A1, A3, A7, A5};
    const float R[] = {10.0, 10.0, 10.0, 10.0};
    const int ledPin[]= {9, 8, 7, 6};
    const float stopVoltage = 0.9;
    const float resetVoltage = 0.1;
    const int batteryOn = 1;
    const int batteryStop = 0;
    const int batteryReset = -1;
    const float referenceVoltage = 5.00;
    
    

    float batteryVoltage[3] = {0.0, 0.0, 0.0};  // volts
    float baseVoltage[3] = {0.0, 0.0, 0.0}; // volts
    float capacity[3] = {0L, 0L, 0L};  // mAh
    float energy[3] = {0L, 0L, 0L};  // mWh
    unsigned long previousTime = 0L;  // milliseconds
    unsigned long currentTime = 0L;  // milliseconds
    int batteryStatus[3] = {batteryReset, batteryReset, batteryReset};

    float latestViableCurrent[3] = {0.0, 0.0, 0.0}; // this is temporary
    unsigned long lastUpdate = 0L;
    long animationStep = -200;
    byte cursorLCD = 15;
    int numberOfColumns = 16;
    long randomVariable = 0;
    boolean waitForAnimationDelay = true;
    int printCycle = 0;
    
    void animationSetup() {
      for (int i=0; i < 8; i++) {
        byte charLine[8];
        for (int j=0; j < 8; j++) {
          if (j > i) charLine[7-j]=B00000;
          else charLine[7-j]=B11111;
        }
        lcd.createChar(i, charLine);
      }
    }

    void setup() {
      Serial.begin(9600);
      lcd.init();
      lcd.backlight();
      animationSetup();
    }

    void animation1() {
      lcd.setCursor(14,0);
      lcd.write(byte(animationStep % 8));
      // lcd.write(7);
      animationStep++;
    }    
    
    void animation3() {
      byte character[2];
      for (int column = 0; column < numberOfColumns; column++) {
        int actualValue=cos(animationStep*(0.1+column*0.01))*8.5+9;
        if (actualValue == 17) {
          character[0]=byte(255);
          character[1]=byte(255);
        }
        else if (actualValue > 9) {
          character[0]=byte(actualValue-10);
          character[1]=byte(255);
        }
        else if (actualValue == 9) {
          character[0]=byte(32);
          character[1]=byte(255);
        }
        else if (actualValue == 8) {
          character[0]=byte(32);
          character[1]=byte(255);
        }
        else if (actualValue == 0) {
          character[0]=byte(32);
          character[1]=byte(32);
        }      
        else {
          character[0]=byte(32);
          character[1]=byte(actualValue-1);
        }
        lcd.setCursor(15-column,0);
        lcd.write(character[0]);
        lcd.setCursor(15-column,1);
        lcd.write(character[1]);
        }
      // lcd.write(byte(animationStep % 8));
      // lcd.write(7);
      animationStep++;
    }
    
    void loop() {
      animation3();
    }
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习的小莲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值