光照指示器
Language | English |
---|
目的做一个的光照指示器,将光值分为三个等级,通过绿、蓝、红三种颜来反应光的强弱。 原理通过Light光线传感器检测光的强弱,系统判断光强在定义的哪个范围区间,对应让彩灯亮不同颜色,同时,在同一区间内光越强,颜色越亮。 设备
准备
调试
#include <Adafruit_NeoPixel.h>
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);
#define Light_PIN A0
#define Light_value1 400
#define Light_value2 800
int sensorValue;
void setup() {
Serial.begin(115200);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
sensorValue = analogRead(Light_PIN);
Serial.println(sensorValue);
if (sensorValue < Light_value1)
colorWipe(strip.Color(0, map(sensorValue, 10, 400, 0, 255), 0));
else if (sensorValue >= Light_value1 && sensorValue < Light_value2)
colorWipe(strip.Color(0, 0, map(sensorValue, 400, 800, 0, 255)));
else if (Light_value2 >= 800)
colorWipe(strip.Color(map(sensorValue, 800, 960, 0, 255), 0, 0));
}
void colorWipe(uint32_t c) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
}
}
改变光的环境,光从暗到亮颜色依次变化为绿-蓝-红,并且光越弱,亮度越低。 程序说明
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);
#define Light_PIN A0
#define Light_value1 400
#define Light_value2 800
sensorValue = analogRead(Light_PIN);
if (sensorValue < Light_value1)
colorWipe(strip.Color(0, map(sensorValue, 10, 400, 0, 255), 0));
else if (sensorValue >= Light_value1 && sensorValue < Light_value2)
colorWipe(strip.Color(0, 0, map(sensorValue, 400, 800, 0, 255)));
else if (Light_value2 >= 800)
colorWipe(strip.Color(map(sensorValue, 800, 960, 0, 255), 0, 0));
视频 |