【Follow me Season 2 Episode 1】02 Control the colorful LED on the board, light up the marquee and change the color
[Copy link]
This post was last edited by Congcong brother on 2024-9-8 23:27
Basic Task 1: Control the onboard colorful LED, light up the marquee and change the color
This task is relatively simple and can be achieved using the Adafruit_NeoPixel.h library. Seven different colors are set to cycle through.
Program flow chart
The code is as follows:
#include<Arduino.h>
#include<Adafruit_NeoPixel.h>
#definePIN8
#defineNNUMPIN10
Adafruit_NeoPixel pixels(NNUMPIN, PIN, NEO_GRB + NEO_KHZ800);
voidsetup(){
// write your initialization code here
pixels.begin();
}
voidloop(){
// write your code here
staticuint8_tled_num = 0;
//static uint32_t colors[8] = {0x040304, 0x000505, 0x050005, 0x050500, 0x050005, 0x000A00, 0x0A0000,0x0A000A};
staticuint32_tcolors[7] = {0x0A0000, 0x000A00, 0x00000A, 0x050500, 0x050005, 0x000505, 0x040304};
//static uint32_t colors[8] = {0x011011, 0x22022, 0x033033, 0x44044, 0x055055, 0x66066, 0x77777,0x088088};
staticuint8_tcolor_num = 0;
staticAdafruit_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_number = 0;
}
delay(200);
}
02-1
Basic Task 2: Monitor ambient temperature and light, and display comfort level through onboard LEDs
By referring to the officially released schematic diagram and the code of the CircuitPython library, we can derive the relationship between the pin voltage (A8) and temperature and light (A9).
#include<Arduino.h>
#include<Adafruit_NeoPixel.h>
#include"SensorAB.h"
#definePIN8
#defineNUMPIXELS10
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
voidsetup(){
// write your initialization code here
Serial.begin(115200);
pixels.begin();
}
voidloop(){
// write your code here
constdoubletemperature = get_temperature(A9);
constdoublephotocell = get_photocell(A8);
pixels.clear();
if(temperature < 14){
pixels.setPixelColor(5, 0, 0, 10);
}elseif(temperature < 18&& temperature >= 14){
pixels.setPixelColor(6, 0, 5, 5);
}elseif(temperature >= 18&& temperature <= 20){
pixels.setPixelColor(7, 0, 10, 0);
}elseif(temperature > 20&& temperature <= 25){
pixels.setPixelColor(8, 5, 5, 0);
}elseif(temperature > 25){
pixels.setPixelColor(9, 10, 0, 0);
}
if(photocell > 1000){
pixels.setPixelColor(0, 10, 0, 0);
}elseif(photocell > 500&& photocell <= 1000){
pixels.setPixelColor(1, 5, 5, 0);
}elseif(photocell >= 200&& photocell <= 500){
pixels.setPixelColor(2, 0, 10, 0);
}elseif(photocell >= 50&& photocell < 200){
pixels.setPixelColor(3, 0, 5, 5);
}elseif(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"
doubleget_temperature(constuint32_tpin){
return1.0/(log(1023.0/analogRead(pin)-1)/3950.0+1.0/(273.15+25))-273.15;
}
doubleget_photocell(constuint32_tpin){
returnanalogRead(pin)*3.3/1023.0/2.9*3446;
}
#ifndefSENSOR_H
#defineSENSOR_H
#include<Arduino.h>
#defineR_T210000
#defineB3380000
#defineT225
doubleget_temperature(uint32_tpin);
doubleget_photocell(uint32_tpin);
#endif//SENSOR_H
3-1
Basic Task 3 (Required): Proximity Detection - Set a safe distance and display it through the onboard LED. When an intrusion is detected, an audible alarm is triggered.
Using an infrared sensor, the IR LED emits pulses of a certain frequency, and the analog value measured by the receiver is read. When the analog value increases, an object is approaching.
Test phenomenon: When there is no object approaching, all the light beads are off or only one is displayed. As the object approaches, more and more light beads will light up.
#include<Adafruit_CircuitPlayground.h>
#define SAFE_DISTANCE 500 // Define safe distance
const int alertTone = 500; // Alert tone
const int irTransmitterPin = 25; //Pin definition
const int irReceiverPin = A10;
voidsetup()
{
CircuitPlayground.begin();
Serial.begin(9600);//
pinMode(irReceiverPin, INPUT); // Infrared sensor input
pinMode(irTransmitterPin, OUTPUT); // infrared LED output
delay(100);
}
voidloop(){
sendIRPulse();
int distance = analogRead(irReceiverPin); // Read the value of the infrared sensor
displayDistance(distance);
checkForIntrusion(distance);
delay(300);
}
voiddisplayDistance(intdistance){
int ledCount = map(distance, 290, SAFE_DISTANCE, 1, 10); // Map distance value to LED count from 0 to 10
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(", LED Count: ");
Serial.println(ledCount);
for(inti = 0; i < 10; i++){
if(i < ledCount){
CircuitPlayground.setPixelColor(i, 0, 255, 0);
}else{
CircuitPlayground.setPixelColor(i, 0);
}
}
}
voidcheckForIntrusion(intdistance){
if(distance > SAFE_DISTANCE){
Serial.println("Intrusion detected!");
playAlertTone();
}
}
void sendIRPulse() {
for(inti = 0; i < 32; i++){
digitalWrite(irTransmitterPin, HIGH);
delayMicroseconds(13);
digitalWrite(irTransmitterPin, LOW);
delayMicroseconds(13);
}
}
voidplayAlertTone(){
CircuitPlayground.playTone(alertTone, 500); // Play the alert tone for 500ms
}
4
|