I am trying to control a motor using a remote with Arduino Mega. The motor turns on when I press the button on the remote but when I release the button, it continues to move. Does anyone know how to stop the motor spinning when I release the button on the remote?
For an infra-red remote control you generally can't. Those don't normally have the concept of button release.
Normally there are two kinds of buttons on a remote:
- Burst code
- Repeat code
The first sends a short burst of the same code. This allows for a function to be run once per button press. The second sends the same code over and over again constantly until you release it.
For the repeat code buttons you can use that to your advantage. You program your Arduino to run the motor for a short period. That period needs to be slightly longer than the time between each code reception. Then it can appear that it's responding to the release of the button.
For example, if your remote sends codes 5 times per second then whenever you receive a code you start the motor. If you haven't received a code for more than say 300ms then stop the motor.