lastchancename:
since I expect I can enter the value '11', '22', 33' etc., and this would obviously imply 2 presses
What happens in-between when you press 1, 1, 1 ?
There will either be a 'no key', or 'key-released' message from the remote, or you can implement a timeout to clear prevKey (e.g. 100mS) ... then the next press will register as a newKey.
For my remote:
short press, it does not send any value other than the button value,
Long press, it sends 0xFFFFFFFF after the button value.
I've spent the morning testing, reconnecting, and re-writing the code.
I rewrote for a 2-digit final number to keep it simple, planning to restore to 3-digit when all working properly.
It seems that at the point I came, I need a debouncing for my remote, as I can get all 2-digit presses accumulated correctly at almost any attempt.
The newKey!=prevKey statement stopped the accumulation (i-e- I could only register 1 digit),
so I replaced this line with
if ( index < MaxChars ), which allows me to see the 2-digit complex number.
Increasing the delay time from 100 to 160 after the IRrecv helped a bit.
I even tried to add a flag bool, that was 'true' after IRrecv, then 'false' before each case's break. Without luck.
I don't expect to have someone to finish my code, but my lack of experience makes me stop at this point.
So any hint is more than welcome.
My updated (almost working) code, for 2-digit accumulation,
follows:
#include <IRremote.h>
/*
Variables
*/
int RECV_PIN = 11;
const int MaxChars = 2;
char strValue[MaxChars + 1]; // String for 3 digits + null char
char ch;
int index = 0;
int accumulVal = 0;
int newKey = 0;
int prevKey = 0;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
// In case the interrupt driver crashes on setup, give a clue
// to the user what's going on.
Serial.println("Enabling IRin");
irrecv.enableIRIn(); // Start the receiver
Serial.println("Enabled IRin");
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, DEC);
irrecv.resume(); // Receive the next value
}
delay(150);
newKey = results.value;
// switch / case function
if ( index < MaxChars ) { // check if a key and different than previous key
switch (newKey)
{
case 0x20DF8877 : // Button '1' on my remote
strValue[index++] = '1';
break; // assign number to button press
case 0x20DF48B7 : // Button '2' on my remote
strValue[index++] = '2';
break;
case 0x20DFC837 : // Button '3' on my remote
strValue[index++] = '3';
break;
case 0x20DF28D7 : // Button '4' on my remote
strValue[index++] = '4';
break;
case 0x20DFA857 : // Button '5' on my remote
strValue[index++] = '5';
break;
case 0x20DF6897 : // Button '6' on my remote
strValue[index++] = '6';
break;
case 0x20DFE817 : // Button '7' on my remote
strValue[index++] = '7';
break;
case 0x20DF18E7 : // Button '8' on my remote
strValue[index++] = '8';
break;
case 0x20DF9867 : // Button '9' on my remote
strValue[index++] = '9';
break;
case 0x20DF08F7 : // Button '0' on my remote
strValue[index++] = '0';
break;
case 0xFFFFFFFF : // Key depressed long
prevKey = 0; // clear
break;
} // end of cases
}
/*
Accumulation
*/
else
{
// here when the buffer is full or on the first non digit
strValue[index] = 0; // terminate the string with a 0
accumulVal = atoi(strValue); // use atoi to convert string to an int
index = 0; // reset index to receive other data
}
Serial.print("Accumulated value: ");
Serial.println(accumulVal, DEC);
} // loop end