Advertisement
gabbyshimoni

מבריח יונים

Apr 29th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. // הגדרת מנוע סרוו לתנועות הפחדה
  2. #include <Servo.h>
  3. Servo handsServo;
  4. #define srvPin 10
  5.  
  6. // משתנים לחיישן אולטראסוני
  7. #define trigPin 8
  8. #define echoPin 9
  9. long duration, distance;
  10.  
  11. // נורות חיווי על מצב הפעלה
  12. #define powerOnLed 3
  13. #define yonaLed 4
  14.  
  15. // משתנים של מפסקי גבול סוף מסילה
  16. #define startLSPin 5
  17. #define endLSPin 6
  18. int startLSValue = 0;
  19. int endLSValue = 0;
  20.  
  21. //משתנים למנוע רכב רובוטי
  22. #define motorPin1 11
  23. #define motorPin2 12
  24.  
  25. // משתנה לחיבור הזמזם
  26. #define buzPin 7
  27.  
  28. // משתנה כיוון הנסיעה
  29. int dir = 0; // 0- back or stop, 1 - froward
  30.  
  31. void setup() {
  32.   Serial.begin(9600);
  33.   handsServo.attach(srvPin);
  34.   // מצב התחלתי של זרועות הדחליל
  35.   handsServo.write(0);
  36.  
  37.   pinMode(trigPin, OUTPUT);
  38.   pinMode(echoPin, INPUT);
  39.  
  40.   pinMode(powerOnLed, OUTPUT);
  41.   // הדלק נורה כאשר יש מתח במערכת
  42.   digitalWrite(powerOnLed, HIGH);
  43.   pinMode(yonaLed, OUTPUT);
  44.   // מצב התחלתי לא זוהתה יונה
  45.   digitalWrite(yonaLed, LOW);
  46.  
  47.   pinMode(startLSPin, INPUT);
  48.   pinMode(endLSPin, INPUT);
  49.  
  50.   pinMode(motorPin1, OUTPUT);
  51.   pinMode(motorPin2, OUTPUT);
  52.   digitalWrite(motorPin1, LOW);
  53.   digitalWrite(motorPin2, LOW);
  54.  
  55. }
  56.  
  57. void loop() {
  58.  
  59.   digitalWrite(trigPin, LOW);
  60.   delayMicroseconds(2);
  61.   digitalWrite(trigPin, HIGH);
  62.   delayMicroseconds(10);
  63.   digitalWrite(trigPin, LOW);
  64.   duration = pulseIn(echoPin, HIGH);
  65.   distance = duration / 2 / 29.1;
  66.  
  67.   if (distance < 100) {
  68.     //זוהתה יונה במרחק של פחות ממטר אחד
  69.     endLSValue = digitalRead(endLSPin);
  70.     // כל עוד הרכב לא היגיע לקצה המסילה תבצע את הפעולות הבאות
  71.     while (endLSValue == LOW) {
  72.       // הפעל מנועים בנסיעה קדימה
  73.       digitalWrite(motorPin1, HIGH);
  74.       digitalWrite(motorPin2, LOW);
  75.       // הפעל זמזם להבריח יונים
  76.       tone(buzPin, 4000);
  77.       // הדלק נורת זיהוי יונה
  78.       digitalWrite(yonaLed, HIGH);
  79.       // הפעל מנוע ידי דחליל הלוך וחזור
  80.       handsServo.write(90);
  81.       delay(200);
  82.       handsServo.write(00);
  83.       delay(200);
  84.  
  85.       // קרא מפסקי גבול כדי לוודא שהרכב נמצא בתחילת המסילה
  86.       endLSValue = digitalRead(endLSPin);
  87.     }
  88.     // עצור מנועים עם ההגעה לקצה המסילה
  89.     digitalWrite(motorPin1, LOW);
  90.     digitalWrite(motorPin2, LOW);
  91.  
  92.     startLSValue = digitalRead(startLSPin);
  93.     // כל עוד הרובוט לא שב עד קצה המסילה תבצע את הפעולות הבאות
  94.     while (startLSValue == LOW) {
  95.       // הפעל מנועים בנסיעה לאחור
  96.       digitalWrite(motorPin1, LOW);
  97.       digitalWrite(motorPin2, HIGH);
  98.       // השתק זמזם
  99.       noTone(buzPin);
  100.       // כבה יונה חיווי על המסילה
  101.       digitalWrite(yonaLed, LOW);
  102.       // עצור ידי דחליל
  103.       handsServo.write(0);
  104.       delay(200);
  105.  
  106.       // קרא מפסקי גבול כדי לוודא שהרכב נמצא בתחילת המסילה
  107.       startLSValue = digitalRead(startLSPin);
  108.     }
  109.     // עצור מנועים עם ההגעה לקצה המסילה
  110.     digitalWrite(motorPin1, LOW);
  111.     digitalWrite(motorPin2, LOW);
  112.  
  113.   }
  114.  
  115.   else {
  116.     digitalWrite(yonaLed, LOW);
  117.   }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement