I am new to arduino programming. I am building a robotic car using l298h motor driver to move the car forward,left ,right and stop using functions and HC-SR04 ultrasonic circuit to detect an object. Now I am having a problem that if an ultrasonic sensor detects an object it stops and then again it moves the direction it was going for example if robot is moving right and detects an object it stops and when that object removed it again start moving right.
Now I want an assistance in it to how to do it??
In your code to detect an object, you have to set a variable that indicates if an object is present or not and then the rest of your movement code has to look at that variable and either move or not.
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
It would make things much easier for yourself and others looking at your code if you would use more descriptive labels for your variables and functions. At the moment, while things are fresh in your mind, terms like in1, in2 etc for motor inputs might be meaningful, but to others it requires going back to the top and find the declarations. (How about leftMotorA ,l eftMotorB and rightMotorA , rightMotorB?
Something that you have mostly overlooked is comments on most of your statements. Properly worded comments makes it much easier to understand what that section of code is supposed to do. Also avoid adding comments to statements that basically repeat the statement, for example;
//Serial Port begin
Serial.begin (9600);
As best as I can tell what your code does is move the bot forward, or f in your case, for two seconds and using a timer 1 interrupt to look for an object 10 times per second. After two seconds the bot stops (s) and immediately goes into a left turn for two more seconds while pinging the SR04, and then turns right for two seconds pinging the SR04.
I do not see the (object) isr doing anything other than reporting a distance of less than five inches.
I would think that if the reported object is less than 5 inches from your bot then some type of movement modification is in order, such as STOPPING and then twist back and forth to scan the area with your single SR04 to locate a favorable path to travel.
In the object isr, is there any good reason for re-declaring the pinMode of the echoPin every .1 seconds? Isn't once in void startup enough?