Da ich auch mal was mit einem Servo bauen wollte,
habe ich eine kleine „Selbstschussanlage“ in 2 Versionen gebaut .
Es wurden hierfür verwendet:
- Arduinio Uno
- Laser aus einem alten Laserstift
- Modellbau Servo
- HC-SR04 Ultraschallsensors
Viel Spaß damit ;o)
Version 1:
https://www.youtube.com/watch?v=Rm9CACL7nVs
#include <Servo.h> #include <NewPing.h> Servo myservo; //Servo Config static byte ScanServoPin = 9; //Sonar Config static byte TRIGGER_PIN = 12; static byte ECHO_PIN = 11; static byte MAX_DISTANCE = 30; static byte LaserPin = 3; static byte Standby = 7; long previousMillis = 0; int ledState = HIGH; void setup() { Serial.begin (115200); pinMode(LaserPin, OUTPUT); pinMode(Standby, INPUT); } void loop() { if(digitalRead(Standby) == LOW) { digitalWrite(LaserPin, LOW); ScanServo(ScanServoPin); } else { LaserSleep(); } } void Laser() { for (int i = 0; i < 5; i++) { digitalWrite(LaserPin, HIGH); delay(55); digitalWrite(LaserPin, LOW); delay(55); } } void LaserSleep() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis > 1000) { previousMillis = currentMillis; if (ledState == LOW) { ledState = HIGH; SleepMessag(); } else { ledState = LOW; } digitalWrite(LaserPin, ledState); } } void FireMessag(int Target, int Range) { Serial.println(F("Alarm!")); Serial.print(F("Es wurde ein Ziel gesichtet auf ")); Serial.print(Target); Serial.print(F("Grad und ")); Serial.print(Range); Serial.println(F("cm Entfernung.")); Serial.println(F("Feuer!!!!!!")); Serial.println(F("")); Serial.println(F("")); } void NoTargetMessag() { Serial.println(F("Es wurden keine Ziel gesichtet.")); Serial.println(F("Starte naechsten Scann der Umgebung........")); Serial.println(F("")); Serial.println(F("")); } void SleepMessag() { Serial.println(F("System ist im Standby.....")); Serial.println(F("")); Serial.println(F("")); } void ScanServo (int Pin) { myservo.attach(Pin); int pos = 1; for(pos = 1; pos < 180; pos += 1) { unsigned long currentMillis = millis(); myservo.write(pos); if(currentMillis - previousMillis > 100) { previousMillis = currentMillis; if(Sonar() > 0) { FireMessag(pos, Sonar()); Laser(); } } delay(10); } NoTargetMessag(); previousMillis = 0; for(pos = 180; pos>=1; pos-=1) { unsigned long currentMillis = millis(); myservo.write(pos); if(currentMillis - previousMillis > 100) { previousMillis = currentMillis; if(Sonar() > 0) { FireMessag(pos, Sonar()); Laser(); } } delay(10); } // waits 15ms for the servo to reach the position NoTargetMessag(); previousMillis = 0; } int Sonar() { NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); unsigned int dist = sonar.ping()/ US_ROUNDTRIP_CM; return dist; }
Version 2:
https://www.youtube.com/watch?v=Z51RZTEvTdU
#include <Servo.h> #include <NewPing.h> Servo myservo; //Servo Config static byte ScanServoPin = 9; //Sonar Config static byte TRIGGER_PIN = 12; static byte ECHO_PIN = 11; static byte MAX_DISTANCE = 30; static byte LaserPin = 3; static byte Standby = 7; long previousMillis = 0; int ledState = HIGH; void setup() { Serial.begin (115200); pinMode(LaserPin, OUTPUT); pinMode(Standby, INPUT); } void loop() { if(digitalRead(Standby) == LOW) { digitalWrite(LaserPin, HIGH); ScanServo(ScanServoPin); } else { LaserSleep(); } } void Laser() { for (int i = 0; i < 21; i++) { digitalWrite(LaserPin, LOW); delay(55); digitalWrite(LaserPin, HIGH); delay(55); } } void LaserSleep() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis > 1000) { previousMillis = currentMillis; if (ledState == LOW) { ledState = HIGH; SleepMessag(); } else { ledState = LOW; } digitalWrite(LaserPin, ledState); } } void FireMessag(int Target, int Range) { Serial.println(F("Alarm!")); Serial.print(F("Es wurde ein Ziel gesichtet auf ")); Serial.print(Target); Serial.print(F("Grad und ")); Serial.print(Range); Serial.println(F("cm Entfernung.")); Serial.println(F("Feuer!!!!!!")); Serial.println(F("")); Serial.println(F("")); } void NoTargetMessag() { Serial.println(F("Es wurden keine Ziel gesichtet.")); Serial.println(F("Starte naechsten Scann der Umgebung........")); Serial.println(F("")); Serial.println(F("")); } void SleepMessag() { Serial.println(F("System ist im Standby.....")); Serial.println(F("")); Serial.println(F("")); } void ScanServo (int Pin) { myservo.attach(Pin); int pos = 1; int Range = 999; int Target = 0; int SonarNew = 0; for(pos = 1; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees { unsigned long currentMillis = millis(); // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' if(currentMillis - previousMillis > 50) { previousMillis = currentMillis; SonarNew = Sonar(); if(SonarNew > 0) { if(Range > SonarNew) { Range = SonarNew; Target = pos; } } } delay(10); // waits 15ms for the servo to reach the position } if(Target > 0) { myservo.write(Target); delay(600); FireMessag(Target, Range); Laser(); } else { NoTargetMessag(); Range = 999; Target = 0; previousMillis = 0; } for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees { unsigned long currentMillis = millis(); myservo.write(pos); // tell servo to go to position in variable 'pos' if(currentMillis - previousMillis > 80) { previousMillis = currentMillis; SonarNew = Sonar(); if(SonarNew > 0) { if(Range > SonarNew) { Range = SonarNew; Target = pos; } } } delay(10); // waits 15ms for the servo to reach the position } if(Target > 0) { myservo.write(Target); delay(600); FireMessag(Target, Range); Laser(); } else { NoTargetMessag(); Range = 999; Target = 0; previousMillis = 0; } } int Sonar() { NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); unsigned int dist = sonar.ping()/ US_ROUNDTRIP_CM; return dist; }
Der Aufbau: