#include <FastLED.h>
#include <Arduino.h>
#include <HC_SR04.h>
// | Echo | Trigger
// Sensor 1: | 2 | 8
// Sensor 2: | 3 | 8
// Sensor 3: | 4 | 8
// Sensor 4: | 5 | 11
HC_SR04_BASE *Slaves[] = { new HC_SR04<3>(), new HC_SR04<4>(), new HC_SR04<5>(11) };
HC_SR04<2> sonicMaster(8, Slaves, 3);
#define NUM_LEDS 60 // Número toral dos les
CRGB leds[NUM_LEDS];
#define STRIP1_PIN 12 // Pin fitaled
#define STRIP2_PIN 10
#define STRIP3_PIN 9
#define STRIP4_PIN 7
#define STRIP5_PIN 6
#define STRIP6_PIN 14
#define STRIP7_PIN 15
#define STRIP8_PIN 16
int Distancia = 0;
void setup()
{
Serial.begin(9600);
sonicMaster.begin();
FastLED.addLeds < WS2811, STRIP1_PIN, GRB > (leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.addLeds < WS2811, STRIP2_PIN, GRB > (leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.addLeds < WS2811, STRIP3_PIN, GRB > (leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.addLeds < WS2811, STRIP4_PIN, GRB > (leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.addLeds < WS2811, STRIP5_PIN, GRB > (leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.addLeds < WS2811, STRIP6_PIN, GRB > (leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.addLeds < WS2811, STRIP7_PIN, GRB > (leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.addLeds < WS2811, STRIP8_PIN, GRB > (leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
}
void loop() {
sonicMaster.startMeasure(200000);
int menorValor = sonicMaster.getDist_cm(0);
for (int i = 0; i < sonicMaster.getNumberOfSensors(); i++) {
int valorAtual = sonicMaster.getDist_cm(i);
if (valorAtual < menorValor && valorAtual != 0) {
menorValor = valorAtual;
}
}
Serial.println(menorValor);
// Se algum sensor estiver dentro do limite, acender o LED
if (menorValor < 30) {
// meteorRain - Color (red, green, blue), meteor size, trail decay, random trail decay (true/false), speed delay
meteorRain(0xff,0x00,0x00,3, 100, true, 10);
Serial.println("1");
} else {
//Serial.println("0");
}
delay(1000); // Aguardar um segundo antes da próxima leitura
}
// *************************
// ** Meteor Rain **
// *************************
void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {
setAll(0,0,0);
for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) {
// fade brightness all LEDs one step
for(int j=0; j<NUM_LEDS; j++) {
if( (!meteorRandomDecay) || (random(10)>5) ) {
fadeToBlack(j, meteorTrailDecay);
}
}
// draw meteor
for(int j = 0; j < meteorSize; j++) {
if( ( i-j <NUM_LEDS) && (i-j>=0) ) {
setPixel(i-j, red, green, blue);
}
}
showStrip();
delay(SpeedDelay);
}
}
// used by meteorrain
void fadeToBlack(int ledNo, byte fadeValue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
uint32_t oldColor;
uint8_t r, g, b;
int value;
oldColor = strip.getPixelColor(ledNo);
r = (oldColor & 0x00ff0000UL) >> 16;
g = (oldColor & 0x0000ff00UL) >> 8;
b = (oldColor & 0x000000ffUL);
r=(r<=10)? 0 : (int) r-(r*fadeValue/256);
g=(g<=10)? 0 : (int) g-(g*fadeValue/256);
b=(b<=10)? 0 : (int) b-(b*fadeValue/256);
strip.setPixelColor(ledNo, r,g,b);
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[ledNo].fadeToBlackBy( fadeValue );
#endif
}
// ***************************************
// ** FastLed/NeoPixel Common Functions **
// ***************************************
// Apply LED color changes
void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}
// Set a LED color (not yet visible)
void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}
// Set all LEDs to a given color and apply it (visible)
void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}