Advertisement
gabbyshimoni

small project example code

Mar 16th, 2024
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | Software | 0 0
  1. //push button
  2. #define pbPin 2
  3. int pbState = 0; // 0 - released  1 - pressed
  4.  
  5. //potentiometer
  6. #define potPin A0
  7. int potValue = 0; // range 0-1023
  8.  
  9. // servo motor
  10. #include <Servo.h>
  11. Servo rightServo, leftServo;
  12.  
  13. // dc motor
  14. #define motor1 7
  15. #define motor2 8
  16. #define motorSpeed 6
  17.  
  18. // ultra sonic sensor
  19. #define trig 4
  20. #define echo 5
  21. long distance, duration;
  22.  
  23. // LED
  24. #define Rpin 3
  25. #define Gpin 11
  26. #define Bpin 12
  27.  
  28. void setup() {
  29.   Serial.begin(9600);
  30.   pinMode(pbPin , INPUT);
  31.   pinMode(potPin , INPUT);
  32.   pinMode(motor1 , OUTPUT);
  33.   pinMode(motor2 , OUTPUT);
  34.   pinMode(motorSpeed , OUTPUT);
  35.   pinMode(trig , OUTPUT);
  36.   pinMode(echo , INPUT);
  37.   pinMode(Rpin , OUTPUT);
  38.   pinMode(Gpin , OUTPUT);
  39.   pinMode(Bpin , OUTPUT);
  40.   rightServo.attach(9);
  41.   leftServo.attach(10);
  42. }
  43.  
  44. void loop() {
  45.   // קבלת מידע
  46.   pbState = digitalRead(pbPin);
  47.   Serial.print("pbState=");
  48.   Serial.println(pbState);
  49.   potValue = analogRead(potPin); // 0-1023
  50.   Serial.print("potValue=");
  51.   Serial.println(potValue);
  52.  
  53.   // איתחול השידור על ידי איפוס חיבור ה TRIG
  54.   digitalWrite(trig, LOW);
  55.   delayMicroseconds(2);
  56.   // שליחת פולס מעורר
  57.   digitalWrite(trig, HIGH);
  58.   delayMicroseconds(10);
  59.   // סיום פולס מעורר
  60.   digitalWrite(trig, LOW);
  61.   // המתנה והד מוחזר ומדידת זמן
  62.   duration = pulseIn(echo, HIGH);
  63.   //חישוב המרחק
  64.   distance = duration / 29 / 2;
  65.   Serial.print("distance[cm]=");
  66.   Serial.println(distance);
  67.  
  68.   // עיבוד מידע
  69.  
  70.   if (pbState == 1) {
  71.     // פעולה
  72.     rightServo.write(45);
  73.     delay(300);
  74.     leftServo.write(120);
  75.     delay(300);
  76.   }
  77.   else {
  78.     // פעולה
  79.     rightServo.write(0);
  80.     delay(300);
  81.     leftServo.write(0);
  82.     delay(300);
  83.   }
  84.  
  85.   if (distance < 70) {
  86.     int Speed = map(potValue , 0, 1023, 0, 255); // 0-255
  87.     analogWrite(motorSpeed , Speed);
  88.     digitalWrite(motor1, HIGH);
  89.     digitalWrite(motor2, LOW);
  90.     digitalWrite(Rpin , LOW);
  91.     digitalWrite(Gpin , HIGH);
  92.     digitalWrite(Bpin , LOW);
  93.     delay(500);
  94.     digitalWrite(Rpin , LOW);
  95.     digitalWrite(Gpin , LOW);
  96.     digitalWrite(Bpin , HIGH);
  97.     delay(500);
  98.   }
  99.   else {
  100.     analogWrite(motorSpeed , 0);
  101.     digitalWrite(motor1, LOW);
  102.     digitalWrite(motor2, LOW);
  103.     digitalWrite(Rpin , HIGH);
  104.     digitalWrite(Gpin , LOW);
  105.     digitalWrite(Bpin , LOW);
  106.   }
  107.  
  108.  
  109. }
Tags: Arduino
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement