/********************************************************************** * Halloween 'Falling Spider' (22.10.2021) * ======================================= * Effekt nach einer Idee von Kristian Blåsol (Dropping Spider on Doorbell) * Abstands-Messverfahren stammt aus eBook zum Sensor von AZ-Delivery * * Dieser Code ist (W) by J.Reinert, Lehrte (JR-Projekte@gmx.de) * * Eine Vorrichtung mit einer nach unten gerichteten Öffnung, aus der bei * Annäherung eines Hindernisses eine Spinne herausfällt, kurz verweilt und * dann wieder nach oben gezogen wird * * Material: Arduino UNO/Nano oder Ähnliches * Installierte Arduino-Entwicklungsumgebung * Modellbau-Servo (z.B. SG90) * Ultraschall-Abstandssensor HC-SR04 * * Bibliotheken 'Servo.h' (Bestandteil der Entwicklungsumgebung) * * Konstanten SERVO_PIN PWM-Pin, an dem das Servo angeschlossen ist * Spider_Upperpos Winkel-Position Spinne oben* * Spider_Lowerpos Winkel-Position Spinne unten* * Spider_lowdelay Verweildauer der Spinne in unterster Position in ms * * debug Wenn definiert, erfolgen Ausgaben auf ein Terminal * * * Programm geht davon aus, dass das Servo im Kasten rechts montiert wurde * und der Servo-Arm sich entgegen des Uhrzeigersinnes bewegt, um die Spinne * nach unten zu bringen. In diesem Scenario ist die obere Position immer * kleiner als die Untere. Sollte das Servo links pltziert sein, muss das * Programm angepasst werden! */ // Ultraschall-Sendor Trigger an D3, Echo an D2 //#define debug #include #include #define SERVO_PIN 5 #define Spider_Upperpos 10 #define Spider_Lowerpos 84 #define Spider_lowdelay 2500 #define NUMPIXELS 12 // How many NeoPixels are attached to the Arduino? // Popular NeoPixel ring size #define LEDPIN 12 // Which pin on the Arduino is connected to the NeoPixels? #define RANDOM_INIT_PIN 0 // Analog-Input for init Random Generator Adafruit_NeoPixel pixels(NUMPIXELS, LEDPIN, NEO_GRB + NEO_KHZ800); Servo myservo; // create servo object to control a servo const int echoPin = 3; const int trigPin = 2; long duration; // Abstand in Zeit int pos = 0; // Servo Position long FirelastTime = 0; int interval; byte FireSequence=0; void setup() { #ifdef debug Serial.begin(9600); #endif myservo.attach(SERVO_PIN); // attaches the servo to the servo object pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); myservo.write(Spider_Upperpos); pixels.begin(); // INITIALIZE NeoPixels pixels.show(); // Initialize all pixels to 'off' interval = 300; randomSeed(analogRead(RANDOM_INIT_PIN)); } void loop() { SimulateFire(FireSequence); // Beginn Abstandsmessung (eBook AZ-Delivery) //noInterrupts(); digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); // Duration 1700-350 //interrupts(); // Ende Abstandsmessung (eBook AZ-Delivery) #ifdef debug Serial.print("Duration="); Serial.print(duration); Serial.print(" Position"); Serial.println(pos); #endif if (duration>350 && duration<1700) { // Signal-Laufzeit, auf die reagiert wird (<=30cm) pixels.fill(0xEE0000, 0, NUMPIXELS); pixels.show(); for (pos=Spider_Upperpos; posSpider_Upperpos; pos--){ // Spinne langsam einfahren myservo.write(pos); delay(20); } //pixels.fill(0x999900, 0, NUMPIXELS); //pixels.show(); } delay(2); // Prozessor-Entlastung } void SimulateFire (byte FireSq) { byte LightValue[NUMPIXELS * 3]; if (millis() - FirelastTime >= interval) { FirelastTime = millis(); interval = 200; if (FireSq == 0) // Orange starkes flackern kleine Abstände { interval = random(50, 100); for (int i = 0; i < NUMPIXELS; i++) { // For each pixel... LightValue[i * 3] = random(240, 255); // 250 LightValue[i * 3 + 1] = random(30, 60); // 50 LightValue[i * 3 + 2] = 0; } // Switch some lights out byte LightsOff = random(0, 6); for (int i = 0; i < LightsOff; i++) { byte Selected = random(NUMPIXELS); LightValue[Selected * 3] = 0; LightValue[Selected * 3 + 1] = 0; LightValue[Selected * 3 + 2] = 0; } for (int i = 0; i < NUMPIXELS; i++) { // For each pixel... pixels.setPixelColor(i, LightValue[i * 3], LightValue[i * 3 + 1], LightValue[i * 3 + 2]); } pixels.show(); // Send the updated pixel colors to the hardware. } if (FireSq == 1) // Kaminfeuer { interval = random(150, 200); for (int i = 0; i < NUMPIXELS; i++) { // For each pixel... LightValue[i * 3] = random(240, 255); // 250 LightValue[i * 3 + 1] = random(30, 60); // 50 LightValue[i * 3 + 2] = 0; } // Switch some lights darker byte LightsOff = random(0, 4); for (int i = 0; i < LightsOff; i++) { byte Selected = random(NUMPIXELS); LightValue[Selected * 3] = random(50, 60); LightValue[Selected * 3 + 1] = random(5, 10); LightValue[Selected * 3 + 2] = 0; } for (int i = 0; i < NUMPIXELS; i++) { // For each pixel... pixels.setPixelColor(i, LightValue[i * 3], LightValue[i * 3 + 1], LightValue[i * 3 + 2]); } pixels.show(); // Send the updated pixel colors to the hardware. } } }