Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

Ritorna ai Tutorial

la foto é grandina ma almeno non si fanno errori ;)

il ricevitore dei seganli IR é un TSOP 1738. il datasheet é qui

il codice utilizzato per legare ad un determinato codice IR un evento su Arduino é una modifica di Massimo fatta ad uno degli esempi (IRrecord) della libreria IR di Ken Shirriff.

Uploadando questo codice é possibile ricevere sulla porta seriale il codice del pulsante premuto sul telecomando. Se la libreria lo riconosce esso verra restituito in esadecimale sulla porta seriale. Andando a sostituire quel codice a quello usato da noi nel workshop (e cioé 0xA16EC03F, per esempio) sarà possibile comandare un output di Arduino

N.B. Ovviamente perché il codice funzioni dovrete installare la libreria nella cartella libraries di arduino ;)

N.B.2. il bottone sulla breadboard non serve a nulla ;) il codice é fatto per ricevere e rimandare un segnale IR. Questo esempio non implementa la seconda funzione. Più elementi su questo argomento in seguito.

un grazie ad andrea per avermi mandato l'ultima versione del codice.

/*
 * IRrecord: record and play back IR signals as a minimal 
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * An IR LED must be connected to the output PWM pin 3.
 * A button must be connected to the input BUTTON_PIN; this is the
 * send button.
 * A visible LED can be connected to STATUS_PIN to provide status.
 *
 * The logic is:
 * If the button is pressed, send the IR code.
 * If an IR code is received, record it.
 *
 * Version 0.11 September, 2009
 * Copyright 2009 Ken Shirriff
 * https://arcfn.com
 */

#include <IRremote.h>

#define led1 9
#define led2 10
#define led3 6

int RECV_PIN = 5;
int BUTTON_PIN = 7;
int STATUS_PIN = 13;
int r = 0;
int g = 0;
int b = 0;
IRrecv irrecv(RECV_PIN);
IRsend irsend;


int fading = 0; // se = 1 effetti luminosi

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(BUTTON_PIN, INPUT);
  pinMode(STATUS_PIN, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}

// Storage for the recorded code
int codeType = -1; // The type of code
unsigned long codeValue; // The code value if not raw
unsigned int rawCodes[RAWBUF]; // The durations if raw
int codeLen; // The length of the code
int toggle = 0; // The RC5/6 toggle state

// Stores the code for later playback
// Most of this code is just logging
void storeCode(decode_results *results) {
  codeType = results->decode_type;
  int count = results->rawlen;
  if (codeType == UNKNOWN) {
    Serial.println("Received unknown code, saving as raw");
    codeLen = results->rawlen - 1;
    // To store raw codes:
    // Drop first value (gap)
    // Convert from ticks to microseconds
    // Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
    for (int i = 1; i <= codeLen; i++) {
      if (i % 2) {
        // Mark
        rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
        Serial.print(" m");
      } 
      else {
        // Space
        rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
        Serial.print(" s");
      }
      Serial.print(rawCodes[i - 1], DEC);
    }
    Serial.println("");
  }
  else {
    if (codeType == NEC) {
      Serial.print("Received NEC: ");
      if (results->value == REPEAT) {
        // Don't record a NEC repeat value as that's useless.
        Serial.println("repeat; ignoring.");
        return;
      }
    } 
    else if (codeType == SONY) {
      Serial.print("Received SONY: ");
    } 
    else if (codeType == RC5) {
      Serial.print("Received RC5: ");
    } 
    else if (codeType == RC6) {
      Serial.print("Received RC6: ");
    } 
    else {
      Serial.print("Unexpected codeType ");
      Serial.print(codeType, DEC);
      Serial.println("");
    }
    Serial.println(results->value, HEX);
    codeValue = results->value;
    codeLen = results->bits;
  }
}

int lastButtonState;

void loop() {
  if (fading == 1) {
      analogWrite(led1,r);
      r=r+random(5);      
        r=r-random(5);
      delay(10);
      analogWrite(led2,g);
       g=g+random(5);
           g=g-random(5);
      delay(15);     
      analogWrite(led3,b);
       b=b+random(5);
           b=b-random(5);
      delay(5);
      Serial.println("random");


  }



  if (irrecv.decode(&results)) {
    digitalWrite(STATUS_PIN, HIGH);
    storeCode(&results);
    //Serial.println(results->value,HEX);
    if (codeValue == 0xA16EC03F) {
      analogWrite(led1,0);
      analogWrite(led2,0);
      analogWrite(led3,0);
      Serial.println("spento");
    }
    if (codeValue == 0xA16E34CB) {
      fading = 0;
      analogWrite(led1,255);
      analogWrite(led2,0);
      analogWrite(led3,0);
      Serial.println("r");
    }
    if (codeValue == 0xA16ED42B) {
      fading = 0;
      analogWrite(led1,0);
      analogWrite(led2,255);
      analogWrite(led3,0);
      Serial.println("g");
    }
    if (codeValue == 0xA16E54AB) {
      fading = 0;
      analogWrite(led1,0);
      analogWrite(led2,0);
      analogWrite(led3,255);
      Serial.println("b");
    }
     if (codeValue == 0xA16E0CF3) {
        fading = 1- fading;
    }

    irrecv.resume(); // resume receiver
    digitalWrite(STATUS_PIN, LOW);
  }

}


Ritorna ai Tutorial