//push button #define pbPin 2 int pbState = 0; // 0 - released 1 - pressed //potentiometer #define potPin A0 int potValue = 0; // range 0-1023 // servo motor #include Servo rightServo, leftServo; // dc motor #define motor1 7 #define motor2 8 #define motorSpeed 6 // ultra sonic sensor #define trig 4 #define echo 5 long distance, duration; // LED #define Rpin 3 #define Gpin 11 #define Bpin 12 void setup() { Serial.begin(9600); pinMode(pbPin , INPUT); pinMode(potPin , INPUT); pinMode(motor1 , OUTPUT); pinMode(motor2 , OUTPUT); pinMode(motorSpeed , OUTPUT); pinMode(trig , OUTPUT); pinMode(echo , INPUT); pinMode(Rpin , OUTPUT); pinMode(Gpin , OUTPUT); pinMode(Bpin , OUTPUT); rightServo.attach(9); leftServo.attach(10); } void loop() { // קבלת מידע pbState = digitalRead(pbPin); Serial.print("pbState="); Serial.println(pbState); potValue = analogRead(potPin); // 0-1023 Serial.print("potValue="); Serial.println(potValue); // איתחול השידור על ידי איפוס חיבור ה TRIG digitalWrite(trig, LOW); delayMicroseconds(2); // שליחת פולס מעורר digitalWrite(trig, HIGH); delayMicroseconds(10); // סיום פולס מעורר digitalWrite(trig, LOW); // המתנה והד מוחזר ומדידת זמן duration = pulseIn(echo, HIGH); //חישוב המרחק distance = duration / 29 / 2; Serial.print("distance[cm]="); Serial.println(distance); // עיבוד מידע if (pbState == 1) { // פעולה rightServo.write(45); delay(300); leftServo.write(120); delay(300); } else { // פעולה rightServo.write(0); delay(300); leftServo.write(0); delay(300); } if (distance < 70) { int Speed = map(potValue , 0, 1023, 0, 255); // 0-255 analogWrite(motorSpeed , Speed); digitalWrite(motor1, HIGH); digitalWrite(motor2, LOW); digitalWrite(Rpin , LOW); digitalWrite(Gpin , HIGH); digitalWrite(Bpin , LOW); delay(500); digitalWrite(Rpin , LOW); digitalWrite(Gpin , LOW); digitalWrite(Bpin , HIGH); delay(500); } else { analogWrite(motorSpeed , 0); digitalWrite(motor1, LOW); digitalWrite(motor2, LOW); digitalWrite(Rpin , HIGH); digitalWrite(Gpin , LOW); digitalWrite(Bpin , LOW); } }