How can I reset the results.value to 0 every press. e.g. when I press 2 my motor will go continuously but if I can sent the 0 value it will move while I'm pressing and stop when unpressed it
That is not something you can do easily with an IR remote.
If the remote sends a code repeatedly while the button is pressed or sends a special 'repeat' code while the button is still pressed, you can use a timer to stop your motor if the code or 'repeat' code has not arrived as often as expected.
Does your remote keep sending while the button is down?
It can be other number. Main thing that I want to do is to stop motor when not press it. This is one of my ideas that if I'm not pressed, results.value will be 0.
Remote controllers work in different ways when you hold down a button. Some will only send the button code once - so you can’t know when the key is released nor how long it was held down, some rare ones will send the same key every x ms as long as you hold the button down and most will send a generic repeat key code to mention that the key is still held down but it’s not a new press.
It is very likely your remote behaves like the latter one so you should trap that repeat code and program a timeout that says
If I receive a code
If it’s the repeat code
Then update the timeout moment
Else
Stop the current action and do the new one
Else If I did not receive a code (repeat or different) in the last x ms
Then stop
You need that only because your logic of reading of results.value is incorrect.
Look in the code - you read the IR code value at the start of the each loop() run, even if no new data was arrived. So if there is no button pressed, you get outdated results.value.
I think you should change the logic - Instead of unconditional reading of results, first test either a new data received with
irrecv.available()
method and get the new value if new data arrived only.