本帖最后由 Maker_kun 于 2024-5-29 22:56 编辑
一、乐高套件
刚好这个MOMC拼装玩具——城市车库卷帘门闸门,这个套件有个楼顶放太阳能板,而且是透明的可以看见内部照明,装一个太阳能储能系统刚刚好
二、硬件电路
增加ws2812数控LED和人体传感器,ws2812可以当做照明LED,人体传感器感知有没有人,有人时点亮ws2812数控LED灯
三、程序代码:
// 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
// Which pin on the Arduino is connected to the NeoPixels?
#define PIN 3 // On Trinket or Gemma, suggest changing this to 1
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 8 // Popular NeoPixel ring size
// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals. Note that for older NeoPixel
// strips you might need to change the third parameter -- see the
// strandtest example for more information on possible values.
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels
/* 使用0.96寸的OLED屏幕需要使用包含这个头文件 */
#include "SSD1306Wire.h"
#include <Battery.h>
Battery battery(3400, 4200, 0, 12);
#include "driver/temperature_sensor.h"
#define UNIT_C // C - 摄氏度 F - 华氏度
// 定义温度数据读取变量
temperature_sensor_handle_t temp_sensor = NULL;
temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(10, 50);
// 转换摄氏度到华氏度的函数
float celsiusToFahrenheit(float celsius) {
return (celsius * 9.0 / 5.0) + 32.0;
}
/* 设置oled屏幕的相关信息 */ //有一些引脚接口不支持I2C
const int I2C_ADDR = 0x3c; // oled屏幕的I2c地址
#define SDA_PIN SDA //
#define SCL_PIN SCL //
/* 新建一个oled屏幕对象,需要输入IIC地址,SDA和SCL引脚号 */
SSD1306Wire oled(I2C_ADDR, SDA_PIN, SCL_PIN);
//测试屏幕显示
void drawRect(void) {
for (int16_t i=0; i<oled.getHeight()/2; i+=2) {
oled.drawRect(i, i, oled.getWidth()-2*i, oled.getHeight()-2*i);
oled.display();
delay(50);
}
}
void setup() {
pinMode(23, INPUT);
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
// END of Trinket-specific code.
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
Serial.begin(115200); //串口频率
// 启用内置温度传感器
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor));
ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor));
analogReadResolution(12); // switches ADC resolution to 12 bits
battery.begin(4200, 2, &asigmoidal);
// put your setup code here, to run once:
/* oled屏幕初始化 */
oled.init();
oled.flipScreenVertically(); // 设置屏幕翻转
oled.setContrast(255); // 设置屏幕亮度
drawRect(); // 测试屏幕显示
oled.clear(); oled.display(); // 清除屏幕
for(int i=0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
}
pixels.show();
}
void loop() {
float tsens_value;
// 获取温度值
ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_sensor, &tsens_value));
// put your main code here, to run repeatedly:
/* 显示字母 */
oled.setFont(ArialMT_Plain_16); // 设置字体
oled.clear();
//oled.display();
oled.drawString(0,0, "Voltage:" +String(battery.voltage())+"mV"); // 将要显示的字母写入缓存
oled.drawString(0,20, "POWER:"+String(battery.level())+"%"); // 将要显示的字母写入缓存
oled.drawString(0,40, "Temp:"+String(tsens_value)+"`C"); // 将要显示的字母写入缓存
oled.display(); // 将缓存里的文字在屏幕上显示
delay(50);
if(digitalRead(23))
{
for(int j=0; j<255; j++)
{
for(int i=0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(j, j, j));
}
pixels.show();
delay(20);
}
delay(5000);
for(int j=255; j<1; j--)
{
for(int i=0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(j, j, j));
}
pixels.show();
delay(20);
}
for(int i=0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
}
pixels.show();
}
}
实现呼吸灯效果:
四、组装效效果
视频展示:
储能感应负载LED演示