Simple robot/ floor cleaner to go through all the space available

Hi,

I have building a robot with 2 motors on each side and a spinning wheel on the front, so it can go forward/backward and turn right/left easily. My objective is to creat floor cleaner to go through all the space available at my room.

I have created one with this components:

  1. ARDUINO UNO R3
  2. L293d Motor Shield
  3. 2 Bo motor 100 rpm
  4. hc-sr04 ultrasonic sensor

It works fine for a but not fulfill my objective, "go through all the space available"

My code is as under

#include <AFMotor.h>[color=#222222][/color]
#define trigPin 12[color=#222222][/color]
#define echoPin 13[color=#222222][/color]
AF_DCMotor motor1(1);[color=#222222][/color]
AF_DCMotor motor2(2);[color=#222222][/color]
[color=#222222][/color]
void setup() {[color=#222222][/color]
  pinMode(trigPin, OUTPUT);[color=#222222][/color]
  pinMode(echoPin, INPUT);[color=#222222][/color]
}[color=#222222][/color]
[color=#222222][/color]
void loop() {[color=#222222][/color]
[color=#222222][/color]
  long duration, distance;[color=#222222][/color]
  digitalWrite(trigPin, LOW);[color=#222222][/color]
  delayMicroseconds(2);[color=#222222][/color]
  digitalWrite(trigPin, HIGH);[color=#222222][/color]
[color=#222222][/color]
  delayMicroseconds(2);[color=#222222][/color]
  digitalWrite(trigPin, LOW);[color=#222222][/color]
  duration = pulseIn(echoPin, HIGH);[color=#222222][/color]
  distance = (duration / 2) / 29.1;[color=#222222][/color]
[color=#222222][/color]
  if (distance < 15) {[color=#222222][/color]
    motor1.setSpeed(100);[color=#222222][/color]
    motor2.setSpeed(0);[color=#222222][/color]
    motor1.run(FORWARD);[color=#222222][/color]
    motor2.run(FORWARD);[color=#222222][/color]
    delay(4740); [color=#222222][/color]
  }[color=#222222][/color]
  else {[color=#222222][/color]
    motor1.setSpeed(100); [color=#222222][/color]
    motor2.setSpeed(100);[color=#222222][/color]
    motor1.run(FORWARD);[color=#222222][/color]
    motor2.run(FORWARD);[color=#222222][/color]
  }[color=#222222][/color]
}

Please help to upgrade the code so that it go through all the space available.

IMG_20201209_151105_346.jpg

IMG_20201209_151105_346.jpg

Duplicate deleted

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Thank you for using code tags but unfortunately the forum software has a bug that causes tags to be added which makes the code block useless (not your fault)

Please turn of the WYSIWYG editor option in your forum settings and post the code again

That's a hard problem. IIRC, the early Roombas just wandered about randomly in the hope that they would get most of the floor space clean. I expect modern ones are a bit more clever.

Take a look at SLAM mapping - it lets you search and map as you go. To use it though, you will need to get some data about how far you have moved and in what direction. Right now all you have is the crudest kind of dead reckoning based on how long your motors ran.

Do some experiments - try to drive a square course that brings you back to the same point and you will likely find that you can't do so with any accuracy. You will need to solve that before you can map and navigate.

I assume from the picture that you're using an Uno. You may need something with more RAM. Probably a faster CPU too.