Grundfunktion für Ultraschallsensor und LED Licht
This commit is contained in:
parent
b00bf39e57
commit
c11fc035db
1 changed files with 107 additions and 0 deletions
107
Abstandssensor.ino
Normal file
107
Abstandssensor.ino
Normal file
|
@ -0,0 +1,107 @@
|
|||
#include <NeoPixelBus.h>
|
||||
#include <NeoPixelSegmentBus.h>
|
||||
#include <NeoPixelAnimator.h>
|
||||
#include <NeoPixelBrightnessBus.h>
|
||||
|
||||
|
||||
// DEV or PROD Setup
|
||||
#define DEBUG
|
||||
|
||||
// Only for ESP8266 D1 Mini
|
||||
#define ECHO 5 // D1
|
||||
#define TRIG 16 // D0
|
||||
#define POTI_IN 17 // A0, analog In
|
||||
|
||||
#define DATA_PIN 3
|
||||
const uint16_t PixelCount = 8;
|
||||
NeoPixelBus<NeoGrbFeature, NeoEsp8266Dma800KbpsMethod> strip(PixelCount, DATA_PIN);
|
||||
|
||||
long duration; // Variable um die Zeit der Ultraschall-Wellen zu speichern
|
||||
float distance; // Variable um die Entfernung zu berechnen
|
||||
|
||||
int poti;
|
||||
int percent=0;
|
||||
int colorSaturation=255;
|
||||
|
||||
RgbColor red(colorSaturation, 0, 0);
|
||||
RgbColor green(0, colorSaturation, 0);
|
||||
RgbColor yellow(colorSaturation, colorSaturation, 0);
|
||||
RgbColor blue(0, 0, colorSaturation);
|
||||
RgbColor white(colorSaturation);
|
||||
RgbColor black(0);
|
||||
|
||||
void setup() {
|
||||
|
||||
pinMode(TRIG, OUTPUT); // TRIG-Pin: Output
|
||||
pinMode(ECHO, INPUT); // ECHO-Pin: Input
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.begin(9600); // Baudrate: 115200
|
||||
#endif
|
||||
|
||||
// this resets all the neopixels to an off state
|
||||
strip.Begin();
|
||||
for(int i=0; i<PixelCount; i++) {strip.SetPixelColor(i, red);}
|
||||
strip.Show();
|
||||
|
||||
}
|
||||
|
||||
void setColor(RgbColor c) {
|
||||
for(int i=0; i<PixelCount; i++) {
|
||||
strip.SetPixelColor(i, c);
|
||||
}
|
||||
strip.Show();
|
||||
}
|
||||
|
||||
void setRed() {
|
||||
setColor(red);
|
||||
}
|
||||
|
||||
void setYellow() {
|
||||
setColor(yellow);
|
||||
}
|
||||
|
||||
void setGreen() {
|
||||
setColor(green);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
|
||||
// Read poti (Value range 16 - 1024)
|
||||
poti=analogRead(POTI_IN);
|
||||
|
||||
// Measure Distance
|
||||
digitalWrite(TRIG, LOW); delayMicroseconds(2);
|
||||
|
||||
// TRIG-Pin ist HIGH für 10 Microsekunden
|
||||
digitalWrite(TRIG, HIGH);
|
||||
delayMicroseconds(10);
|
||||
digitalWrite(TRIG, LOW);
|
||||
|
||||
// Liest das ECHO aus und speichert die Zeit von Senden / Empfangen in Microsekunden
|
||||
duration = pulseIn(ECHO, HIGH);
|
||||
|
||||
// Berechnung der Entfernung
|
||||
// Da der Weg doppel ist: Hinweg - Rückweg, muss der Wert durch 2 geteilt werden
|
||||
distance = duration * 0.034 / 2;
|
||||
|
||||
// Set color depending on poti
|
||||
// We assume poti value/20 (resulting in range <1,5 cm - 100cm)
|
||||
// Green if we are more than 50% above goal, yellow below 50% of goal, red at or below goal
|
||||
|
||||
percent=distance/((float)poti/10)*100;
|
||||
|
||||
if (percent >150) { setGreen(); }
|
||||
else if (percent >100) { setYellow(); }
|
||||
else {setRed(); }
|
||||
|
||||
// Anzeige der Entfernung im seriellen Monitor
|
||||
#ifdef DEBUG
|
||||
Serial.println("Poti: " + String(poti));
|
||||
Serial.println("Entfernung: " + String(distance) + "cm");
|
||||
Serial.println("Percent: " + String(percent) + " of goal value");
|
||||
#endif
|
||||
delay(100);
|
||||
|
||||
}
|
Loading…
Reference in a new issue