Bueno, vamos avanzando.
He encontrado una libreria que va bien: RCswitch
Este es el código:
/*
Example for receiving
http://code.google.com/p/rc-switch/
If you want to visualize a telegram copy the raw data and
paste it into http://test.sui.li/oszi/
*/
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(0, output);
}
void loop() {
}
void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw) {
if (decimal == 0) {
Serial.print("Unknown encoding.");
} else {
char* b = dec2binWzerofill(decimal, length);
Serial.print("Decimal: ");
Serial.print(decimal);
Serial.print(" (");
Serial.print( length );
Serial.print("Bit) Binary: ");
Serial.print( b );
Serial.print(" Tri-State: ");
Serial.print( bin2tristate( b) );
Serial.print(" PulseLength: ");
Serial.print(delay);
Serial.println(" microseconds");
}
Serial.print("Raw data: ");
for (int i=0; i<= length*2; i++) {
Serial.print(raw[i]);
Serial.print(",");
}
Serial.println();
Serial.println();
}
static char* bin2tristate(char* bin) {
char returnValue[50];
int pos = 0;
int pos2 = 0;
while (bin[pos]!='\0' && bin[pos+1]!='\0') {
if (bin[pos]=='0' && bin[pos+1]=='0') {
returnValue[pos2] = '0';
} else if (bin[pos]=='1' && bin[pos+1]=='1') {
returnValue[pos2] = '1';
} else if (bin[pos]=='0' && bin[pos+1]=='1') {
returnValue[pos2] = 'F';
} else {
return "not applicable";
}
pos = pos+2;
pos2++;
}
returnValue[pos2] = '\0';
return returnValue;
}
static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength){
static char bin[64];
unsigned int i=0;
while (Dec > 0) {
bin[32+i++] = (Dec & 1 > 0) ? '1' : '0';
Dec = Dec >> 1;
}
for (unsigned int j = 0; j< bitLength; j++) {
if (j >= bitLength - i) {
bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];
}else {
bin[j] = '0';
}
}
bin[bitLength] = '\0';
return bin;
}
De mi mando recibo estos 4 códigos:
Decimal: 7805964 (24Bit) Binary: 011101110001110000001100 Tri-State: F1F10F100010 PulseLength: 616 microseconds
Raw data: 19104,572,1892,1812,664,1824,660,1816,668,572,1900,1808,668,1824,664,1816,668,568,1908,568,1900,572,1904,1808,668,1804,668,1816,672,576,1908,560,1900,560,1908,564,1900,572,1900,560,1900,1796,672,1812,672,572,1908,560,1896,
Decimal: 7806144 (24Bit) Binary: 011101110001110011000000 Tri-State: F1F10F101000 PulseLength: 616 microseconds
Raw data: 19120,576,1892,1808,664,1824,656,1820,660,576,1900,1812,660,1828,660,1816,664,576,1904,568,1904,576,1896,1808,664,1808,668,1812,668,584,1904,568,1896,1804,664,1812,668,576,1908,564,1900,560,1904,564,1900,576,1900,564,1896,
Decimal: 7805955 (24Bit) Binary: 011101110001110000000011 Tri-State: F1F10F100001 PulseLength: 616 microseconds
Raw data: 19116,572,1896,1808,668,1824,660,1816,672,568,1904,1808,668,1824,668,1812,672,568,1908,564,1904,572,1904,1808,668,1804,672,1812,676,572,1912,560,1900,564,1912,560,1900,564,1908,560,1900,564,1908,560,1900,1804,668,1812,676,
Decimal: 7806000 (24Bit) Binary: 011101110001110000110000 Tri-State: F1F10F100100 PulseLength: 617 microseconds
Raw data: 19132,572,1896,1808,664,1824,660,1816,664,576,1904,1808,664,1824,668,1812,668,572,1908,568,1900,576,1904,1804,672,1804,672,1812,672,580,1908,560,1904,564,1908,560,1904,1804,668,1808,676,564,1912,564,1904,572,1908,560,1900,
Bien, el tema está en como puedo modificar para que cuando reciba el primer código me encienda un led...... pero que se mantenga encendido hasta que no reciba nada. Vamos, mientras mantenga pulsado el boton, que el led se encienda, y si no lo pulso que se apague.
Este es el sketch que he utilizado
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
mySwitch.enableReceive(0, output);
}
void loop() {
}
void output(unsigned long decimal, unsigned int length, unsigned int lenght, unsigned int* raw) {
digitalWrite(13, LOW);
Serial.print(decimal);
while(decimal==7805964){
Serial.print(decimal);
decimal = 0;
digitalWrite(13, HIGH);
Serial.print(decimal);}
}
Lo he intentado de 1000 formas, pero no veo donde estoy fallando.
A ver si alguien me echa una mano.
Un saludo