Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayoussef127/d97141c8605aa79d8f9c02031f01bb96 to your computer and use it in GitHub Desktop.
Save ayoussef127/d97141c8605aa79d8f9c02031f01bb96 to your computer and use it in GitHub Desktop.
#include <FastLED.h>
#define NUM_LEDS 200
#define datapin 6
#define LED_TYPE WS2811
CRGB leds[NUM_LEDS];
const int trigPin = 10;
const int echoPin = 11;
// defines variables
long duration;
int distance;
unsigned long interval=2000; // the time we need to wait
unsigned long startMillis=0; // millis() returns an unsigned long.
const unsigned long period = 1000; //the value is a number of millisecond
const unsigned long runInterval; //the value is a number of millisecond
void setup() {
FastLED.addLeds<WS2811, datapin>(leds, NUM_LEDS);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
startMillis = millis();
}
void loop() {
//distance code
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
// main code
unsigned long currentMillis = millis(); // grab current time
// check the time elapsed against our interval - if less, run the code, if more, move
// back to default lights.
if (distance < 40) {
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
currentMillis + period == runInterval;
if (currentMillis < runInterval) { //test whether the period has elapsed
fill_solid(leds, NUM_LEDS, CRGB::Blue);
FastLED.show();
startMillis = currentMillis;
}
}
else {
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment