HI
could anybody please correct this code to turn the motor opposit direction and continue when abutton is pressed and released . below are 2 codes that i have tried, but it doesn`t work.
code-1 (this code doesn`t work at all
int switchPin =2;
int motorPin1 = 3;
int motorPin2 = 4;
You need to detect when the button becomes pressed rather than when it is pressed. Each time you detect that it has become pressed flip the state of a variable to indicate the direction to rotate in and act on it .
Have a look at the StateChangeDetection example in the IDE to see the principle.
Thank you very much for all your replies. still I cant figure out how to do it. I am still learning. don`t have much knowledge in programming. if somebody could change one of above codes that will be really appreciated.
I am using this for hobby project- home made automatic car wash.
The high pressure hose traveling forward and back ward on a track. plan is when it reach the forward end it press a switch and then it travels backward. when it reach the back end same thing happens.
You will learn much more if you write it yourself but here is some pseudo code to help you
declare variables as usual
set direction variable to 0 (indicating stopped)
setup()
{
pinMode()s etc
}
start of loop()
if the start button becomes pressed (assuming that some action starts the process)
set direction variable to forward (maybe set it to 1 to indicate forward)
end if
if the stop button becomes pressed (assuming that some action stops the process)
stop the motor
set direction to 0 to indicate that the motor has stopped
end if
if direction is forward
run the motor in forward direction
if the forward limit switch becomes pressed
stop the motor
set direction to reverse (maybe set it to 2 to indicate reverse)
end if
end if
if direction is reverse
run the motor in reverse direction
if the reverse limit switch becomes pressed
stop the motor
set direction to forward (maybe set it to 1 to indicate forward)
end if
end if
end of loop()