I'd like to switch the direction of a brushed dc motor if has too much resistance. I want the motor to open and shut a door, but if the door is already shut (and senses resistance, to go the opposite way for an allotted amount of time.
Actual switching of motor will be done by L293D motor control IC
Something like this:
defualt (all outputs LOW)
scenario one:
if (button is pressed)
{pin1 = HIGH; pin2 = LOW; } // try to move motor in default direction
if (motor resistance is lower than x) && (millis is less than 500)
{pin1 = HIGH; pin2 = LOW; } // we're good - default direction of motor
when (motor feels resistance again (door is fully open), then pin 1 and 2 LOW
scenario two:
if (button is pressed)
{pin1 = HIGH; pin2 = LOW; } // try to move motor in default direction
if (motor resistance is MORE THAN x) && (millis is less than 500)
{pin1 = LOW; pin2 = HIGH; } // we're not good - motor go opposite direction
when (motor feels resistance again (door is fully closed), then pin 1 and 2 LOW
I'm quite lost because the code seems contradictory to itself every time I try to write it out. For example, I ask to look for motor resistance, than switch directions to bring the resistance down, then look for resistance again to stop the motor...I am confused with this one???
To do what you want, you need a way to measure the current going to the motor. When the motor tries to close a door that doesn't want to close, the current will go up, because the motor is working harder. The chip you are using does not measure the current.
Unfortunately, the "stall" current is the same amount of current that the motor needs to start moving, so you program will have to determine if the motor is just starting to close a door, or if it is trying to move a door that is already closed.
Another problem you have not addressed is how does the motor know the door is fully open?
To measure the current drawn by the motor, you need to place a resistor between the power source ground and the negative side of the motor. Measure the voltage across the resistor produced by the motor current. This will tell you about the current changes in the motor.
Figure out the answers to the above and then you will have better ideas for the code.
Paul, thanks dor youe reply. Im not having a problem with reading current draw. I found a way via voltage divider finding a max value which an analogRead knows is too much current draw. More to it than that, but anyway....
It's the code I'm having issues with about validating a current load that is too high, then, asking it to reverse direction until current is too high again, then stopping code. Just can't wrap my head around it.