Created
July 3, 2018 12:25
-
-
Save ayoussef127/a835b2320fd8f2c5656c03fbbe5107bf to your computer and use it in GitHub Desktop.
Distance sensor - minimum reading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
int oldValue; | |
int lightingVal; | |
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 | |
} | |
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 | |
EVERY_N_MILLISECONDS (50) | |
{ | |
distance = oldValue; | |
} | |
if (distance < oldValue) { | |
distance = lightingVal; | |
} | |
else { | |
lightingVal = oldValue; | |
Serial.println(lightingVal); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment