|
|
Zeile 9: |
Zeile 9: |
| . | | . |
| [[Datei:AldiLed.JPG|miniatur]] | | [[Datei:AldiLed.JPG|miniatur]] |
| Und hier der Sketch ohne LCD Display:
| |
| <source>
| |
| int pingPin = 3;
| |
| int inPin = 2;
| |
|
| |
| int mittel = 0;
| |
|
| |
| #include <Adafruit_NeoPixel.h>
| |
| #define PIN 6
| |
|
| |
|
| |
| Adafruit_NeoPixel strip = Adafruit_NeoPixel(50, PIN, NEO_GRB + NEO_KHZ800);
| |
|
| |
| void setup() {
| |
|
| |
| strip.begin();
| |
| strip.show(); // Initialize all pixels to 'off'
| |
|
| |
| }
| |
|
| |
| void loop()
| |
| {
| |
| // establish variables for duration of the ping,
| |
| // and the distance result in inches and centimeters:
| |
| long duration, inches, cm;
| |
|
| |
| // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
| |
| // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
| |
| pinMode(pingPin, OUTPUT);
| |
| digitalWrite(pingPin, LOW);
| |
| delayMicroseconds(2);
| |
| digitalWrite(pingPin, HIGH);
| |
| delayMicroseconds(10);
| |
| digitalWrite(pingPin, LOW);
| |
|
| |
| // The same pin is used to read the signal from the PING))): 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(inPin, INPUT);
| |
| duration = pulseIn(inPin, HIGH);
| |
|
| |
| // convert the time into a distance
| |
|
| |
| cm = microsecondsToCentimeters(duration);
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
| delay(10);
| |
|
| |
|
| |
| int dm = cm/10;
| |
|
| |
| mittel = (mittel * 9 + dm) / 10;
| |
|
| |
|
| |
| for (int P = 0; P <mittel; P++)
| |
| {
| |
| strip.setPixelColor(P, 0, 0, 254);
| |
|
| |
|
| |
| }
| |
| for (int P = 50; P >mittel; P--)
| |
| {
| |
| strip.setPixelColor(P, 0, 255, 0);
| |
|
| |
|
| |
| }
| |
| strip.setPixelColor(mittel, 255, 0, 0);
| |
| strip.show();
| |
|
| |
| }
| |
|
| |
|
| |
| long microsecondsToCentimeters(long microseconds)
| |
| {
| |
| // The speed of sound is 340 m/s or 29 microseconds per centimeter.
| |
| // The ping travels out and back, so to find the distance of the
| |
| // object we take half of the distance travelled.
| |
| return microseconds / 29 / 2;
| |
| }
| |
| </source>
| |