#include <Arduino.h>
#include <FastLED.h> //点一个灯,点矩阵也需要
#include <FastLED_NeoMatrix.h> //点矩阵用的
#include "my_font.h"
#define LIGHT_OFF 0
#define LIGHT_GREEN_SAFE 1
#define LIGHT_GREEN 2
#define LIGHT_YELLOW 3
#define LIGHT_ORANGE 4
#define LIGHT_RED 5
#define LIGHT_RED_ALERT 6
#define LIGHT_X 3
#define LIGHT_Y 2
#define LIGHT_R 2
#define kMatrixWidth 32
#define kMatrixHeight 8
#define BRIGHTNESS 255
#define LED_PIN 12
#define COLOR_ORDER GRB
#define CHIPSET WS2812B
#define NUM_LEDS ((kMatrixWidth) * (kMatrixHeight))
#define SR04_TRIG_PIN 4
#define SR04_ECHO_PIN 5
#define BEEP_PIN 14
CRGB leds[NUM_LEDS]; //用来存每个灯的颜色
FastLED_NeoMatrix *matrix; //矩阵需要用这个初始化,点一个灯不需要
int light_r_delta = 0;
int light_blink_count = 0;
DEFINE_GRADIENT_PALETTE(red_to_green) {
0, 255, 0, 0, // red
127, 255, 255, 0, // yellow
255, 0, 255, 0 // green
};
CRGBPalette16 red_green_palette = red_to_green;
float readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns
// the sound wave travel time in microseconds
return round(0.034002 / 2 * pulseIn(echoPin, HIGH) * 10) / 10;
}
void BeepOn()
{
tone(BEEP_PIN, 250);
}
void BeepOff()
{
pinMode(BEEP_PIN, OUTPUT);
digitalWrite(BEEP_PIN, LOW);
}
void drawDistanceBar(int length)
{
if(length > 31)
length = 31;
for (int i = 0; i <= length; i++)
{
matrix->drawPixel(i, 6, ColorFromPalette( red_green_palette, i * 8, 255, NOBLEND ));
matrix->drawPixel(i, 7, ColorFromPalette( red_green_palette, i * 8, 255, NOBLEND ));
}
if(length == 31)
{
matrix->drawPixel(31, 6, matrix->Color(0, 255, 0));
matrix->drawPixel(31, 7, matrix->Color(0, 255, 0));
}
}
void drawLight(int color)
{
switch (color)
{
case LIGHT_OFF:
matrix->fillCircle(LIGHT_X, LIGHT_Y, LIGHT_R, matrix->Color(0, 0, 0));
BeepOff();
break;
case LIGHT_RED_ALERT:
light_r_delta = 0;
matrix->fillCircle(LIGHT_X, LIGHT_Y, LIGHT_R, matrix->Color(255, 0, 0));
BeepOn();
break;
case LIGHT_RED:
matrix->fillCircle(LIGHT_X, LIGHT_Y, LIGHT_R + light_r_delta, matrix->Color(255, 0, 0));
if (light_r_delta == 0)
{
BeepOn();
}
else
{
BeepOff();
}
light_r_delta--;
break;
case LIGHT_GREEN_SAFE:
BeepOff();
matrix->fillCircle(LIGHT_X, LIGHT_Y, LIGHT_R, matrix->Color(0, 255, 0));
light_blink_count = 0;
break;
case LIGHT_GREEN:
BeepOff();
matrix->fillCircle(LIGHT_X, LIGHT_Y, LIGHT_R + light_r_delta, matrix->Color(0, 255, 0));
light_blink_count++;
if (light_blink_count > 10)
{
light_r_delta--;
light_blink_count = 0;
}
break;
case LIGHT_YELLOW:
matrix->fillCircle(LIGHT_X, LIGHT_Y, LIGHT_R + light_r_delta, matrix->Color(255, 255, 0));
if (light_r_delta == 0)
{
BeepOn();
}
else
{
BeepOff();
}
light_blink_count++;
if (light_blink_count > 5)
{
light_r_delta--;
light_blink_count = 0;
}
break;
case LIGHT_ORANGE:
matrix->fillCircle(LIGHT_X, LIGHT_Y, LIGHT_R + light_r_delta, matrix->Color(255, 192, 0));
if (light_r_delta == 0)
{
BeepOn();
}
else
{
BeepOff();
}
light_blink_count++;
if (light_blink_count > 2)
{
light_r_delta--;
light_blink_count = 0;
}
break;
default:
break;
}
if (light_r_delta < -1)
light_r_delta = 0;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// 要配置一共点多少灯,类型是什么,接哪根线上
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setCorrection(UncorrectedColor); //颜色修正方式,可以不写
FastLED.setTemperature(UncorrectedTemperature); //温度修正方式,可以不写
FastLED.setDither(DISABLE_DITHER); //抖动设置,可以不写
//矩阵的设置
matrix = new FastLED_NeoMatrix(leds, 32, 8, NEO_MATRIX_TOP );
matrix->setTextWrap(false);
matrix->setBrightness(50);
matrix->setFont(&Picopixel);
matrix->clear();
matrix->setBrightness(255);
}
float distance = 0;
void loop() {
char text[7];
matrix->clear();
distance = readUltrasonicDistance(SR04_TRIG_PIN, SR04_ECHO_PIN);
sprintf(text, "%0.2f M", distance / 100);
if (distance >= 300)
{
matrix->setTextColor(matrix->Color(0, 255, 0));
matrix->setCursor(8, 4);
matrix->print(text);
drawLight(LIGHT_GREEN_SAFE);
}
else if (distance > 200 && distance < 300)
{
matrix->setTextColor(matrix->Color(0, 255, 0));
matrix->setCursor(8, 4);
matrix->print(text);
drawLight(LIGHT_GREEN);
}
else if (distance <= 200 && distance > 100 )
{
matrix->setTextColor(matrix->Color(255, 255, 0));
matrix->setCursor(8, 4);
matrix->print(text);
drawLight(LIGHT_YELLOW);
}
else if (distance <= 100 && distance > 40 )
{
matrix->setTextColor(matrix->Color(255, 192, 0));
matrix->setCursor(8, 4);
matrix->print(text);
drawLight(LIGHT_ORANGE);
}
else if (distance <= 40 && distance > 10 )
{
matrix->setTextColor(matrix->Color(255, 0, 0));
matrix->setCursor(8, 4);
matrix->print(text);
drawLight(LIGHT_RED);
}
else if (distance <= 10)
{
matrix->setTextColor(matrix->Color(255, 0, 0));
matrix->setCursor(8, 4);
matrix->print(text);
drawLight(LIGHT_RED_ALERT);
}
drawDistanceBar(distance / 12.5);
matrix->show();
delay(100); // this speeds up the simulation
}