Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
arduino_einstieg [24.06.2018] |
arduino_einstieg [30.11.2020] (aktuell) |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | ==== Arduino Einstieg ==== | ||
+ | |||
+ | Mein Einstieg begann mit einem Nachbau eines Arduino Uno.\\ | ||
+ | \\ | ||
+ | Conrad bot einen Nachbau zum Preis von ca. 17 Euro an:\\ | ||
+ | |||
+ | |||
+ | |||
+ | \\ | ||
+ | Ein Ebay-Shop in China zum Preis von unter 7 Euro inkl. 4 Wochen Lieferzeit: | ||
+ | {{:: | ||
+ | \\ | ||
+ | Pinbelegung gibt es u.a. hier:\\ | ||
+ | https:// | ||
+ | \\ | ||
+ | ==== IDE-Software download ==== | ||
+ | |||
+ | \\ | ||
+ | So startete ich also mit der Herunterladen der IDE und entpacken der zip-Datei.\\ | ||
+ | https:// | ||
+ | \\ | ||
+ | Hier lade ich die ZIP-Version herunter, ohne lässtige Installation.\\ | ||
+ | Der Anschluss des Arduino Uno (Nachbaus) erfolgt mit dem USB-Kabel.\\ | ||
+ | \\ | ||
+ | \\ | ||
+ | ==== Treiberinstallation ==== | ||
+ | |||
+ | |||
+ | Zunächst scheitert dies an dem fehlenden Treiber.\\ | ||
+ | {{:: | ||
+ | \\ | ||
+ | Der Treiber befindet sich glücklicherweise in dem entpackten Verzeichnis arduino-1.8.5-windows\arduino-1.8.5\drivers\\ | ||
+ | \\ | ||
+ | \\ | ||
+ | Über den Gerätemanager installieren wir den Teriber wie folgt:\\ | ||
+ | {{:: | ||
+ | \\ | ||
+ | \\ | ||
+ | {{:: | ||
+ | \\ | ||
+ | //Auf dem Computer nach Treibern suchen//\\ | ||
+ | \\ | ||
+ | {{:: | ||
+ | \\ | ||
+ | \\ | ||
+ | Der passende Treiber wurde gefunden und um eine Bestätigung wird gebeten:\\ | ||
+ | {{:: | ||
+ | \\ | ||
+ | Klicke auf Installieren.\\ | ||
+ | \\ | ||
+ | Das Gerät ist nun installiert und verfügbar.\\ | ||
+ | {{:: | ||
+ | \\ | ||
+ | {{:: | ||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ | ==== IDE-Software erster Start ==== | ||
+ | |||
+ | Die Software {{:: | ||
+ | \\ | ||
+ | {{:: | ||
+ | |||
+ | \\ | ||
+ | \\ | ||
+ | Dazu wählt man den Menüpunkt Werkzeug / Board.\\ | ||
+ | Hier steht nun Arduino Uno und kann sonst ausgewählt werden.\\ | ||
+ | {{:: | ||
+ | \\ | ||
+ | \\ | ||
+ | \\ | ||
+ | Unter Werkzeug / Port muss noch der richtige Port ausgewählt werden. In meienm Fall COM3.\\ | ||
+ | {{:: | ||
+ | \\ | ||
+ | \\ | ||
+ | \\ | ||
+ | Unter Datei / Beispiele findet man ganz viele Beispiele die als Vorlage für eigene Projekt dienen können.\\ | ||
+ | {{:: | ||
+ | \\ | ||
+ | \\ | ||
+ | \\ | ||
+ | Unter Werkzeug / Serieller Monitor bzw. Serieller Plotter steht die Ausgabe von Daten zur Verfügung.\\ | ||
+ | {{:: | ||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ | ==== Erstes Beispiel ==== | ||
+ | |||
+ | Die interne LED soll blinken wenn der Analogeingang A0 ausgelesen wird.\\ | ||
+ | In der IDE wird dazu ein neues Gerüst erzeugt:\\ | ||
+ | Datei / Neu\\ | ||
+ | und folgender Code hinein kopiert: | ||
+ | |||
+ | < | ||
+ | // Definition der Ports | ||
+ | int sensorValue0 = 0; // Analog Eingang A0 | ||
+ | int ledPin = 13; // die interne LED | ||
+ | |||
+ | // Grundgeruest | ||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | pinMode(ledPin, | ||
+ | } | ||
+ | |||
+ | |||
+ | // Schleife | ||
+ | void loop() { | ||
+ | sensorValue0 = analogRead(sensorValue0); | ||
+ | Serial.print(" | ||
+ | Serial.println(sensorValue0); | ||
+ | | ||
+ | digitalWrite(ledPin, | ||
+ | delay(200); | ||
+ | digitalWrite(ledPin, | ||
+ | delay(1000); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Nach dem übertragen blinkt die interne LED und am Seriellen Monitor / Plotter werden die Messwerte ausgegeben.\\ | ||
+ | \\ | ||
+ | {{:: | ||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ | |||
+ | ==== Abstand messen ==== | ||
+ | |||
+ | Ich messe den Abstand mit einem Ultraschallmodul HC-SR04.\\ | ||
+ | Dabei werden VCC mit dem Arduino-5Volt-Ausgang, | ||
+ | |||
+ | Das folgende Script misst die Entfernung und gibt sie in cm über den Seriellen Monitor aus:\\ | ||
+ | \\ | ||
+ | < | ||
+ | // | ||
+ | // arduino_uno_ultraschall_4.ino | ||
+ | // | ||
+ | int TriggerPin = 12; // Digital 12 -> HC-SR04 Trigger | ||
+ | int EchoPin = 13; // Digital 13 -> HC-SR04 Echo | ||
+ | // HC-SR04 Vcc -> 5V, GNG -> GND | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(9600); | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | // establish variables for duration of the ping, | ||
+ | // and the distance result in inches and centimeters: | ||
+ | long duration, 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(TriggerPin, | ||
+ | digitalWrite(TriggerPin, | ||
+ | delayMicroseconds(2); | ||
+ | digitalWrite(TriggerPin, | ||
+ | delayMicroseconds(10); | ||
+ | digitalWrite(TriggerPin, | ||
+ | |||
+ | // 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(EchoPin, | ||
+ | duration = pulseIn(EchoPin, | ||
+ | |||
+ | // convert the time into a distance | ||
+ | cm = microsecondsToCentimeters(duration); | ||
+ | if (cm <= 500) | ||
+ | { | ||
+ | Serial.print(cm); | ||
+ | Serial.println("" | ||
+ | } | ||
+ | else Serial.println(" | ||
+ | delay(100); | ||
+ | } | ||
+ | |||
+ | 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; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ | Im zweiten Beispiel sind die Bauteile wie folgt verkabelt: | ||
+ | \\ | ||
+ | Sensor - Arduino\\ | ||
+ | +5V - +5V\\ | ||
+ | Gnd - Gnd\\ | ||
+ | Trig - 13 Digital\\ | ||
+ | Echo - 12 Digital\\ | ||
+ | \\ | ||
+ | Der Programmcode gibt die Messwerte in cm aus: | ||
+ | < | ||
+ | // | ||
+ | // http:// | ||
+ | // | ||
+ | // arduino_uno_ultraschall_3.ino | ||
+ | // | ||
+ | |||
+ | /* | ||
+ | HC-SR04 Ping distance sensor] | ||
+ | VCC to arduino 5v GND to arduino GND | ||
+ | Echo to Arduino pin 13 Trig to Arduino pin 12 | ||
+ | Red POS to Arduino pin 11 | ||
+ | Green POS to Arduino pin 10 | ||
+ | 560 ohm resistor to both LED NEG and GRD power rail | ||
+ | More info at: http:// | ||
+ | Original code improvements to the Ping sketch sourced from Trollmaker.com | ||
+ | Some code and wiring inspired by http:// | ||
+ | */ | ||
+ | |||
+ | #define trigPin 13 | ||
+ | #define echoPin 12 | ||
+ | //#define led 11 | ||
+ | //#define led2 10 | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin (9600); | ||
+ | pinMode(trigPin, | ||
+ | pinMode(echoPin, | ||
+ | // | ||
+ | // | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | // long duration, distance; | ||
+ | float duration, distance; | ||
+ | digitalWrite(trigPin, | ||
+ | delayMicroseconds(2); | ||
+ | digitalWrite(trigPin, | ||
+ | // delayMicroseconds(1000); | ||
+ | delayMicroseconds(10); | ||
+ | digitalWrite(trigPin, | ||
+ | duration = pulseIn(echoPin, | ||
+ | distance = (duration/ | ||
+ | |||
+ | |||
+ | Serial.print(distance); | ||
+ | Serial.println(" | ||
+ | |||
+ | delay(1000); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | |||
+ | 2422.06 cm | ||
+ | 2421.63 cm | ||
+ | 2421.80 cm | ||
+ | 2421.48 cm | ||
+ | 2421.55 cm | ||
+ | 2421.68 cm | ||
+ | 2421.56 cm | ||
+ | 2421.51 cm | ||
+ | 2421.82 cm | ||
+ | 2421.82 cm | ||
+ | 2421.96 cm | ||
+ | 2422.56 cm | ||
+ | 2422.37 cm | ||
+ | 2422.08 cm | ||
+ | 2422.66 cm | ||
+ | 2422.30 cm | ||
+ | 2422.27 cm | ||
+ | 2422.22 cm | ||
+ | 2422.46 cm | ||
+ | 2422.10 cm | ||
+ | 2421.63 cm | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | \\ | ||
+ | \\ | ||
+ | Möchte man die Messwerte gemittelt ausgeben, müssen die Messwerte zunächst Summiert und durch die Anzahl der Messwerte geteilt werden.\\ | ||
+ | Dazu verwende ich eine For-Schleife die nach einer vorgegebenen Anzahl an Messwerte die Loop-Schleife beendet und den Mittelwert bildet. | ||
+ | |||
+ | |||
+ | < | ||
+ | // | ||
+ | // http:// | ||
+ | // | ||
+ | // | ||
+ | // arduino_uno_ultraschall_8.ino | ||
+ | // | ||
+ | // https:// | ||
+ | // http:// | ||
+ | |||
+ | |||
+ | /* | ||
+ | HC-SR04 Ping distance sensor] | ||
+ | VCC to arduino 5v GND to arduino GND | ||
+ | Echo to Arduino pin 13 Trig to Arduino pin 12 | ||
+ | Red POS to Arduino pin 11 | ||
+ | Green POS to Arduino pin 10 | ||
+ | 560 ohm resistor to both LED NEG and GRD power rail | ||
+ | More info at: http:// | ||
+ | Original code improvements to the Ping sketch sourced from Trollmaker.com | ||
+ | Some code and wiring inspired by http:// | ||
+ | */ | ||
+ | |||
+ | #define trigPin 13 | ||
+ | #define echoPin 12 | ||
+ | |||
+ | //int i; // schleifenzaehler | ||
+ | float duration, distance; | ||
+ | float summeallermesswerte = 0; | ||
+ | int zaehler = 0; | ||
+ | float mittelwert = 0; | ||
+ | int durchlaeufe = 99; // hier die gewünschte Anzahl eintragen | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin (9600); | ||
+ | pinMode(trigPin, | ||
+ | pinMode(echoPin, | ||
+ | } // void-setup-ende | ||
+ | |||
+ | |||
+ | void loop() { | ||
+ | |||
+ | for (int i=0; i< | ||
+ | |||
+ | // Ausgabe des aktuellen Durchlauf | ||
+ | // | ||
+ | // | ||
+ | | ||
+ | digitalWrite(trigPin, | ||
+ | delayMicroseconds(2); | ||
+ | digitalWrite(trigPin, | ||
+ | // | ||
+ | delayMicroseconds(10); | ||
+ | digitalWrite(trigPin, | ||
+ | duration = pulseIn(echoPin, | ||
+ | | ||
+ | distance = (duration/ | ||
+ | |||
+ | // Ausgabe des aktuellen Messwertes | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | |||
+ | summeallermesswerte = summeallermesswerte + distance; | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | |||
+ | zaehler = zaehler + 1; | ||
+ | // | ||
+ | // | ||
+ | |||
+ | mittelwert = summeallermesswerte / zaehler; | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | | ||
+ | delay(100); | ||
+ | |||
+ | } // for-schleifen-ende | ||
+ | |||
+ | |||
+ | // endausgabe | ||
+ | // Ausgabe des Mittelwertes | ||
+ | Serial.print(" | ||
+ | Serial.print(mittelwert); | ||
+ | Serial.println(" | ||
+ | |||
+ | |||
+ | |||
+ | // Reset aller Ausgangswerte | ||
+ | summeallermesswerte = 0; | ||
+ | zaehler = 0; | ||
+ | mittelwert = 0; | ||
+ | |||
+ | } // void-loop-ende | ||
+ | |||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | Quelle: http:// | ||
+ | \\ | ||
+ | |||
+ | ==== Netzwerk-Shield ==== | ||
+ | |||
+ | |||
+ | In China kann ein Netzwerkshield für rund 5 Euro bestellt werden.\\ | ||
+ | Nach 4 Wochen liegt es nun endlich auf meinem Schreibtisch: | ||
+ | {{:: | ||
+ | {{:: | ||
+ | {{:: | ||
+ | \\ | ||
+ | Nach dem Zusammenbau: | ||
+ | {{:: | ||
+ | |||
+ | \\ | ||
+ | belegt das Netzwerkshild die PINs 4, 10, 11, 12 und 13.\\ | ||
+ | Diese stehen dann nicht mehr zur Verfügung.\\ | ||
+ | Das muss für andere Projekte beachtet werden.\\ | ||
+ | \\ | ||
+ | Ein Beispielscript überträgt Daten an einen Server:\\ | ||
+ | < | ||
+ | /* | ||
+ | | ||
+ | | ||
+ | * Kurzfassung | ||
+ | * | ||
+ | * This program is free software: you can redistribute it and/or modify | ||
+ | * it under the terms of the GNU General Public License as published by | ||
+ | * the Free Software Foundation, either version 3 of the License, or | ||
+ | * (at your option) any later version. | ||
+ | | ||
+ | * This program is distributed in the hope that it will be useful, | ||
+ | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
+ | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
+ | * GNU General Public License for more details. | ||
+ | * | ||
+ | * You should have received a copy of the GNU General Public License | ||
+ | * along with this program. If not, see < | ||
+ | */ | ||
+ | |||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | // Hier muss eine eigene IP-Adresse, Gateway und DNS eingetragen werden, | ||
+ | // wenn die Netzwerkkonfiguration nicht über DHCP bezogen wird | ||
+ | // Sofern nicht mehrere Ethernet-Apdater gleichzeitig verwendet werden, | ||
+ | // braucht die MAC-Adresse nicht geändert werden | ||
+ | // Bitte darauf achten, dass die Adressen im Netzwerk nicht doppelt vergeben sind | ||
+ | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; | ||
+ | IPAddress ip(192, 168, 178, 111); | ||
+ | IPAddress gateway(255, | ||
+ | IPAddress dnServer(192, | ||
+ | |||
+ | // IP-Adresse statt Domainname zum Webserver mit Upload-Skript | ||
+ | // Bei verwendung der IP-adresse ist " | ||
+ | IPAddress server(192, 168, 178, 222); | ||
+ | |||
+ | // Domainname statt IP-Adresse zum Webserver mit beelogger-Skript | ||
+ | //static char server[] = " | ||
+ | |||
+ | // Verzeichnis in dem die Webserver-Skripte für den jeweiligen Logger liegen (mit nachgestelltem "/" | ||
+ | // z.B. const char* pfad[1][1]={" | ||
+ | const char* pfad[1][1]={"/ | ||
+ | |||
+ | // Passwort vom Webserver-Skript | ||
+ | static char Passwort[] = " | ||
+ | |||
+ | EthernetClient client; | ||
+ | |||
+ | // Test-Messdaten (regulär kommen die Messdaten von den Sensoren) | ||
+ | float wert1 = 25.00; | ||
+ | float wert2 = 35.00; | ||
+ | float wert3 = 40.00; | ||
+ | float wert4 = 60.00; | ||
+ | float wert5 = 50000.00; | ||
+ | long wert6 = 25000; | ||
+ | long wert7 = 200; | ||
+ | long wert8 = 500; | ||
+ | |||
+ | long Check; | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | delay(5000); | ||
+ | |||
+ | pinMode(4, | ||
+ | digitalWrite(4, | ||
+ | |||
+ | // Für Verwendung der Konfiguration über DHCP | ||
+ | //if (Ethernet.begin(mac) == 0) Serial.println(" | ||
+ | |||
+ | // Für Verwendung die eigenen Konfiguration | ||
+ | Ethernet.begin(mac, | ||
+ | |||
+ | delay(1000); | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | void loop() { | ||
+ | Check = round(wert1+wert2+wert3+wert4+wert5+wert6+wert7+wert8); | ||
+ | |||
+ | Serial.println(F(" | ||
+ | |||
+ | for (byte i = 0; i < 5; i++) { // Anzahl der Wiederholungen bei Verbindungsproblemen | ||
+ | // oder korrupten Daten, die vom Webserver empfangen werden | ||
+ | |||
+ | if (client.connect(server, | ||
+ | client.print(" | ||
+ | client.print(pfad[0][0]); | ||
+ | client.print(" | ||
+ | client.print (wert1); | ||
+ | |||
+ | client.print ("& | ||
+ | client.print (wert2); | ||
+ | |||
+ | client.print ("& | ||
+ | client.print (wert3); | ||
+ | |||
+ | client.print ("& | ||
+ | client.print (wert4); | ||
+ | |||
+ | client.print ("& | ||
+ | client.print (wert5); | ||
+ | |||
+ | client.print ("& | ||
+ | client.print (wert6); | ||
+ | |||
+ | client.print ("& | ||
+ | client.print (wert7); | ||
+ | |||
+ | client.print ("& | ||
+ | client.print (wert8); | ||
+ | |||
+ | client.print ("& | ||
+ | client.print (Check); | ||
+ | |||
+ | client.print ("& | ||
+ | client.print (Passwort); | ||
+ | |||
+ | client.println (" HTTP/ | ||
+ | client.print(" | ||
+ | client.println(server); | ||
+ | client.println(" | ||
+ | client.println(); | ||
+ | |||
+ | delay(10000); | ||
+ | |||
+ | Serial.println(F(" | ||
+ | |||
+ | } else Serial.println(F(" | ||
+ | |||
+ | unsigned long start = millis(); | ||
+ | char c; | ||
+ | while ((millis() - start < 2000) and (client.connected() )) { | ||
+ | c = client.read(); | ||
+ | // | ||
+ | if (c==' | ||
+ | i=10; | ||
+ | Serial.println(F(" | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | client.stop(); | ||
+ | client.flush(); | ||
+ | |||
+ | delay(1000); | ||
+ | |||
+ | Serial.println(F(" | ||
+ | Serial.println(); | ||
+ | } | ||
+ | // Test-Messdaten um 1 erhöhen | ||
+ | //TempIn++; | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | //Licht++; | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | |||
+ | // | ||
+ | } | ||
+ | </ | ||
+ | \\ | ||
+ | \\ | ||
+ | ==== Blitzdetektor ==== | ||
+ | |||
+ | \\ | ||
+ | Um Blitze mit dem Arduino Uno zu bestimmen, habe ich im Netz eine kleine Schaltung und Code gefunden.\\ | ||
+ | {{:: | ||
+ | {{:: | ||
+ | \\ | ||
+ | Der Code zeigt im SerialMonitor bei ersten Tests mit Berührungen der Antenne mit meinem Finger Signale an.\\ | ||
+ | |||
+ | < | ||
+ | // More information at: https:// | ||
+ | // https:// | ||
+ | // | ||
+ | // ardunino_uni_blitzdetektor_1 | ||
+ | // | ||
+ | |||
+ | #define FASTADC 1 | ||
+ | #ifndef cbi | ||
+ | #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) | ||
+ | #endif | ||
+ | #ifndef sbi | ||
+ | #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) | ||
+ | #endif | ||
+ | |||
+ | |||
+ | int data = 512; | ||
+ | int storage[512]; | ||
+ | |||
+ | long FlashStart; | ||
+ | long FlashEnd; | ||
+ | long tmr; | ||
+ | int reading; | ||
+ | int count; | ||
+ | int maximum; | ||
+ | int minimum; | ||
+ | bool toSend; | ||
+ | |||
+ | void setup() { | ||
+ | #if FASTADC | ||
+ | sbi(ADCSRA, ADPS2) ; | ||
+ | cbi(ADCSRA, ADPS1) ; | ||
+ | cbi(ADCSRA, ADPS0) ; | ||
+ | #endif | ||
+ | |||
+ | Serial.begin(9600); | ||
+ | pinMode(A5, INPUT); | ||
+ | Serial.println(micros()); | ||
+ | |||
+ | FlashStart = 0; | ||
+ | FlashEnd = 0; | ||
+ | reading = 0; | ||
+ | count = 0; | ||
+ | maximum = 0; | ||
+ | minimum = 1023; | ||
+ | toSend = false; | ||
+ | tmr = millis(); | ||
+ | } | ||
+ | |||
+ | |||
+ | void loop() { | ||
+ | reading = (analogRead(A5)); | ||
+ | storage[count] = reading; | ||
+ | if ((!toSend) && (count != 0) && ((reading > storage[count - 1] + 10) || (reading < storage[count - 1] - 10))) { | ||
+ | toSend = true; | ||
+ | } | ||
+ | |||
+ | count = count + 1; | ||
+ | if ((count == 512) && (toSend)) | ||
+ | { | ||
+ | count = 0; | ||
+ | FlashEnd = millis(); | ||
+ | sendData(); | ||
+ | FlashStart = millis(); | ||
+ | |||
+ | } | ||
+ | else if (count == 512) { | ||
+ | count = 0; | ||
+ | FlashEnd = millis(); | ||
+ | FlashStart = millis(); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | void sendData() | ||
+ | { | ||
+ | int getout; | ||
+ | for (int i = 0; i < data; i++) { | ||
+ | getout = storage[i]; | ||
+ | } | ||
+ | double tm = ((millis() - tmr) / 1000); | ||
+ | Serial.print(" | ||
+ | Serial.print(getout); | ||
+ | Serial.print(" | ||
+ | Serial.print(tm); | ||
+ | Serial.println(" | ||
+ | tmr = millis(); | ||
+ | toSend = false; | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | Den Code habe ich erweitert. Nun blinkt die interne LED und ein Digitalanschluss soll eine externe LED oder einen Optokoppler schalten.\\ | ||
+ | Der Code ist folgender: | ||
+ | < | ||
+ | //More information at: https:// | ||
+ | // das Original Script misst eine Blitz in einiger Entfernung | ||
+ | // | ||
+ | // das geänderte Script lässt die interne LED bei einem Blitzergeinis blinken | ||
+ | // | ||
+ | // in der nächsten Änderung schaltet dann ein Digitalausgang eine externe LED oder andere Dinge | ||
+ | // z.b. einen Optokoppler oder den Eingang eines NodeMCU8266 | ||
+ | // | ||
+ | // | ||
+ | |||
+ | |||
+ | // digitalausgang | ||
+ | int exdigi = 7; // Digitalausgang für externe Schaltung | ||
+ | |||
+ | |||
+ | // Blitzdetektor: | ||
+ | #define FASTADC 1 | ||
+ | #ifndef cbi | ||
+ | #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) | ||
+ | #endif | ||
+ | #ifndef sbi | ||
+ | #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) | ||
+ | #endif | ||
+ | |||
+ | |||
+ | int data = 512; | ||
+ | int storage[512]; | ||
+ | |||
+ | long FlashStart; | ||
+ | long FlashEnd; | ||
+ | long tmr; | ||
+ | int reading; | ||
+ | int count; | ||
+ | int maximum; | ||
+ | int minimum; | ||
+ | bool toSend; | ||
+ | |||
+ | void setup() { | ||
+ | #if FASTADC | ||
+ | sbi(ADCSRA, ADPS2) ; | ||
+ | cbi(ADCSRA, ADPS1) ; | ||
+ | cbi(ADCSRA, ADPS0) ; | ||
+ | #endif | ||
+ | |||
+ | Serial.begin(9600); | ||
+ | pinMode(A5, INPUT); | ||
+ | Serial.println(micros()); | ||
+ | |||
+ | FlashStart = 0; | ||
+ | FlashEnd = 0; | ||
+ | reading = 0; | ||
+ | count = 0; | ||
+ | maximum = 0; | ||
+ | minimum = 1023; | ||
+ | toSend = false; | ||
+ | tmr = millis(); | ||
+ | |||
+ | pinMode(13, OUTPUT); // interne LED | ||
+ | |||
+ | |||
+ | // digitalausgang | ||
+ | pinMode(exdigi, | ||
+ | } | ||
+ | |||
+ | |||
+ | void loop() { | ||
+ | reading = (analogRead(A5)); | ||
+ | storage[count] = reading; | ||
+ | if ((!toSend) && (count != 0) && ((reading > storage[count - 1] + 10) || (reading < storage[count - 1] - 10))) { | ||
+ | toSend = true; | ||
+ | } | ||
+ | |||
+ | count = count + 1; | ||
+ | if ((count == 512) && (toSend)) | ||
+ | { | ||
+ | count = 0; | ||
+ | FlashEnd = millis(); | ||
+ | sendData(); | ||
+ | FlashStart = millis(); | ||
+ | |||
+ | } | ||
+ | else if (count == 512) { | ||
+ | count = 0; | ||
+ | FlashEnd = millis(); | ||
+ | FlashStart = millis(); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | void sendData() | ||
+ | { | ||
+ | int getout; | ||
+ | for (int i = 0; i < data; i++) { | ||
+ | getout = storage[i]; | ||
+ | } | ||
+ | double tm = ((millis() - tmr) / 1000); | ||
+ | Serial.print(" | ||
+ | Serial.print(getout); | ||
+ | Serial.print(" | ||
+ | Serial.print(tm); | ||
+ | Serial.println(" | ||
+ | tmr = millis(); | ||
+ | toSend = false; | ||
+ | |||
+ | |||
+ | // digitalausgang schalten: | ||
+ | Serial.println(" | ||
+ | digitalWrite(exdigi, | ||
+ | delay(1000); | ||
+ | digitalWrite(exdigi, | ||
+ | delay(100); | ||
+ | |||
+ | |||
+ | // lasse interne LED leuchten | ||
+ | Serial.println(" | ||
+ | digitalWrite(13, | ||
+ | delay(100); | ||
+ | digitalWrite(13, | ||
+ | delay(100); | ||
+ | digitalWrite(13, | ||
+ | delay(100); | ||
+ | digitalWrite(13, | ||
+ | delay(100); | ||
+ | digitalWrite(13, | ||
+ | delay(100); | ||
+ | digitalWrite(13, | ||
+ | delay(100); | ||
+ | | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | Die dazugehörige Schaltung folgt, sobald ich sie erfolgreich erprobt habe :-)\\ | ||
+ | \\ | ||
+ | Neuer Stand: Die Schaltung funktioniert. Ein Optokoppler schaltet den Digitaleingang D1 eines NodeMCU ESP8266 von LOW auf HIGH.\\ | ||
+ | Der auf dem ESP8266 laufende Code (esp8266_blitzdetektor_1) überwacht den Eingang D1 und gibt dann per Wlan eine Meldung an meine RaspberryPi-Webserver: | ||
+ | \\ | ||
+ | < | ||
+ | // Quelle: https:// | ||
+ | // Quelle: https:// | ||
+ | // | ||
+ | // Pinzuordnung am esp8266 | ||
+ | // static const uint8_t D0 = 16; | ||
+ | // static const uint8_t D1 = 5; | ||
+ | // static const uint8_t D2 = 4; | ||
+ | // static const uint8_t D3 = 0; | ||
+ | // static const uint8_t D4 = 2; | ||
+ | // static const uint8_t D5 = 14; | ||
+ | // static const uint8_t D6 = 12; | ||
+ | // static const uint8_t D7 = 13; | ||
+ | // static const uint8_t D8 = 15; | ||
+ | // static const uint8_t D9 = 3; | ||
+ | // static const uint8_t D10 = 1; | ||
+ | |||
+ | #include < | ||
+ | const char* ssid = " | ||
+ | const char* password = " | ||
+ | const char* host = " | ||
+ | const char* SensorID = " | ||
+ | |||
+ | const byte interruptPin = 5; | ||
+ | volatile byte interruptCounter = 0; | ||
+ | int numberOfInterrupts = 0; | ||
+ | const char* meldung = " | ||
+ | |||
+ | void setup() { | ||
+ | |||
+ | // Kontakt zum wlan aufnehmen | ||
+ | Serial.println(); | ||
+ | Serial.println(); | ||
+ | Serial.print(" | ||
+ | Serial.println(ssid); | ||
+ | | ||
+ | WiFi.begin(ssid, | ||
+ | | ||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | |||
+ | Serial.println("" | ||
+ | Serial.println(" | ||
+ | Serial.println(" | ||
+ | Serial.println(WiFi.localIP()); | ||
+ | // Kontakt zum wlan aufnehmen - code ende - | ||
+ | | ||
+ | |||
+ | Serial.begin(9600); | ||
+ | pinMode(interruptPin, | ||
+ | attachInterrupt(digitalPinToInterrupt(interruptPin), | ||
+ | |||
+ | } | ||
+ | |||
+ | void handleInterrupt() { | ||
+ | interruptCounter++; | ||
+ | Serial.println(" | ||
+ | // | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | // wlan datenübergabe - beginn | ||
+ | |||
+ | Serial.print(" | ||
+ | Serial.println(host); | ||
+ | | ||
+ | // Use WiFiClient class to create TCP connections | ||
+ | WiFiClient client; | ||
+ | const int httpPort = 80; | ||
+ | if (!client.connect(host, | ||
+ | Serial.println(" | ||
+ | return; | ||
+ | } | ||
+ | | ||
+ | // uri erstellent | ||
+ | // esp8266.php? | ||
+ | String url = "/ | ||
+ | url += " | ||
+ | url += SensorID; | ||
+ | url += "& | ||
+ | url += meldung; | ||
+ | | ||
+ | Serial.print(" | ||
+ | Serial.println(url); | ||
+ | | ||
+ | // senden | ||
+ | client.print(String(" | ||
+ | " | ||
+ | " | ||
+ | Serial.println(" | ||
+ | delay(10); | ||
+ | | ||
+ | // Read all the lines of the reply from server and print them to Serial | ||
+ | while(client.available()){ | ||
+ | String line = client.readStringUntil(' | ||
+ | // | ||
+ | } | ||
+ | | ||
+ | Serial.println(); | ||
+ | Serial.println(" | ||
+ | |||
+ | // wlan datenübergabe - ende | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | |||
+ | if(interruptCounter> | ||
+ | |||
+ | interruptCounter--; | ||
+ | numberOfInterrupts++; | ||
+ | |||
+ | // | ||
+ | // | ||
+ | // | ||
+ | | ||
+ | // | ||
+ | // | ||
+ | | ||
+ | } | ||
+ | |||
+ | // reset des zaehlers und ausgabe fue den benutzer | ||
+ | numberOfInterrupts = 0; | ||
+ | // | ||
+ | // | ||
+ | |||
+ | |||
+ | } // loop ende | ||
+ | |||
+ | |||
+ | </ | ||
+ | \\ | ||
+ | |||
+ | ==== Frequenzgenerator ==== | ||
+ | |||
+ | |||
+ | Im [[http:// | ||
+ | Den könnte ich ja direkt für andere Projekt nutzen, wie z.b. meinem Geigerzähler.\\ | ||
+ | \\ | ||
+ | Also habe ich meinen Arduino Uno mit einem leicht abgewandelten Code gefüttert. Hier wird die Frequenz fest vorgegeben und nicht durch Eingabe abgefragt: | ||
+ | < | ||
+ | // | ||
+ | // Frequenzgenerator | ||
+ | // http:// | ||
+ | // | ||
+ | // Patrick' | ||
+ | // Frequenz-/ | ||
+ | |||
+ | #define INLENGTH 5 // | ||
+ | #define INTERMINATOR ' | ||
+ | |||
+ | char inString[INLENGTH+2]; | ||
+ | int inCount; | ||
+ | |||
+ | #define MinFreq 33 | ||
+ | #define MaxFreq 65535 | ||
+ | #define Ausgangspin 12 // | ||
+ | |||
+ | unsigned int Frequenz = 1000; //Hz (Maximum: 65535 Hz) | ||
+ | |||
+ | void setup(){ | ||
+ | |||
+ | Serial.begin(9600); | ||
+ | // | ||
+ | | ||
+ | tone(Ausgangspin, | ||
+ | |||
+ | Serial.print(" | ||
+ | Serial.print(Frequenz, | ||
+ | Serial.println(" | ||
+ | } | ||
+ | |||
+ | void loop(){ | ||
+ | // | ||
+ | |||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | |||
+ | //Bereich prüfen | ||
+ | Frequenz=constrain(Frequenz, | ||
+ | |||
+ | | ||
+ | |||
+ | tone(Ausgangspin, | ||
+ | } | ||
+ | |||
+ | void Eingabe(){ | ||
+ | inCount=0; | ||
+ | | ||
+ | do | ||
+ | { | ||
+ | while (Serial.available()==0); | ||
+ | inString[inCount] = Serial.read(); | ||
+ | if(inString[inCount]==INTERMINATOR) break; | ||
+ | if((inString[inCount]<' | ||
+ | // | ||
+ | inCount--; | ||
+ | } | ||
+ | } | ||
+ | while(++inCount < (INLENGTH+1)); | ||
+ | Serial.flush(); | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | \\ | ||
+ | \\ | ||
+ | ==== Winkelgeber ==== | ||
+ | |||
+ | Im Internet habe ich eine Seite gefunden, die den Anschluss eines Drehimpulsgebers (KY-040 Rotary Encoder Drehgeber) beschreibt.\\ | ||
+ | Als der Geber endlich auf dem Schreibtisch lag, kopierte ich den Code und kämpfte zunächst mit der Pinbelegung.\\ | ||
+ | \\ | ||
+ | {{: | ||
+ | \\ | ||
+ | Mein Geber ist wie folgt an dem Arduino angeschlossen: | ||
+ | \\ | ||
+ | Geberpin - Arduinopin\\ | ||
+ | CLK - 6\\ | ||
+ | DT - 5\\ | ||
+ | SW - 2\\ | ||
+ | \\ | ||
+ | \\ | ||
+ | \\ | ||
+ | drehimpulsgeber_4.ino | ||
+ | < | ||
+ | // Den Rotary encoder KY-040 auslesen | ||
+ | // Die Taste speichert den aktuellen Wert ins EEPROM | ||
+ | // | ||
+ | // Matthias Busse 10.2017 Version 1.0 | ||
+ | // | ||
+ | // http:// | ||
+ | // | ||
+ | |||
+ | #include < | ||
+ | |||
+ | int ra = 6; // Pin 6 zu CLK am KY-040 | ||
+ | int rb = 5; // Pin 7 zu DT am KY-040 | ||
+ | int sw = 2; // Pin 8 zu SW am KY-040 | ||
+ | int pos, alast, aval; | ||
+ | int eeadr=14; // Speicheradresse wird hier gewählt | ||
+ | |||
+ | void setup() { | ||
+ | pinMode (ra, | ||
+ | pinMode (rb,INPUT); | ||
+ | pinMode(sw, INPUT); | ||
+ | digitalWrite(sw, | ||
+ | EEPROM.get(eeadr, | ||
+ | alast = digitalRead(ra); | ||
+ | Serial.begin (38400); | ||
+ | Serial.print(" | ||
+ | Serial.println(pos); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | if( digitalRead(sw) == 0) { // Taste gedrückt? | ||
+ | EEPROM.put(eeadr, | ||
+ | Serial.print(" | ||
+ | Serial.println(pos); | ||
+ | while (digitalRead(sw) == 0); // warten bis die Taste losgelassen wird | ||
+ | } | ||
+ | aval = digitalRead(ra); | ||
+ | if (aval != alast){ // wenn gedreht | ||
+ | if (digitalRead(rb) != aval) { pos++; } // rechts rum | ||
+ | else { pos--; } // sonst links | ||
+ | Serial.print(" | ||
+ | Serial.println(pos); | ||
+ | } | ||
+ | alast = aval; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | Ausgabe beim drehen des Gebers: | ||
+ | < | ||
+ | Startpos: -3 | ||
+ | Pos: -2 | ||
+ | Pos: -1 | ||
+ | Pos: 0 | ||
+ | Pos: 1 | ||
+ | Pos: 2 | ||
+ | Pos: 3 | ||
+ | Pos: 4 | ||
+ | Pos: 3 | ||
+ | Pos: 4 | ||
+ | Pos: 3 | ||
+ | Pos: 4 | ||
+ | Pos: 5 | ||
+ | Pos: 6 | ||
+ | Pos: 7 | ||
+ | Pos: 8 | ||
+ | Pos: 9 | ||
+ | Pos: 10 | ||
+ | Pos: 11 | ||
+ | Pos: 10 | ||
+ | Pos: 11 | ||
+ | Pos: 10 | ||
+ | Pos: 9 | ||
+ | Pos: 8 | ||
+ | Pos: 7 | ||
+ | Pos: 6 | ||
+ | Pos: 5 | ||
+ | Pos: 4 | ||
+ | Pos: 3 | ||
+ | Pos: 2 | ||
+ | Pos: 3 | ||
+ | Pos: 2 | ||
+ | Pos: 3 | ||
+ | Pos: 2 | ||
+ | Pos: 1 | ||
+ | Pos: 0 | ||
+ | Pos: 1 | ||
+ | Pos: 0 | ||
+ | Pos: -1 | ||
+ | Pos: -2 | ||
+ | Pos: -1 | ||
+ | Pos: -2 | ||
+ | Pos: -3 | ||
+ | |||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | \\ | ||
+ | Die Suche nach einem anderen Code ergab:\\ | ||
+ | \\ | ||
+ | drehgeber_5.ino\\ | ||
+ | < | ||
+ | // Quelle: http:// | ||
+ | // Initialisierung benötigter Variablen | ||
+ | int Counter = 0; | ||
+ | boolean Richtung; | ||
+ | int Pin_clk_Letzter; | ||
+ | int Pin_clk_Aktuell; | ||
+ | |||
+ | // Definition der Eingangs-Pins | ||
+ | int pin_clk = 6; | ||
+ | int pin_dt = 5; | ||
+ | int button_pin = 2; | ||
+ | | ||
+ | | ||
+ | void setup() | ||
+ | { | ||
+ | // Eingangs-Pins werden initialisiert... | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | // ...und deren Pull-Up Widerstände aktiviert | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | // Initiales Auslesen des Pin_CLK | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | // Das Programm überprüft, | ||
+ | // Pins sich zuerst geändert hatte, was auf die Drehrichtung schließen lässt. | ||
+ | // Diese Information erhält man, in dem man einen der beiden Pin-Werte aus einem vorherigen | ||
+ | // Durchlauf mit dem Wert des aktuellen Durchlaufs vergleicht. | ||
+ | // Nachdem die Richtung festgestellt wurde, werden die Schritte von der Startposition an gezählt und ausgegeben. | ||
+ | // Ein Drücken auf den Knopf des Drehgebers resettet die aktuelle Position. | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | // Auslesen des aktuellen Statuses | ||
+ | | ||
+ | | ||
+ | // Überprüfung auf Änderung | ||
+ | if (Pin_clk_Aktuell != Pin_clk_Letzter) | ||
+ | | ||
+ | | ||
+ | if (digitalRead(pin_dt) != Pin_clk_Aktuell) | ||
+ | { | ||
+ | // Pin_CLK hat sich zuerst verändert | ||
+ | Counter ++; | ||
+ | Richtung = true; | ||
+ | } | ||
+ | | ||
+ | else | ||
+ | { // Andernfalls hat sich Pin_DT zuerst verändert | ||
+ | Richtung = false; | ||
+ | Counter--; | ||
+ | } | ||
+ | Serial.println (" | ||
+ | Serial.print (" | ||
+ | |||
+ | if (Richtung) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | |||
+ | Serial.print(" | ||
+ | Serial.println(Counter); | ||
+ | Serial.println(" | ||
+ | | ||
+ | | ||
+ | | ||
+ | // Vorbereitung für den nächsten Druchlauf: | ||
+ | // Der Wert des aktuellen Durchlaufs ist beim nächsten Druchlauf der vorherige Wert | ||
+ | | ||
+ | | ||
+ | // Reset-Funktion um aktuelle Position zu speichern | ||
+ | if (!digitalRead(button_pin) && Counter!=0) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | Ausgabe:\\ | ||
+ | < | ||
+ | Drehung erkannt: | ||
+ | Drehrichtung: | ||
+ | Aktuelle Position: -1 | ||
+ | ------------------------------ | ||
+ | Drehung erkannt: | ||
+ | Drehrichtung: | ||
+ | Aktuelle Position: -2 | ||
+ | ------------------------------ | ||
+ | Position resettet | ||
+ | Drehung erkannt: | ||
+ | Drehrichtung: | ||
+ | Aktuelle Position: 1 | ||
+ | ------------------------------ | ||
+ | Drehung erkannt: | ||
+ | Drehrichtung: | ||
+ | Aktuelle Position: 2 | ||
+ | ------------------------------ | ||
+ | Drehung erkannt: | ||
+ | Drehrichtung: | ||
+ | Aktuelle Position: 3 | ||
+ | ------------------------------ | ||
+ | Drehung erkannt: | ||
+ | Drehrichtung: | ||
+ | Aktuelle Position: 4 | ||
+ | ------------------------------ | ||
+ | Drehung erkannt: | ||
+ | Drehrichtung: | ||
+ | Aktuelle Position: 3 | ||
+ | ------------------------------ | ||
+ | Drehung erkannt: | ||
+ | Drehrichtung: | ||
+ | Aktuelle Position: 2 | ||
+ | ------------------------------ | ||
+ | Drehung erkannt: | ||
+ | Drehrichtung: | ||
+ | Aktuelle Position: 1 | ||
+ | ------------------------------ | ||
+ | Drehung erkannt: | ||
+ | Drehrichtung: | ||
+ | Aktuelle Position: 0 | ||
+ | ------------------------------ | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | Diese habe ich nun so angepasst, dass eine volle Umdrehung des Gebers auch die 20 Schritte die er hat, anzeigt:\\ | ||
+ | \\ | ||
+ | drehgeber_6.ino\\ | ||
+ | < | ||
+ | // Initialisierung benötigter Variablen | ||
+ | int Counter = 0; | ||
+ | int Counter2 = 0; | ||
+ | boolean Richtung; | ||
+ | int Pin_clk_Letzter; | ||
+ | int Pin_clk_Aktuell; | ||
+ | |||
+ | // Definition der Eingangs-Pins | ||
+ | int pin_clk = 6; | ||
+ | int pin_dt = 5; | ||
+ | int button_pin = 2; | ||
+ | | ||
+ | | ||
+ | void setup() | ||
+ | { | ||
+ | // Eingangs-Pins werden initialisiert... | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | // ...und deren Pull-Up Widerstände aktiviert | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | // Initiales Auslesen des Pin_CLK | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | // Das Programm überprüft, | ||
+ | // Pins sich zuerst geändert hatte, was auf die Drehrichtung schließen lässt. | ||
+ | // Diese Information erhält man, in dem man einen der beiden Pin-Werte aus einem vorherigen | ||
+ | // Durchlauf mit dem Wert des aktuellen Durchlaufs vergleicht. | ||
+ | // Nachdem die Richtung festgestellt wurde, werden die Schritte von der Startposition an gezählt und ausgegeben. | ||
+ | // Ein Drücken auf den Knopf des Drehgebers resettet die aktuelle Position. | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | // Auslesen des aktuellen Statuses | ||
+ | | ||
+ | | ||
+ | // Überprüfung auf Änderung | ||
+ | if (Pin_clk_Aktuell != Pin_clk_Letzter) | ||
+ | | ||
+ | | ||
+ | if (digitalRead(pin_dt) != Pin_clk_Aktuell) | ||
+ | { | ||
+ | // Pin_CLK hat sich zuerst verändert | ||
+ | Counter ++; | ||
+ | Richtung = true; | ||
+ | } | ||
+ | | ||
+ | else | ||
+ | { // Andernfalls hat sich Pin_DT zuerst verändert | ||
+ | Richtung = false; | ||
+ | Counter--; | ||
+ | } | ||
+ | Serial.println (" | ||
+ | Serial.print (" | ||
+ | |||
+ | if (Richtung) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | |||
+ | Serial.print(" | ||
+ | Counter = Counter; | ||
+ | Serial.println(Counter); | ||
+ | |||
+ | Serial.print(" | ||
+ | Counter2 = (Counter/ | ||
+ | Serial.println(Counter2); | ||
+ | |||
+ | Serial.println(" | ||
+ | | ||
+ | | ||
+ | | ||
+ | // Vorbereitung für den nächsten Druchlauf: | ||
+ | // Der Wert des aktuellen Durchlaufs ist beim nächsten Druchlauf der vorherige Wert | ||
+ | | ||
+ | | ||
+ | // Reset-Funktion um aktuelle Position zu speichern | ||
+ | if (!digitalRead(button_pin) && Counter!=0) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | Ausgabe:\\ | ||
+ | < | ||
+ | Drehung erkannt: | ||
+ | Drehrichtung: | ||
+ | Aktuelle Position: 18 | ||
+ | Wahre aktuelle Position: 9 | ||
+ | ------------------------------ | ||
+ | Drehung erkannt: | ||
+ | Drehrichtung: | ||
+ | Aktuelle Position: 19 | ||
+ | Wahre aktuelle Position: 9 | ||
+ | ------------------------------ | ||
+ | Drehung erkannt: | ||
+ | Drehrichtung: | ||
+ | Aktuelle Position: 20 | ||
+ | Wahre aktuelle Position: 10 | ||
+ | ------------------------------ | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | \\ | ||
+ | \\ | ||
+ | === Quellen === | ||
+ | |||
+ | https:// | ||
+ | https:// | ||
+ | http:// | ||
+ | http:// | ||
+ | http:// | ||
+ | \\ | ||
+ | \\ |