Hi, I am new to Arduino, just start to do a little car .
But very confused with program here.I am doing a little car with two motors, I will put a bump sensor in the front , so if it is bumped, the motor will backwards spin for a while (car reverse), and then turn(one motor spin, another stops).
However, I really don't know how to set this in program. I put the bump sensor as NO(normally open,open circuit ),if it is bumped, so the two pins voltage for the sensor will be same(closed circuit) ,then start the loop (supple backward voltage to motors and it will bakwards spin for 5sec?--->then forward voltage for one motor , stop another motor spining for 5secs?--->then back the normal, both forward spinning).
But the thing is how can Arduino monitoring the pins around sensor? I mean the bump happen in a very short time, that means the Arduino UNO should read the pin voltage value all the time constantly, like every 0.001s per time ,can Arduino do that? I would use "if " to realise this, but is it capable for this situation? or just I am thinking overhead?
Sorry, I am very new to it.please help me out here. and please tell me if any wrong for my program algorithm, all comments will be appreciated. thanks.
I have a program that changes the state of an output bit every 100microseconds, reads an input to see if it matches the toggle bit, and counts the number of matches in a row.
A simple open/close every 1 millisecond is not a problem.
Set up a blink without delay program - you will monitor millis(), and whenever 1mS has gone by you do something:
see if you switch is tripped, see how long you've been going backwards, see if enoughtime has gone by & start turning, etc.
Welcome in Arduino land,
Take pen and paper and imagine you are the car with sensors and motors.
Write down what you must do to start, to sense a bump, to reverse, to turn, to stop.
It might seem foolish to do but you get a good understanding what the code should do.
Because your new I advise you to spend some time in the tutorial section and go through the examples. It gives you a quite good understanding of some basic techniques used .
Hopes this gets your started,
Rob
like every 0.001s per time ,can Arduino do that?
In that time, your Arduino could execute 16 000 instructions.
Time to stop thinking in human timescales.
Once you know what your program needs to do, you can start to think about how you will achive it. This means writing some software to take sensor readings and respond to them, and designing the hardware to control the motors and make things happen.
You can use an h-bridge to control the direction of the motor. By using the PWM outputs, you can also change the speed of the motors.
There are several methods of checking the switch state. You could do it the simple way using digitalRead, you could use interrupts, or you could challenge yourself by trying to do it with port manipulation. In programming, there is (almost) always several methods of doing something.
H-bridges: http://itp.nyu.edu/physcomp/Labs/DCMotorControl
analogWrite (PWM): analogWrite() - Arduino Reference
digitalRead: http://arduino.cc/en/Reference/DigitalRead
port manipulation: Arduino Reference - Arduino Reference
interrupts: http://arduino.cc/en/Reference/AttachInterrupt
If I remember correctly, the first arduino program that I wrote was to do the same thing. My lack of knowledge at the time meant that I would have fried my arduino if I tried to make it work. Good job I did not test it! XD
Good luck!
Onions.
Hello again!
I decided to write a library to simplify controling motors. Using the tutorial I posted above and my library here, you can control DC motors easily, using the right(), left() and stop() functions. If you choose to use it, let me know! (And if it works or not).
Onions.