I wrote a code to make it so when you press a button on an IR remote several times, it lights the correct LED. There are three options for this, so for example: in order to light the third led, you must press any button on the remote three times with one-second delay between every press maximum.
However, over time, that one-second delay gets faster, which is not intended. At first, it all works perfectly but then it starts checking so fast and always thinks it is only pressed once and only lights the LED number one.
Here is the code:
#include <IRremote.h>
int RECV_PIN = 13;
IRrecv irrecv(RECV_PIN);
decode_results results;
int UP = 10;
int STOP = 11;
int DOWN = 12;
int Times[] = {0,0};
void setup() {
pinMode(UP, OUTPUT);
pinMode(STOP, OUTPUT);
pinMode(DOWN, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop() {
if (irrecv.decode(&results)) {
if (Times[0] < 3) {
Times[0] = Times[0] + 1;
Times[1] = millis();
}
irrecv.resume();
}
if (millis() - Times[1] > 1000 and Times[1] != 0) {
if (Times[0] == 1) {
digitalWrite(UP, HIGH);
digitalWrite(STOP, LOW);
digitalWrite(DOWN, LOW);
}
else if (Times[0] == 2) {
digitalWrite(UP, LOW);
digitalWrite(STOP, LOW);
digitalWrite(DOWN, HIGH);
}
else if (Times[0] == 3) {
digitalWrite(UP, LOW);
digitalWrite(STOP, HIGH);
digitalWrite(DOWN, LOW);
}
Times[0] = 0;
Times[1] = 0;
}
}
Usually, array are used to hold like kinds of data. Your Times array does NOT hold like kinds of data. It holds the number of times a button was pressed and the time that the button was last pressed.
Use two variables for two separate things.
If the button is pressed exactly 1000 milliseconds later than last time, the effect will be useless. Use >= to compare times, not >.
Your Serial.print() statements, and output, do not support your claims.
You just telling me "long" helped me fix it! I used "int" in the second version of the code and after it passed the maximum int could take, it started breaking.
You don't HAVE any Serial.print() statements to help you learn what the program is doing. You are, therefore, debugging by guesswork, which is the hardest possible way to debug a program. Facts beat guesses EVERY time.