Adafruit_GFX matrix ws2812像素屏库使用教程AWTRIX2.0像素时钟

6 篇文章 2 订阅
6 篇文章 2 订阅

AWTRIX2.0像素时钟很炫酷但必须要与服务器配合使用。这个库可以做自己的点阵时钟离线版。想怎么玩就怎么玩不受服务器牵绊。
第一步:下载mixy库然后倒入,必须有以下库文件: Adafruit_GFX FastLED FastLED_NeoMatrix TomThumb
#include <LightDependentResistor.h>//LDR光敏电阻
#include <Adafruit_GFX.h>
#include <FastLED.h>
#include <FastLED_NeoMatrix.h>
#include <Fonts/TomThumb.h> //字体库
特别说明TomThumb 字体必须用下载包里的字体,用原Adafruit_GFX字体效果很难看。
在这里插入图片描述
上测试时效果图:
在这里插入图片描述
需要的硬件:
WS2812软点阵屏 32X8 256灯珠。
在这里插入图片描述
以上全部齐全后即可开整!先看库使用程序图。一看即懂。
垂直线:
在这里插入图片描述
水平线
在这里插入图片描述
画空心矩形
在这里插入图片描述
显示效果
在这里插入图片描述
简单NTP时钟演示程序图
在这里插入图片描述
代码:

#include <LightDependentResistor.h>
#include <ESP8266WiFi.h>
#include <TimeLib.h>
#include <NtpClientLib.h>
#include <Adafruit_GFX.h>
#include <FastLED.h>
#include <FastLED_NeoMatrix.h>
#include <Fonts/TomThumb.h>

volatile int texiao;
String lnnmonth;
String lnnday;
String huor;
String feng;
String seconds;
volatile int ldrbr;
int8_t timeZone = 8;
const PROGMEM char *ntpServer = "ntp1.aliyun.com";
volatile int item;

LightDependentResistor myldr(A0,1000,LightDependentResistor::GL5516);

CRGB leds[256];
FastLED_NeoMatrix *matrix;
void sysloop() {
  matrix->setTextColor(matrix->Color(0,200,160));
  matrix->setCursor(0,6);
  matrix->print(String(String(huor) + String(":")) + String(feng));
  matrix->setCursor(18,8);
  matrix->print(".......");
  matrix->setTextColor(matrix->Color(0,200,160));
  matrix->setCursor(25,6);
  matrix->print(lnnday);
  switch (NTP.getDateWeekday()) {
   case 1:
    matrix->setTextColor(matrix->Color(50,255,50));
    matrix->setCursor(18,8);
    matrix->print(".");
    matrix->show();
    break;
   case 2:
    matrix->setTextColor(matrix->Color(50,255,50));
    matrix->setCursor(20,8);
    matrix->print(".");
    matrix->show();
    break;
   case 3:
    matrix->setTextColor(matrix->Color(50,255,50));
    matrix->setCursor(22,8);
    matrix->print(".");
    matrix->show();
    break;
   case 4:
    matrix->setTextColor(matrix->Color(50,255,50));
    matrix->setCursor(24,8);
    matrix->print(".");
    matrix->show();
    break;
   case 5:
    matrix->setTextColor(matrix->Color(50,255,50));
    matrix->setCursor(26,8);
    matrix->print(".");
    matrix->show();
    break;
   case 6:
    matrix->setTextColor(matrix->Color(50,255,50));
    matrix->setCursor(28,8);
    matrix->print(".");
    matrix->show();
    break;
   case 7:
    matrix->setTextColor(matrix->Color(50,255,50));
    matrix->setCursor(30,8);
    matrix->print(".");
    matrix->show();
    break;
   default:
    break;
  }
  myfillRect(19, 1, 5, 5, matrix->Color((random(50, 255)),(random(50, 255)),(random(50, 255))));
  mydrawCircle(21, 3, 1, matrix->Color((random(50, 255)),(random(50, 255)),(random(50, 255))));
  for(int i = 2; i <= 12; i++)
  {
    matrix->setTextColor(matrix->Color((random(50, 255)),(random(50, 255)),(random(50, 255))));
    matrix->setCursor((i + 1),8);
    matrix->print(".");
    matrix->show();
    delay(500);
    if (i == 12) {
      texiao = texiao + 1;
      if (texiao >= 5) {
        texiao = 1;

      }

    }
  }
  Serial.println(NTP.getDateYear());
  item = random(1, 5);
  Serial.println(String("1=") + String(item));
  switch (item) {
   case 1:
    for(int j = -3; j<= 33; j++)
    {
      matrix->clear();
      mydrawFastVLine(j + 1, 0, 8, matrix->Color(0,200,160));
      delay(15);
    }
    break;
   case 2:
    for(int j = 32; j>-3; j--)
    {
      matrix->clear();
      mydrawFastVLine(j + 1, 0, 8, matrix->Color(0,200,160));
      delay(15);
    }
    break;
   case 3:
    for(int j = -3; j<=10; j++)
    {
      matrix->clear();
      mydrawFastHLine(0, j + 1, 32, matrix->Color(100,255,100));
      delay(30);
    }
    break;
   case 4:
    for(int j = 10; j>-3; j--)
    {
      matrix->clear();
      mydrawFastHLine(0, j + 1, 32, matrix->Color(100,255,100));
      delay(30);
    }
    break;
   default:
    break;
  }
  for(int j = 7; j<= 17; j++)
  {
    matrix->clear();
    matrix->setTextColor(matrix->Color(50,255,50));
    matrix->setCursor((j + 1),8);
    matrix->print(".");
    matrix->setTextColor(matrix->Color(50,50,200));
    matrix->setCursor(7,6);
    matrix->print(String(String(lnnmonth) + String("-")) + String(lnnday));
    matrix->show();
    delay(400);
  }
  item = random(1, 5);
  Serial.println(item);
  switch (item) {
   case 1:
    for(int j = -3; j<= 33; j++)
    {
      matrix->clear();
      mydrawFastVLine(j + 1, 0, 8, matrix->Color(0,200,160));
      delay(15);
    }
    break;
   case 2:
    for(int j = 32; j>-3; j--)
    {
      matrix->clear();
      mydrawFastVLine(j + 1, 0, 8, matrix->Color(0,200,160));
      delay(15);
    }
    break;
   case 3:
    for(int j = -3; j<=10; j++)
    {
      matrix->clear();
      mydrawFastHLine(0, j + 1, 32, matrix->Color(100,255,100));
      delay(30);
    }
    break;
   case 4:
    for(int j = 10; j>-3; j--)
    {
      matrix->clear();
      mydrawFastHLine(0, j + 1, 32, matrix->Color(100,255,100));
      delay(30);
    }
    break;
   default:
    break;
  }
}

void mydrawCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color) {
  matrix->drawCircle(x0,y0,r,color);
  matrix->show();
}

void mydrawFastHLine(int x, int y, int w, int color) {
  matrix->drawFastHLine(x,y,w,color);
  matrix->show();
}

void mydrawFastVLine(int x, int y, int h, int color) {
  matrix->drawFastVLine(x,y,h,color);
  matrix->show();
}

void myfillRect(int x, int y, int w, int h, int color) {
  matrix->fillRect(x,y,w,h,color);
  matrix->show();
}

void date_time() {
  if (NTP.getTimeHour24() >= 0 && NTP.getTimeHour24() <= 9) {
    huor = String("0") + String(NTP.getTimeHour24());

  } else {
    huor = NTP.getTimeHour24();

  }
  if (NTP.getTimeMinute() >= 0 && NTP.getTimeMinute() <= 9) {
    feng = String("0") + String(NTP.getTimeMinute());

  } else {
    feng = NTP.getTimeMinute();

  }
  if (NTP.getTimeSecond() >= 0 && NTP.getTimeSecond() <= 9) {
    seconds = String("0") + String(NTP.getTimeSecond());

  } else {
    seconds = NTP.getTimeSecond();

  }
  if (NTP.getDateMonth() >= 0 && NTP.getDateMonth() <= 9) {
    lnnmonth = String("0") + String(NTP.getDateMonth());

  } else {
    lnnmonth = NTP.getDateMonth();

  }
  if (NTP.getDateDay() >= 0 && NTP.getDateDay() <= 9) {
    lnnday = String("0") + String(NTP.getDateDay());

  } else {
    lnnday = NTP.getDateDay();

  }
}

void inithaed() {
  matrix->clear();
  matrix->setTextColor(matrix->Color((random(50, 250)),(random(50, 255)),(random(50, 250))));
  matrix->setCursor(5,6);
  matrix->print("MATRIX");
  matrix->show();
}

void setup(){
  texiao = 0;
  lnnmonth = "";
  lnnday = "";
  huor = "";
  feng = "";
  seconds = "";
  ldrbr = 0;
  Serial.begin(9600);
  myldr.setPhotocellPositionOnGround(false);
  WiFi.begin("Redmi_2.4G", "12345678");//Redmi_2.4G= 路由器ssid  12345678= 路由器密码。请改成你自己的ssid 密码
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  NTP.setInterval (600);
  NTP.setNTPTimeout (1500);
  NTP.begin (ntpServer, timeZone, false);
  matrix = new FastLED_NeoMatrix(leds, 32, 8, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG);
  FastLED.addLeds<NEOPIXEL,4>(leds,256).setCorrection(TypicalLEDStrip);
  matrix->begin();
  matrix->setTextWrap(false);//显示字体
  matrix->setFont(&TomThumb);//字体
  matrix->setBrightness(5);
  inithaed();
  delay(2000);
  item = 0;
}

void loop(){
  //date_loop();
  date_time();
  randomSeed(millis());
  ldrbr = (map(myldr.getCurrentLux(), 0, 800, 5, 50));
  matrix->setBrightness(ldrbr);
  matrix->clear();
  sysloop();
  matrix->show();
  delay(50);

}

有问题或有需要改模块功能的 可留言

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

琢磨侠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值