Codici infrarossi con arduino:
Collegamenti del mio ricevitore IR visto di fronte (sono tutti diversi tra loro, se le spie si spengono sull'arduino stiamo tostando il nostro ricevitore):
Pin a sinistra: dati
Pin al centro: GND
Pin a destra: Vcc
Codice Arduino per ricevere dati infrarossi:
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
// decode_results *results = (decode_results *)v
void dump(decode_results *results) {
int count = results->rawlen;
if (results->decode_type == UNKNOWN) {
Serial.print("Unknown encoding: ");
}
else if (results->decode_type == NEC) {
Serial.print("Decoded NEC: ");
}
else if (results->decode_type == SONY) {
Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5) {
Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6) {
Serial.print("Decoded RC6: ");
}
else if (results->decode_type == JVC) {
Serial.print("Decoded JVC: ");
}
else if (results->decode_type == PANASONIC) {
Serial.print("Decoded Panasonic: ");
}
Serial.print(results->value, HEX);
Serial.print("(");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
Serial.print("#define Something_DEC ");
Serial.println(results->value, DEC);
Serial.print("#define Something_HEX ");
Serial.println(results->value, HEX);
Serial.print("Raw (");
Serial.print(count, DEC);
Serial.print("): ");
for (int i = 0; i < count; i++) {
if ((i % 2) == 1) {
Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
}
else {
Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
}
Serial.print(" ");
}
Serial.println("");
}
void loop() {
if (irrecv.decode(&results)) {
dump(&results);
irrecv.resume(); // Receive the next value
}
}
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
// decode_results *results = (decode_results *)v
void dump(decode_results *results) {
int count = results->rawlen;
if (results->decode_type == UNKNOWN) {
Serial.print("Unknown encoding: ");
}
else if (results->decode_type == NEC) {
Serial.print("Decoded NEC: ");
}
else if (results->decode_type == SONY) {
Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5) {
Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6) {
Serial.print("Decoded RC6: ");
}
else if (results->decode_type == JVC) {
Serial.print("Decoded JVC: ");
}
else if (results->decode_type == PANASONIC) {
Serial.print("Decoded Panasonic: ");
}
Serial.print(results->value, HEX);
Serial.print("(");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
Serial.print("#define Something_DEC ");
Serial.println(results->value, DEC);
Serial.print("#define Something_HEX ");
Serial.println(results->value, HEX);
Serial.print("Raw (");
Serial.print(count, DEC);
Serial.print("): ");
for (int i = 0; i < count; i++) {
if ((i % 2) == 1) {
Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
}
else {
Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
}
Serial.print(" ");
}
Serial.println("");
}
void loop() {
if (irrecv.decode(&results)) {
dump(&results);
irrecv.resume(); // Receive the next value
}
}
Nel monitor seriale dovremmo vedere codici come questi premendo i tasti del telecomando:
Unknown encoding: 731A3E02(32 bits)
#define Something_DEC 1931099650
#define Something_HEX 731A3E02
Raw (68): -6650 4500 -4450 550 -1700 500 -1700 550 -1700 500 -600 550 -550 550 -600 500 -600 500 -600 550 -1700 500 -1700 550 -1650 550 -600 500 -600 550 -550 550 -550 600 -550 550 -1650 550 -550 600 -550 550 -1650 550 -550 600 -550 550 -550 550 -550 550 -550 600 -1650 600 -1600 600 -550 550 -1650 600 -1650 550 -1650 600 -1650 550
Codice Arduino per inviare dati infrarossi:
Riporto qui un esempio di come inviare codici presi dal mio telecomando Samsung
#include "IRremote.h"
IRsend irsend;
void setup()
{
Serial.begin(9678);
}
unsigned int SOURCE [68]={4500,4450,550,1700,550,1650,550,1650,550,600,550,550,550,550,550,550,550,600,550,1650,550,1700,550,1650,550,550,550,550,550,600,550,550,550,550,550,1700,550,550,550,550,550,550,550,600,550,550,550,550,550,550,550,550,550,1700,550,1650,550,1700,550,1650,550,1700,550,1650,550,1700,550,};
unsigned int OK [68]={4500,4450,550,1650,600,1600,600,1650,600,500,600,500,600,550,550,550,550,550,600,1650,550,1650,600,1600,600,550,550,550,600,500,600,500,600,550,550,550,550,550,600,500,600,1650,550,550,600,1600,600,1650,600,500,600,1650,550,1650,600,1650,550,550,550,1650,600,500,600,550,550,1650,600,};
unsigned int UNO [68]={4550,4400,600,1600,600,1650,550,1650,550,600,550,550,550,550,550,550,550,550,600,1650,550,1650,550,1700,550,550,550,550,550,600,550,550,550,550,550,550,550,550,600,1650,550,550,550,550,550,600,550,550,550,550,550,1650,600,1650,550,550,550,1700,550,1650,550,1700,550,1650,600,1650,550,};
unsigned int DUE [68]={4550,4400,600,1600,600,1650,550,1650,600,550,550,550,550,550,600,500,600,550,550,1650,600,1600,600,1650,550,550,600,500,600,550,550,550,550,550,550,1700,550,550,550,1650,600,550,550,550,550,550,550,550,600,500,600,550,550,1650,600,500,600,1650,550,1650,600,1650,550,1650,550,1700,550,};
unsigned int QUATTRO [68]={4450,4450,500,1750,500,1700,500,1750,500,600,500,600,500,650,450,600,550,550,600,1650,500,1750,500,1650,550,600,550,600,500,600,500,600,500,600,550,550,550,600,550,550,500,1700,550,550,600,500,550,600,500,600,550,1700,500,1700,550,1700,500,550,550,1700,550,1650,550,1700,550,1700,500,};
unsigned int ALTO [68]={4500,4450,550,1650,600,1600,600,1650,600,500,600,500,600,550,550,550,550,550,600,1600,600,1650,600,1600,600,550,550,550,600,500,600,500,600,550,550,550,550,550,600,500,600,500,600,550,550,1650,600,1650,550,550,550,1650,600,1650,550,1650,600,1650,550,1650,600,500,600,500,600,1650,600,};
unsigned int DESTRA [68]={4550,4400,600,1650,550,1650,600,1650,550,550,550,550,600,500,600,500,600,550,550,1650,600,1650,550,1650,600,500,600,550,550,550,550,550,600,500,600,500,600,1650,550,550,600,500,600,550,550,1650,600,1600,600,550,550,1650,600,500,600,1650,550,1650,600,1650,550,550,550,550,600,1650,550,};
unsigned int SINISTRA [68]={4500,4450,550,1650,550,1650,550,1700,550,550,550,550,550,600,550,550,550,550,550,1650,550,1700,550,1650,550,600,550,550,550,550,550,550,550,550,550,1700,550,550,550,1700,550,550,550,550,550,1650,550,1700,550,550,550,550,550,1700,550,550,550,1650,550,1700,550,550,550,550,550,1700,550,};
unsigned int BASSO [68]={4500,4450,550,1650,550,1650,550,1700,550,550,550,550,550,600,550,550,550,550,550,1650,600,1650,550,1650,550,600,550,550,550,550,550,550,550,600,550,1650,550,550,550,550,550,600,550,550,550,1650,550,1700,550,550,550,550,550,1700,550,1650,550,1700,550,1650,550,550,550,550,550,1700,550,};
unsigned int VOLUMESU [68]={4500,4450,550,1650,600,1650,550,1650,600,500,600,500,600,550,550,550,600,500,600,1650,550,1650,600,1600,600,550,550,550,600,500,600,500,600,550,550,1650,600,1600,600,1650,600,500,600,500,600,550,550,550,550,550,600,500,600,550,550,550,550,1650,600,1650,550,1650,600,1650,550,1650,600,};
unsigned int VOLUMEGIU [68]={4500,4450,550,1650,600,1650,550,1650,600,500,600,550,550,550,550,550,600,500,600,1650,550,1650,600,1650,550,550,550,550,600,500,600,550,550,550,550,1650,600,1650,550,550,550,1650,600,550,550,550,550,550,600,500,600,500,600,550,550,1650,600,500,600,1650,550,1650,600,1650,550,1650,600,};
void loop() {
delay(5000);
irsend.sendRaw(DUE,68,38);
delay(1000);
irsend.sendRaw(QUATTRO,68,38);
delay(10000);
}
Riporto ora un esempio di come inviare
IRsend irsend;
void setup()
{
Serial.begin(9678);
}
unsigned int SOURCE [68]={4500,4450,550,1700,550,1650,550,1650,550,600,550,550,550,550,550,550,550,600,550,1650,550,1700,550,1650,550,550,550,550,550,600,550,550,550,550,550,1700,550,550,550,550,550,550,550,600,550,550,550,550,550,550,550,550,550,1700,550,1650,550,1700,550,1650,550,1700,550,1650,550,1700,550,};
unsigned int OK [68]={4500,4450,550,1650,600,1600,600,1650,600,500,600,500,600,550,550,550,550,550,600,1650,550,1650,600,1600,600,550,550,550,600,500,600,500,600,550,550,550,550,550,600,500,600,1650,550,550,600,1600,600,1650,600,500,600,1650,550,1650,600,1650,550,550,550,1650,600,500,600,550,550,1650,600,};
unsigned int UNO [68]={4550,4400,600,1600,600,1650,550,1650,550,600,550,550,550,550,550,550,550,550,600,1650,550,1650,550,1700,550,550,550,550,550,600,550,550,550,550,550,550,550,550,600,1650,550,550,550,550,550,600,550,550,550,550,550,1650,600,1650,550,550,550,1700,550,1650,550,1700,550,1650,600,1650,550,};
unsigned int DUE [68]={4550,4400,600,1600,600,1650,550,1650,600,550,550,550,550,550,600,500,600,550,550,1650,600,1600,600,1650,550,550,600,500,600,550,550,550,550,550,550,1700,550,550,550,1650,600,550,550,550,550,550,550,550,600,500,600,550,550,1650,600,500,600,1650,550,1650,600,1650,550,1650,550,1700,550,};
unsigned int QUATTRO [68]={4450,4450,500,1750,500,1700,500,1750,500,600,500,600,500,650,450,600,550,550,600,1650,500,1750,500,1650,550,600,550,600,500,600,500,600,500,600,550,550,550,600,550,550,500,1700,550,550,600,500,550,600,500,600,550,1700,500,1700,550,1700,500,550,550,1700,550,1650,550,1700,550,1700,500,};
unsigned int ALTO [68]={4500,4450,550,1650,600,1600,600,1650,600,500,600,500,600,550,550,550,550,550,600,1600,600,1650,600,1600,600,550,550,550,600,500,600,500,600,550,550,550,550,550,600,500,600,500,600,550,550,1650,600,1650,550,550,550,1650,600,1650,550,1650,600,1650,550,1650,600,500,600,500,600,1650,600,};
unsigned int DESTRA [68]={4550,4400,600,1650,550,1650,600,1650,550,550,550,550,600,500,600,500,600,550,550,1650,600,1650,550,1650,600,500,600,550,550,550,550,550,600,500,600,500,600,1650,550,550,600,500,600,550,550,1650,600,1600,600,550,550,1650,600,500,600,1650,550,1650,600,1650,550,550,550,550,600,1650,550,};
unsigned int SINISTRA [68]={4500,4450,550,1650,550,1650,550,1700,550,550,550,550,550,600,550,550,550,550,550,1650,550,1700,550,1650,550,600,550,550,550,550,550,550,550,550,550,1700,550,550,550,1700,550,550,550,550,550,1650,550,1700,550,550,550,550,550,1700,550,550,550,1650,550,1700,550,550,550,550,550,1700,550,};
unsigned int BASSO [68]={4500,4450,550,1650,550,1650,550,1700,550,550,550,550,550,600,550,550,550,550,550,1650,600,1650,550,1650,550,600,550,550,550,550,550,550,550,600,550,1650,550,550,550,550,550,600,550,550,550,1650,550,1700,550,550,550,550,550,1700,550,1650,550,1700,550,1650,550,550,550,550,550,1700,550,};
unsigned int VOLUMESU [68]={4500,4450,550,1650,600,1650,550,1650,600,500,600,500,600,550,550,550,600,500,600,1650,550,1650,600,1600,600,550,550,550,600,500,600,500,600,550,550,1650,600,1600,600,1650,600,500,600,500,600,550,550,550,550,550,600,500,600,550,550,550,550,1650,600,1650,550,1650,600,1650,550,1650,600,};
unsigned int VOLUMEGIU [68]={4500,4450,550,1650,600,1650,550,1650,600,500,600,550,550,550,550,550,600,500,600,1650,550,1650,600,1650,550,550,550,550,600,500,600,550,550,550,550,1650,600,1650,550,550,550,1650,600,550,550,550,550,550,600,500,600,500,600,550,550,1650,600,500,600,1650,550,1650,600,1650,550,1650,600,};
void loop() {
delay(5000);
irsend.sendRaw(DUE,68,38);
delay(1000);
irsend.sendRaw(QUATTRO,68,38);
delay(10000);
}
Riporto ora un esempio di come inviare
Codice Arduino per ricevere alcuni determinati codici infrarossi:
In questo codice attiveremo l'uscita 12 di arduino quando riceveremo un certo codice infrarossi, il seguente
Unknown encoding: C26BF044(32 bits)
#define Something_DEC 3261853764
#define Something_HEX C26BF044
Raw (68): 4446 4500 -4450 eccetera eccetera...
Qui il codice per attivare l'uscita ricevuto il nostro DETERMINATO codice IR (aggiungiamo semplicemente "0x" al codice esadecimale):
#include <IRremote.h>
//il codice seguente è per ricevere dati sul led infrarossi ricevente
int receiver =7 ;
IRrecv irrecv(receiver);
decode_results results;
boolean noresult = false;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop()
{
leggiSensoreIR();
if (results.value == 0xC26BF044 && noresult == false) //premuto tasto avanti
{
digitalWrite(12, HIGH);
}
}
void leggiSensoreIR(){
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume();
noresult = false;
Serial.println(noresult);
}else{
noresult = true;
}
}
//il codice seguente è per ricevere dati sul led infrarossi ricevente
int receiver =7 ;
IRrecv irrecv(receiver);
decode_results results;
boolean noresult = false;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop()
{
leggiSensoreIR();
if (results.value == 0xC26BF044 && noresult == false) //premuto tasto avanti
{
digitalWrite(12, HIGH);
}
}
void leggiSensoreIR(){
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume();
noresult = false;
Serial.println(noresult);
}else{
noresult = true;
}
}