A spiral robot that sweeps trash

#include <Servo.h>

Servo sweeper;

const int in1=8, in2=9, in3=10, in4=11;
const int servoPin=5;
const int trigPin=6, echoPin=7;

void setup() {
  pinMode(in1, OUTPUT); pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT); pinMode(in4, OUTPUT);
  pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT);
  sweeper.attach(servoPin);
  sweeper.write(90);
  delay(500);
}

void loop() {
  for(int side=5; side>0; side--) {
    for(int i=0; i<4; i++) {
      moveAndDetect(500 + side*300);
      turnRight(500);
    }
  }
  stopMotors();
  delay(3600000);
}

void moveAndDetect(int duration) {
  digitalWrite(in1,HIGH); digitalWrite(in2,LOW);
  digitalWrite(in3,HIGH); digitalWrite(in4,LOW);

  unsigned long start=millis();
  while(millis()-start<duration) {
    int d = checkTrash();
    if(d>0 && d<15) {
      stopMotors();
      sweepTrash();
      delay(200);
      digitalWrite(in1,HIGH); digitalWrite(in2,LOW);
      digitalWrite(in3,HIGH); digitalWrite(in4,LOW);
    }
    sweepServo();
    delay(20);
  }
  stopMotors();
  delay(200);
}

void turnRight(int dur) {
  digitalWrite(in1,HIGH); digitalWrite(in2,LOW);
  digitalWrite(in3,LOW); digitalWrite(in4,HIGH);
  delay(dur);
  stopMotors();
  delay(200);
}

void stopMotors() {
  digitalWrite(in1,LOW); digitalWrite(in2,LOW);
  digitalWrite(in3,LOW); digitalWrite(in4,LOW);
}

void sweepServo() {
  static int pos=90;
  static bool right=true;
  if(right) pos+=5; else pos-=5;
  if(pos>=150) right=false;
  if(pos<=30) right=true;
  sweeper.write(pos);
  delay(5);
}

int checkTrash() {
  digitalWrite(trigPin,LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
  long dur = pulseIn(echoPin,HIGH,15000);
  return dur*0.034/2;
}

void sweepTrash() {
  for(int i=0; i<3; i++) {
    sweeper.write(30); delay(300);
    sweeper.write(150); delay(300);
  }
  sweeper.write(90);
}

Heres the code for the mechanism
can anyone test if it works and help me with the debugging

It compiles, makes the robot "turn" (direction depends on your motor config), responds by "sweeping" at distances (from??) under 15cm (but never reverses to avoid the object so your sweeper must be longer than 15cm, or it will sweep until the 1 hour delay - you should indicate when the 1 hour delay is happening)... but what do you qualify as working? Describe the steps you expect it to perform.

If you you place `Serial.print();' statements alongside the motor and servo pin commands, you can "read" how (if) the code is working.

Hi, @aayan_lama
Welcome to the forum.

Have you tried your code?
Have built a prototype?
If so what does it do and what is it supposed to do?

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Tom.... :smiley: :+1: :coffee: :australia:

Probably not, but someone could offer suggestions, if you explain what the code supposed to do, and what it does instead.