I am building an arduino rc car with 2 ultra sonic sensors (HC-SR04)

They are operating properly i simulated them !

Thanks i will simulate the whole system !

So i did that but i still havent figured it out, I somehow need to put the sensors code in the void loop() and make it consistently scan for obstacles, not just once in the start. Hmm..

What is it that you did? What have you not figured out? Don't try to solve it all at one time. Start with something that seems small, describe that, work on that until it work... then another small part.

What you are seeing is the need and use of resources. The sonar takes time to complete its task. The motors take time to complete their task. They are competing for CPU time, because only one thing can happen at a time... There are advanced ways to do this, but you should get this working first before moving into the more advanced methods. If you want to explore... look at MobaTools library for the motors... but I only know it exists, and not how to use it.

Step one... what is first on the list of small things to correct?

So pretty much everything works fine , the robot goes straight,right,left etc. It also doesn't go forward or backward if an obstacle is there before it starts moving. So the first step i guess is to make the sensors scan consistently while it goes forward ?

Maybe I have read this wrong, but what you wrote is the purpose of the sensor... if an object is in front (behind), do not go forward (backward).

Your code (in the simulation) attempts to move away from the obstacle. Except for the need (later) to make the steering move the vehicle clear of the obstacle, the code (in the sim) works.

What am I not seeing that you see?

That would mean no driving, if the scanners had all the CPU time (again... in the advanced MobaTools, you can do something like time-share between scanning and driving).

This level of object avoidance goes like this:
0. Stop moving.

  1. Scan forward.
  2. If no object, drive forward
  3. If object, look left and right.
  4. If no object left, drive left
  5. If no object right, drive right

MobaTools is here if you want to explore... and the author is @MicroBahner (flagging his name sends him a note)

1 Like

Unless thats too much stuff at once, im relatively new to arduino and it's coding so sorry if im saying stuff that doesn't make that much sense.

I think taking care of the current project to completion will show you what you can do... and with side-study on the advanced coding, you will be doing more advanced things.

I noticed that you use motor movement to "look" left and right. Usually a servo motor is used to do that, but motor movement is just as good.

Enhance your view of the project with a drawing, rather than having the driving occur as lines of code or LEDs (more helpful), drawing a road map and placing a car and an obstacle on it, noting how you need to tell the robot to drive out by pushing the car around (obstacle forward... stop... look right... scan... obstacle... look forward... look left... scan... clear... drive left... and go through that for obstacles in all six positions (front left, front, front right, rear left, rear, rear right)... and note all the movements you need... and ask questions any time.

[edit] I just looked at MobaTools... I did not see DC motors in the list of things it controls.

1 Like

@akisv - Here is another object avoiding simulation. The sketch puts random obstacles in the cars way in front or to the left or to the right. The car look left, looks right, decides which direction is better to travel, turns in that direction and moves "forward" in that direction. I have removed the LEDs (for motors) and started to put stepper motor (bad choice), so you will not see the motors move yet. The sim is to show the decisions your car can make before moving.

1 Like

Yes, regarding motors only steppers are supported. With DC motors it should be more easy, because the turn by themselfs if power is turned on. But with steppers you need to constantly create steps pulses to get them turning.
So with DC-motors it should be simple to get the sensors scan consistantly while the motors are driving.

1 Like

Thanks for that, i removed the scanning steps because i asked my professor and it is not needed. So to make it simpler i just want to make it stop and not start if an obstacle is in the way , and then go if it is removed ..

That sounds very good. Be sure to check in all directions before deciding to stop or move (rotate to left-front/front/right-front, right-rear/rear/left-rear, check for obstacles in all six areas, register distances, rotate to best position, stop/move).

When extra work does not interfere with your other work, look into @MicroBahner mention of "time-sharing" to scan while moving

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.