基础任务1:控制板载炫彩LED,跑马灯点亮和颜色变换
该任务比较简单,使用这个用Adafruit_NeoPixel.h这个库就能实现,设置了七种不同的颜色进行循环切换,
程序流程图
代码如下:
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#define PIN 8
#define NNUMPIN 10
Adafruit_NeoPixel pixels(NNUMPIN, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
// write your initialization code here
pixels.begin();
}
void loop() {
// write your code here
static uint8_t led_num = 0;
//static uint32_t colors[8] = {0x040304, 0x000505, 0x050005, 0x050500, 0x050005, 0x000A00, 0x0A0000,0x0A000A};
static uint32_t colors[7] = {0x0A0000, 0x000A00, 0x00000A, 0x050500, 0x050005, 0x000505, 0x040304};
//static uint32_t colors[8] = {0x011011, 0x22022, 0x033033, 0x44044, 0x055055, 0x66066, 0x77777,0x088088};
static uint8_t color_num = 0;
static Adafruit_NeoPixel pixels(NNUMPIN, PIN, NEO_GRB + NEO_KHZ800);
pixels.clear();
pixels.setPixelColor(led_num, colors[color_num]);
pixels.show();
led_num++;
if (led_num == NNUMPIN) {
led_num = 0;
color_num++;
if (color_num == 8)
color_num = 0;
}
delay(200);
}
基础任务2:监测环境温度和光线,通过板载LED展示舒适程度
参考官方发布的原理图和CircuitPython库的代码可以得出引脚电压(A8)与温度、光照(A9)之间的关系。
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include "SensorAB.h"
#define PIN 8
#define NUMPIXELS 10
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
// write your initialization code here
Serial.begin(115200);
pixels.begin();
}
void loop() {
// write your code here
const double temperature = get_temperature(A9);
const double photocell = get_photocell(A8);
pixels.clear();
if (temperature < 14) {
pixels.setPixelColor(5, 0, 0, 10);
} else if (temperature < 18 && temperature >= 14) {
pixels.setPixelColor(6, 0, 5, 5);
} else if (temperature >= 18 && temperature <= 20) {
pixels.setPixelColor(7, 0, 10, 0);
} else if (temperature > 20 && temperature <= 25) {
pixels.setPixelColor(8, 5, 5, 0);
} else if (temperature > 25) {
pixels.setPixelColor(9, 10, 0, 0);
}
if (photocell > 1000) {
pixels.setPixelColor(0, 10, 0, 0);
} else if (photocell > 500 && photocell <= 1000) {
pixels.setPixelColor(1, 5, 5, 0);
} else if (photocell >= 200 && photocell <= 500) {
pixels.setPixelColor(2, 0, 10, 0);
} else if (photocell >= 50 && photocell < 200) {
pixels.setPixelColor(3, 0, 5, 5);
} else if (photocell < 50) {
pixels.setPixelColor(4, 0, 0, 10);
}
pixels.show();
Serial.print("temperature:" + String(temperature) + " sheshidu " + "photocell:" + String(photocell) + " lux\n");
delay(1000);
}
#include "SensorAB.h"
double get_temperature(const uint32_t pin) {
return 1.0/(log(1023.0/analogRead(pin)-1)/3950.0+1.0/(273.15+25))-273.15;
}
double get_photocell(const uint32_t pin) {
return analogRead(pin)*3.3/1023.0/2.9*3446;
}
#ifndef SENSOR_H
#define SENSOR_H
#include <Arduino.h>
#define R_T2 10000
#define B 3380000
#define T2 25
double get_temperature(uint32_t pin);
double get_photocell(uint32_t pin);
#endif //SENSOR_H
基础任务三(必做):接近检测——设定安全距离并通过板载LED展示,检测到入侵时,发起声音报警
使用红外传感器,,通过IR LED发射一定频率的脉冲,通过读取接收器测得的模拟值,当模拟值增大即有物体靠近。
试验现象:没有物体接近时灯珠全灭或显示一个,随着物体接近灯珠会亮的越来越多。
#include <Adafruit_CircuitPlayground.h>
#define SAFE_DISTANCE 500 // 定义安全距离
const int alertTone = 500; // 警报音调
const int irTransmitterPin = 25; //引脚定义
const int irReceiverPin = A10;
void setup()
{
CircuitPlayground.begin();
Serial.begin(9600); //
pinMode(irReceiverPin, INPUT); // 红外传感器输入
pinMode(irTransmitterPin, OUTPUT);// 红外led输出
delay(100);
}
void loop() {
sendIRPulse();
int distance = analogRead(irReceiverPin); // 读取红外传感器的值
displayDistance(distance);
checkForIntrusion(distance);
delay(300);
}
void displayDistance(int distance) {
int ledCount = map(distance, 290, SAFE_DISTANCE, 1, 10); // 将距离值映射到0-10的LED数量
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(", LED Count: ");
Serial.println(ledCount);
for (int i = 0; i < 10; i++) {
if (i < ledCount) {
CircuitPlayground.setPixelColor(i, 0, 255, 0);
} else {
CircuitPlayground.setPixelColor(i, 0);
}
}
}
void checkForIntrusion(int distance) {
if (distance > SAFE_DISTANCE) {
Serial.println("Intrusion detected!");
playAlertTone();
}
}
void sendIRPulse() {
for (int i = 0; i < 32; i++) {
digitalWrite(irTransmitterPin, HIGH);
delayMicroseconds(13);
digitalWrite(irTransmitterPin, LOW);
delayMicroseconds(13);
}
}
void playAlertTone() {
CircuitPlayground.playTone(alertTone, 500); // 播放警报音500ms
}