Created
November 16, 2019 20:59
-
-
Save reinst/dc2841daa369fa224f9fa6a0c6f3812d to your computer and use it in GitHub Desktop.
Arduino LED & sonar
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> | |
#include <QuickStats.h> | |
QuickStats stats; | |
// defining the pins | |
#define LED_PIN 7 | |
#define NUM_LEDS 29 | |
#define BRIGHTNESS 10 | |
const int trigPin = 9; | |
const int echoPin = 10; | |
// defining variables | |
CRGB leds[NUM_LEDS]; | |
float duration; | |
int distance; | |
int stopdistance=40; //int stopdistance=115; //parking position from sensor (CENTIMETERS) | |
int startdistance=100; //int startdistance=400; //distance from sensor to begin scan as car pulls in(CENTIMETERS) | |
int increment=((startdistance-stopdistance)/15); | |
void setup() { | |
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output | |
pinMode(echoPin, INPUT); // Sets the echoPin as an Input | |
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS); | |
Serial.begin(9600); // Starts the serial communication | |
} | |
void loop() { | |
for (int i=0;i<=14;i++){ | |
FastLED.setBrightness(BRIGHTNESS); | |
// The sensor is triggered by a HIGH pulse of 10 or more microseconds. | |
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse: | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
// Read the signal from the sensor: a HIGH pulse whose | |
// duration is the time (in microseconds) from the sending | |
// of the ping to the reception of its echo off of an object. | |
pinMode(echoPin, INPUT); | |
duration = pulseIn(echoPin, HIGH); | |
// Convert the time into a distance | |
distance = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343 | |
Serial.print(distance); | |
Serial.print("cm, "); | |
Serial.print(increment); | |
Serial.print(" increment variable"); | |
Serial.println(); | |
delay(150); | |
} | |
if (distance<stopdistance){ | |
for (int i = 0; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB (255,0,0); | |
FastLED.show(); | |
} | |
} | |
else | |
if (distance<stopdistance+increment){ | |
for (int i = 1; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 0, 0); | |
} | |
for (int i = 0; i <= 0; i++) { | |
leds[i] = CRGB ( 255, 255, 0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
else | |
if (distance<stopdistance+increment*2){ | |
for (int i = 2; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 0, 0); | |
} | |
for (int i = 0; i <= 1; i++) { | |
leds[i] = CRGB ( 255, 255, 0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
else | |
if (distance<stopdistance+increment*3){ | |
for (int i = 3; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 0, 0); | |
} | |
for (int i = 0; i <= 2; i++) { | |
leds[i] = CRGB ( 255, 255, 0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
else | |
if (distance<stopdistance+increment*4){ | |
for (int i = 4; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 0, 0); | |
} | |
for (int i = 0; i <= 3; i++) { | |
leds[i] = CRGB ( 0, 255,0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
else | |
if (distance<stopdistance+increment*5){ | |
for (int i = 5; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 0, 0); | |
} | |
for (int i = 0; i <= 4; i++) { | |
leds[i] = CRGB ( 0, 255,0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
else | |
if (distance<stopdistance+increment*6){ | |
for (int i = 6; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 0, 0); | |
} | |
for (int i = 0; i <= 5; i++) { | |
leds[i] = CRGB ( 0, 255,0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
else | |
if (distance<stopdistance+increment*7){ | |
for (int i = 7; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 0, 0); | |
} | |
for (int i = 0; i <= 6; i++) { | |
leds[i] = CRGB ( 0, 255,0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
else | |
if (distance<stopdistance+increment*8){ | |
for (int i = 8; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 0, 0); | |
} | |
for (int i = 0; i <= 7; i++) { | |
leds[i] = CRGB ( 0, 255,0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
else | |
if (distance<stopdistance+increment*9){ | |
for (int i = 9; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 0, 0); | |
} | |
for (int i = 0; i <= 8; i++) { | |
leds[i] = CRGB ( 0, 255,0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
else | |
if (distance<stopdistance+increment*10){ | |
for (int i = 10; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 0, 0); | |
} | |
for (int i = 0; i <= 9; i++) { | |
leds[i] = CRGB ( 0, 255,0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
else | |
if (distance<stopdistance+increment*11){ | |
for (int i = 11; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 0, 0); | |
} | |
for (int i = 0; i <= 10; i++) { | |
leds[i] = CRGB ( 0, 255,0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
else | |
if (distance<stopdistance+increment*12){ | |
for (int i = 12; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 0, 0); | |
} | |
for (int i = 0; i <= 11; i++) { | |
leds[i] = CRGB ( 0, 255,0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
else | |
if (distance<stopdistance+increment*13){ | |
for (int i = 13; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 0, 0); | |
} | |
for (int i = 0; i <= 12; i++) { | |
leds[i] = CRGB ( 0, 255,0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
else | |
if (distance<stopdistance+increment*14){ | |
for (int i = 14; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 0, 0); | |
} | |
for (int i = 0; i <= 13; i++) { | |
leds[i] = CRGB ( 0, 255,0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
else | |
if (distance>=stopdistance+increment*14){ | |
for (int i = 0; i <= NUM_LEDS; i++) { | |
leds[i] = CRGB ( 0, 255,0); | |
} | |
FastLED.show(); | |
delay(50); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment