Go Down

Topic: Coding Using Time intervals help (Read 2638 times) previous topic - next topic


Hey all :), Iv been given a project at uni where part of it is for a device to drive around certain obstacles before reaching an end zone. For this iv decided to program a microcontroller which will control two motors on the device. I want to come up with a code that i will be able to use to turn on each of the motors at differnt times for differnt time intervals, as well as at the same time so that once the times are correctly entered into the mircocontroller the device should be able go along the track avoiding obstacles.

Im really struggling to get this started and was wondering if anyone would be able to help me out with a template? 

So far iv got this code, but am struggling to add ledPin2 in, aswell as continue the code so that a differnt time interval can be entered next.

int ledPin = 1;
int ledPin2 = 2;

void setup()
  pinMode(ledPin, OUTPUT);

void loop()
  digitalWrite(ledPin, HIGH); // Sets motor 1 on
  delay(10000);               // Stays on for  10 seconds
  digitalWrite(ledPin,LOW);   // Turns motor 1 off
  delay(1000);                // Off for 1 second

Thanks heaps


Seems like quite a big project to start with.

My advice would be to start by developing code to control the motors to start with, possibly using up/down and left/right controls. This will teach you how to manipulate the motors whilst you're doing it as well as working out timings. Here is a tutorial for a car http://arduinoarts.com/2011/08/arduino-tutorial-f1-rc-car-dc-motor-driven-by-mos-transistor/ here is a basic motor tutorial. http://www.oomlout.com/oom.php/products/ardx/circ-03 it includes wiring, explanations and a breadboard layout.

Then incorporate sensors to control the motors. I'm not sure what kind of avoiding action you want, but you can probably use just a few photoresistors to do the job in a very basic way i.e. stopping, reversing/turning, then repeating this if the obstacle is still in the way and continuing if not.

P.s. I'm not sure why you're using led's and havent wired up a motor yet. If you are a complete beginner, work through these tutorials before doign anything else (its how i was taught basic arduino at uni this year) http://www.oomlout.com/a/products/ardx/ youll just waste time trying to do something too hard.

Example pseudocode once you know how to control the motor:

go forwards
if (sensor says there is an obstacle in the way), stop
reverse motor
turn car (using two motors-one going left and one staying straight/going right)
stop car a few inches back
go forward in a straight line until more obstacles are met
repeat avoiding code

this is just one way to do it if you have say two sensors at the front.

good luck!


If all you are relying on is timing, you will never hit the end zone. Two motors given the same PWM value will not turn at the same speed, unless you are very, very lucky. It is a statistical certainty that two motors will not turn at the same speed for all possible PWM values, on all surfaces, at all times.

You really need a way to determine how much each wheel has caused the device to move, not just how far the wheel has turned. Even then, as you turn left and right, errors accumulate. For 10 foot runs with few turns, the error accumulation may not be significant. For a 100 foot run with dozens of turns, you could end up quite a ways off.
The art of getting good answers lies in asking good questions.


Hey all :), Iv been given a project at uni where part of it is for a device to drive around certain obstacles before reaching an end zone.

Do you actually have to show it working, or only describe your solution? Have you been told anything about the course you need to negotiate? Floor surface, how big it is, what sort of obstacles you can expect and how many, how accurately yo need to hit the end zone? Depending how tricky the challenge is you might be able to solve it using dead reckoning but there are plenty of situations where that wouldn't be adequate and you would need a far more complex solution.


Thanks maraesa1000 iv only had a chance to skim through those web sites at the moment but they seem very useful.
The obstacles are just a couple of blocks of wood on a straight track forcing the device to make a few turns, thats why i thought by timing each motor by a bit of trial an error id be able to make the device avoid the obstacles.
The LEDs were just there because i was thinking of using LEDs at first just to show my timing was working before attaching the motors.
Im thinking sensors may be the way to go now though,
Thanks :)


thanks PaulS, i guess i didnt really consider that too much. The track is about 2 meters long so thought it may not be too far off.


Thanks PeterH for the reply, the course is a straight track with a couple of blocks of wood in the way for the device to go around. I dont need to show working, just a final report in the end of how it was completed. We have to be relatively accurate in reaching the end zone as the device has to lift a baseball it has been carrying into a bucket, for this i have just built a scissor lift.


The above link is a picture of the track, hopefully you can open it. The start zone is in the bottom left hand corner, and it has to make it to the bucket at the other end of the track. Ignore the rope coming out of the bucket as that is for the second device.



your link asks me for a login (just FYI)


ahh damn, thought that might happen haha
Hopefully this works...

Heres a picture of the track:

Go Up