I am trying to add some code to my Arduino where if the variable "distance" is constant for 1 minute the UltraSonic Sensor will only check for distance once per minute until the distance changes again. Once movement is detected it will revert to normal. Im thinking of a dormant mode.
#include <FastLED.h>
#define LED_PIN 12
//#define CLOCK_PIN 4
#define NUM_LEDS 60
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define BRIGHTNESS 128
#define trigPin 9
#define echoPin 10
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
delay(500);
LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
}
void loop()
{
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if(distance >= 617 && distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
//LED Based on Distance
if (distance >= 300 && distance <= 600) //green solid
{
fill_solid(leds, NUM_LEDS, CRGB(0,179,0));
delay(50);
FastLED.show();
}
if (distance > 51 && distance <= 299) //yellow swipe
{
fill_solid(leds, NUM_LEDS, CRGB(246,255,7));
for(int i=0;i<NUM_LEDS;i++) {
leds[i-3].setRGB( 0 , 0 ,0);
leds[i+3].setRGB(246, 255 , 7);
delay(30);
FastLED.show();
}
}
if (distance >= 25 && distance <= 50) //red solid
{
fill_solid(leds, NUM_LEDS, CRGB(255,0,0));
FastLED.show();
}
if (distance >= 0 && distance < 24) //flashing red
{
fill_solid(leds, NUM_LEDS, CRGB(255,0,0));
FastLED.show();
delay(200);
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
delay(200);
}
}