// SHATS ROBO: To'siqdan qochuvchi robot // Plata: Arduino Uno. Kutubxona: Servo #include const uint8_t ENA = 5, IN1 = 7, IN2 = 8; // chap motor const uint8_t ENB = 6, IN3 = A0, IN4 = A1; // o'ng motor const uint8_t TRIG = 2, ECHO = 3, SERVO_PIN = 9; const int TEZLIK = 160; const long XAVFLI_MASOFA = 25; // sm Servo bosh; void motor(uint8_t en, uint8_t a, uint8_t b, int qiymat) { digitalWrite(a, qiymat > 0 ? HIGH : LOW); digitalWrite(b, qiymat < 0 ? HIGH : LOW); analogWrite(en, constrain(abs(qiymat), 0, 255)); } void harakat(int chap, int ong) { motor(ENA, IN1, IN2, chap); motor(ENB, IN3, IN4, ong); } long masofa() { digitalWrite(TRIG, LOW); delayMicroseconds(2); digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW); unsigned long t = pulseIn(ECHO, HIGH, 30000UL); // 30 ms: taxminan 5 m if (t == 0) return 500; // aks-sado yo'q: yo'l bo'sh return t / 58; // santimetr } long qaraVaOlchash(int burchak) { bosh.write(burchak); delay(350); return masofa(); } void setup() { const uint8_t pinlar[] = {ENA, IN1, IN2, ENB, IN3, IN4, TRIG}; for (uint8_t i = 0; i < sizeof(pinlar); i++) pinMode(pinlar[i], OUTPUT); pinMode(ECHO, INPUT); bosh.attach(SERVO_PIN); bosh.write(90); harakat(0, 0); delay(1500); } void loop() { if (masofa() > XAVFLI_MASOFA) { harakat(TEZLIK, TEZLIK); delay(40); return; } harakat(-TEZLIK, -TEZLIK); // biroz orqaga delay(300); harakat(0, 0); long chap = qaraVaOlchash(160); long ong = qaraVaOlchash(20); bosh.write(90); delay(200); if (chap > ong) harakat(-TEZLIK, TEZLIK); else harakat(TEZLIK, -TEZLIK); delay(350); // taxminan 90 daraja: robotingizga moslang harakat(0, 0); }