Hi All, can someone assist me to improve a code below? it is working as i would like but i need to loop on the condition when i press button number "1", so it keeps rotating motor from and back using function void rotateMotor(). but i also would like to be able to stop this function whenever pressing any other button or in example can be a button "5" that in my function it stop spinning motor at all. Will attach photo of the project as well so you may understand how it connected.
well, what i mean is i have 4 programmed button on IR remote control:
Buttons:
1 - spin motor clockwise 4 times with pause and counter clockwise 4 times.
2 - spin motor on maximum.
3 - spin motor in reverse on maximum.
5 - Stop motor.
So what i need help is to make whenever press button "1" motor start spinning clockwise and counter clockwise infinitely. But whenever i press any other button such as "2" or "5" it will come out from infinite function and perform actions from pressed button.
i hope this can help you to understand what i want to achive.
thank you
Very nice. Even better, you did the work. Have fun!
[edit]By the way... you just created a "finite state machine" (FSM) that does not rely on an action ("the button IS pressed") rather the action occurred ("the button WAS pressed") and then you changed states of the "infinite" flag. You will use FSM quite a bit when controlling devices. This is one example, but I would recommend finding other points of view on the FSM...
Really appreciate for this tip, i think on the example below i won't be able to avoid delay(), so i can create a pause that will make motor works for specific period of time before it will shutdown. Is there any other recommendation can be regarding if i need to make motor runs for a while?
In the future, do not attach pictures of your code.
Instead, attach your code as text so we can copy it and do changes to it.
The only time it might be acceptable to use delay(...) is when it is very short and doesn't repeat too often.
If delay()s are long and frequent, you should consider using the State Machine programming technique to incorporate non blocking delay periods,