软硬结合工程师的自我修炼——WS2812b的测试程序
程序说明:按照锂电池电压大小设置呼吸灯速率。电压越大,呼吸速率越慢。
//======================================================================
//
// Copyright (C) 2020 HKUST_AANTC
// All rights reserved.
//
// filename : WS2812b_demo
// description : WS2812b_demo
// PCB : BMP_Control_Board
// IDE : Arduino IDE 1.8.12
//
// Created by Frank at 19/03/2020 20:00:00
// https://lynn1996.github.io/
//
// Libraries copyright notice:
//
// [1]
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// Released under the GPLv3 license to match the rest of the
// Adafruit NeoPixel library
//
//======================================================================
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#include <Wire.h>
#define WS2812_PIN 13
#define NUMPIXELS 1
#define InterruptPin 14 //接收LTC2980关断要求,低电平有效
#define SendCloseCommandPin 12 //接收关断要求并处理好信息准备关断后,该pin向LTC2980发送关断指令,低电平有效
Adafruit_NeoPixel pixels(NUMPIXELS, WS2812_PIN, NEO_GRB + NEO_KHZ800);
#define DELAYVAL 1000 // Time (in milliseconds) to pause between pixels
void LED_FadeDown(int R, int G, int B, int FadeSpeed, int Brightness)
{
// for (int i = 0; i < NUMPIXELS; i++) { pixels.setPixelColor(i, 200, 0, 0); }
pixels.setPixelColor(0, R, G, B);
for (int j = Brightness; j > 0; j = j - 2)
{
pixels.setBrightness(j);
pixels.show();
delay(FadeSpeed);
}
}
void LED_FadeUp(int R, int G, int B, int FadeSpeed, int Brightness)
{
// for (int i = 0; i < NUMPIXELS; i++) { pixels.setPixelColor(i, 200, 0, 0); }
pixels.setPixelColor(0, R, G, B);
for (int j = 2; j < Brightness; j = j + 2)
{
pixels.setBrightness(j);
pixels.show();
delay(FadeSpeed);
}
}
//--------------------------------关断callback函数------------------------------------
ICACHE_RAM_ATTR void shutDown() {
digitalWrite(SendCloseCommandPin, LOW);
ESP.restart();//必须加restart才能正常关断也不知是啥bug T_T...
}
//--------------------------------setup-----------------------------------------------
void setup() {
Serial.begin(500000);
//Serial.println();
Wire.begin();
pinMode(SendCloseCommandPin, OUTPUT);
digitalWrite(SendCloseCommandPin, HIGH);
pinMode(InterruptPin, INPUT_PULLUP); //设置关断pin
attachInterrupt(digitalPinToInterrupt(InterruptPin), shutDown, FALLING);//注册关断callback函数
pixels.begin();
}
//--------------------------------main loop----------------------------------------------
void loop() {
float remainingBatteryCapacity = 0.005732 * analogRead(A0);
uint8_t fadeSpeed = 50 * (1 - (4.28 - remainingBatteryCapacity) / (4.28 - 3.6));
Serial.print(remainingBatteryCapacity);
Serial.print(" ");
Serial.println(fadeSpeed);
LED_FadeUp(255, 0, 0, fadeSpeed, 50);
LED_FadeDown(255, 0, 0, fadeSpeed, 50);
LED_FadeUp(0, 255, 0, fadeSpeed, 50);
LED_FadeDown(0, 255, 0, fadeSpeed, 50);
LED_FadeUp(0, 0, 255, fadeSpeed, 50);
LED_FadeDown(0, 0, 255, fadeSpeed, 50);
}
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!评论系统采用disqus,国内用户可尝试科学上网。