Hello, tell me how to modify this code, so that when you hold the remote control button, the wheels spin, and as soon as you release the button, they stop spinning
#include "IRremote.h"
IRrecv irrecv(6); // указываем вывод, к которому подключен приемник
decode_results results;
unsigned long lastCommand;
int in1 = 2;
int in2 = 3;
int in3 = 4;
int in4 = 5;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
if ( irrecv.decode( &results )) {
switch ( results.value ) {
if (results.value != 0xFFFFFFFF){
lastCommand = results.value; }
if (results.value == 0xFF38C7){
lastCommand = results.value;
digitalWrite(in1,1);
digitalWrite(in2,0);
digitalWrite(in3,1);
digitalWrite(in4,0);}
if (results.value == 0xFFB04F){
lastCommand = results.value;
digitalWrite(in1,0);
digitalWrite(in2,1);
digitalWrite(in3,0);
digitalWrite(in4,1);}
}
irrecv.resume(); // принимаем следующую команду
}
}
The answer is right there. the FFFFFF's are a repeat code. So an "on" event would be defined:
"Some code is received". The response is to turn your device on.
But there is a catch. The condition to turn the device off is a "lack of repeat code" so you have to implement a detection of "it has been x time since the last repeat code was received".
So, you have to use millis() timing. Possibly also a state machine, it will probably be easier.
Helping you at this point, depends on knowing your programming skill level.
The bottom line is this, we receive signals from the remote, but when the button is held down, it starts sending FFFFFFFF, then we need a variable that will store the data that was before FFFFFFFF, it turns out I have to register if (results.value!= 0xFFFFFFFF){
lastCommand = results.value; }, that is, if we get the same code from the remote, then we assign it to our variable
So, that reply simply re-states the problem. You can call it the "bottom line" or whatever, fine. But the problem is not misunderstood. You explained it well enough already, or it's obvious.
What you have said, only solves a trivial part of the problem, recognizing the initial code. I doubt very much that it is what puzzles you. I think that is, how to recognize a key release.
So I gave you an outline of a solution in reply #5. But in keeping with the "self help" spirit of this forum, I don't want to spoon feed you code to do it. I want you to at least understand the solution. You said you did, but it's obvious that you don't. So please go back and read it again and try to understand what I said.
Or, the relevant part -
"The condition to turn the device off is a "lack of repeat code" so you have to implement a detection of "it has been x time since the last repeat code was received".
We'll move on to the state logic once you have grasped that. If you don't understand the basic principle, your chances of coding it successfully are almost zero.