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

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

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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);

}

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

你可能感兴趣的:(mixly库,米思齐,esp8266,c++,arduino,c语言,Mixly)