# Coding a 12vDC motor to chaange direction with H bridge

Hi guys an gals, Just wondering, Im using a 12vDC motor (Crouzet 828630) to turn a belt, on the belt there will be a sort of tab (or trigger for want of a better word), when the belt goes around the tab hits a microswitch and the motor stops for 15 seconds. This happens 5 times. Then i want the motor to do the same, only in the opposite direction. I know you can have a h bridge to change direction of a motor via a SPST switch. but can I have the motor change direction say after x amount of time or after x amount of stops. Can I code it in? Examples or a link would put a highly strung out newbie student to tremendous rest. Thanks guys

mixthetricks: can I have the motor change direction say after x amount of time or after x amount of stops.

Yes.

``````const unsigned long DelayAtEachStop = 15000;
unsigned long TimeOfLastStop = 0;
boolean MovingForward = true;
int MovementCount = 0;

void loop() {
unsigned long currentTime = millis();

if (currentTime - TimeOfLastStop > DelayAtEachStop) {
TimeOfLastStop = currentTime;
moveToNextStop();
MovementCount++;
if (MovementCount > MaxMoves) {
MovementCount = 0;
MovingForward = !MovingForward;  // Reverse direction
}
}
``````

Sure, that is what a H-bridge is for.

With a H-bridge, there are two (sometimes one) pins for the direction, and one for the speed. In the sketch you can do anything with it, any timing, delay or sequence.

Can you post a link to that motor ? Does it require a lot of current.

If you read a microswitch with an Arduino, you should be aware that a microswitch will bounce, like most switches do.

Erdin:
Sure, that is what a H-bridge is for.

With a H-bridge, there are two (sometimes one) pins for the direction, and one for the speed.
In the sketch you can do anything with it, any timing, delay or sequence.
The term ‘sketch’ you relate to?? Whats that?? Could you give me an example of the above circumstances in code?? It’d be really great if you, or anyone could.
Im not familiar with the ‘bounce’ factor. Hold up, I am now. its not an issue, I have the tab tapered down so there’ll be now springiness. but thanks for pointing it out.
http://www.crouzet.com/english/catalog/dc-motors-d-c-geared-motors-with-brushes-2-nm-re1-3-9-watts-3-9-w-Part%20number-12818.htm

The program that you write for the Arduino board is called a "sketch".

Almost every switch bounces. The contacts will connect, bounce a little back and connect again. This happens a few times before the connection is steady. The Arduino is fast enough to read all those ugly bounces.

Your motor is a brushed 12V DC 3W motor. Almost every motor driver board will work with it. The Arduino Motor Shield and the Adafruit Motor Shield are often used. Sparkfun has a nice selection of motor drivers, for example this one, https://www.sparkfun.com/products/9457

Thats really great. Thanks a lot guys. Could you do me a favour and maybe explain what each of these last few lines mean because although I see the "15000" variable for the "stop time" (in the code above) i don't see anything in the code to tell it to stop when the microswitch is activated.

if (currentTime - TimeOfLastStop > DelayAtEachStop) { TimeOfLastStop = currentTime; moveToNextStop(); MovementCount++; if (MovementCount > MaxMoves) { MovementCount = 0; MovingForward = !MovingForward; // Reverse direction

Thanks a mill guys. Life savers. PS...Feel free to jump in anyone :)

It is all about millis(). http://arduino.cc/en/Reference/Millis

millis() is a function that returns the value of a 32-bit software counter. The counter is counting milli seconds. It is used a lot in the Arduino.

If DelayAtEachStop = 15000, meaning 15 seconds. Suppose the TimeOfLastStop was 20000. Something should be done if millis() reaches 35000 (15000 + 20000). That is what the "if" statement is doing. if( 35000 - 20000 > 15000 ) ..... After that, remember the current millis() time.

Once in a while, the moveToNextStop() function is called. Each time a counter is incremented. If it is above MaxMoves, the direction is reversed, and the counter is reset to zero to be able to start counting again the next time.

This line "MovingForward = !MovingForward;" is with a boolean variable. It can be 'true' or 'false'. The "!" is a "not". If you read the code line, you say "not". It changes 'true' into 'false' and vice versa.