NeoPixels LED燈條+PIL人體感應 實習(更新-加入省電模式)
NeoPixels LED燈條
材料
接線方式
三條線其實很容易
VCC 5V+
DI PIN
GND -
必須要匯入adafruit的程式庫
存放在lib裡
程式碼
#include <Adafruit_NeoPixel.h>
#define pin 6
#define numpixel 8
Adafruit_NeoPixel pixels=Adafruit_NeoPixel(numpixel, pin, NEO_GRB + NEO_KHZ800);
void setup() {
pixels.begin();
}
void loop() {
//pixels.clear();
for (int i = 0; i < numpixel; i++)
{
pixels.setPixelColor(i, pixels.Color(150, 245, 120));
pixels.show();
delay(100);
}
for (int i = numpixel; i > 0; i--)
{
pixels.setPixelColor(i, pixels.Color(125, 250, 50));
pixels.show();
delay(100);
}
}
加上PIL人體感應
這裡要注意一下,如果是要用來正式啓用,用9v電池可能一樣不太穩定
我拿18650電池一顆開機待機狀態和不時去感應看看,狀況良好。
程式碼
#include <Adafruit_NeoPixel.h>
#define pin 6
#define PIL 4
#define numpixel 8
byte val;
Adafruit_NeoPixel pixels=Adafruit_NeoPixel(numpixel, pin, NEO_GRB + NEO_KHZ800);
void OnLED(){
for (int i = 0; i < numpixel; i++)
{
pixels.setPixelColor(i, pixels.Color(150, 45, 250));
pixels.show();
delay(10);
}
}
void OffLED(){
for (int i = 0; i < numpixel; i++)
{
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show();
delay(10);
}
}
void setup() {
pixels.begin();
pixels.clear();
}
void loop() {
val=digitalRead(PIL);
if (val==HIGH){
OnLED();
delay(15000);
}
else
{
OffLED();
}
}
(更新)加入省電模式
下載Enerlib庫
#include <Enerlib.h>
#include <Adafruit_NeoPixel.h>
Energy energy;
const int PIR = 2;
const int pin = 8;
const int numpixel = 8;
int var;
Adafruit_NeoPixel pixels=Adafruit_NeoPixel(numpixel, pin, NEO_GRB + NEO_KHZ800);
byte times = 5; // 記錄執行次數
volatile byte state = 0; // 暫存執行狀態
void wakeISR() {
if (energy.WasSleeping()) {
state = 1;
} else {
state = 2;
}
}
void OnLED(){
for (int i = 0; i < numpixel; i++)
{
pixels.setPixelColor(i, pixels.Color(170, 150, 50));
pixels.show();
delay(10);
}
}
void OffLED(){
for (int i = 0; i < numpixel; i++)
{
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show();
delay(10);
}
}
void setup(){
pixels.begin();
pixels.clear();
Serial.begin(9600);
pinMode(PIR, INPUT);
attachInterrupt(0, wakeISR, CHANGE); // 附加中斷服務常式
Serial.println("Running...");
}
void loop(){
if (state == 1) {
Serial.println("Was sleeping...");
} else if (state == 2) {
Serial.println("Was awake...");
}
state = 0;
var = digitalRead(PIR);
if (var == HIGH){
OnLED();
delay(10000);
}else{
OffLED();
times++;
delay(times);
Serial.println(times);
}
if (times > 5) {
times = 0;
Serial.println("Go to sleep...");
energy.PowerDown();
}
}
留言
張貼留言