Sensor IR and Emissor turn on /off a TV

i have this code finish but i didn t understand very well about this. The code is to Turn on/off a TV with sensor IR and Emissor. When you press a button on any TV control pointing to the Receptor IR , this store the code on arduino . When you press the button on protoboard , arduino sends the code to the emissor and you pointing the emissor to the TV , so you can turn On the TV with this. This is the idea , but i didnt understand so well the program , can anyone make a good explanation of the program for me ?

#include <IRremote.h>

int RECV_PIN = 12;
int BUTTON_PIN = 7;
int STATUS_PIN = 13;

IRrecv irrecv(RECV_PIN);
IRsend irsend;

decode_results results;

void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(BUTTON_PIN, INPUT);
pinMode(STATUS_PIN, 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*USECPERTICK - MARK_EXCESS;

  • Serial.print(" m");*
  • }*
  • else {*
  • // Space*
    rawCodes[i - 1] = results->rawbufUSECPERTICK + 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;_
    _
    }_
    _
    }_
    void sendCode(int repeat) {
    _
    if (codeType == NEC) {_
    _
    if (repeat) {_
    _
    irsend.sendNEC(REPEAT, codeLen);_
    _
    Serial.println(“Sent NEC repeat”);_
    _
    }_
    _
    else {_
    _
    irsend.sendNEC(codeValue, codeLen);_
    _
    Serial.print("Sent NEC ");_
    _
    Serial.println(codeValue, HEX);_
    _
    }_
    _
    }_
    _
    else if (codeType == SONY) {_
    _
    irsend.sendSony(codeValue, codeLen);_
    _
    Serial.print("Sent Sony ");_
    _
    Serial.println(codeValue, HEX);_
    _
    }_
    _
    else if (codeType == RC5 || codeType == RC6) {_
    _
    if (!repeat) {_
    _
    // Flip the toggle bit for a new button press*_
    * toggle = 1 - toggle;*
    * }*
    * // Put the toggle bit into the code to send*
    * codeValue = codeValue & ~(1 << (codeLen - 1));*
    * codeValue = codeValue | (toggle << (codeLen - 1));*
    * if (codeType == RC5) {*
    * Serial.print("Sent RC5 ");*
    * Serial.println(codeValue, HEX);*
    * irsend.sendRC5(codeValue, codeLen);*
    * }*
    * else {*
    * irsend.sendRC6(codeValue, codeLen);*
    * Serial.print("Sent RC6 ");*
    * Serial.println(codeValue, HEX);*
    * }*
    * }*
    _ else if (codeType == UNKNOWN /* i.e. raw /) {_
    _
    // Assume 38 KHz*_
    * irsend.sendRaw(rawCodes, codeLen, 38);*
    * Serial.println(“Sent raw”);*
    * }*
    }
    int lastButtonState;
    void loop() {
    * // If button pressed, send the code.*
    * int buttonState = digitalRead(BUTTON_PIN);
    _
    if (lastButtonState == HIGH && buttonState == LOW) {_
    _
    Serial.println(“Released”);_
    _
    irrecv.enableIRIn(); // Re-enable receiver*_
    * }*
    * if (buttonState) {*
    * Serial.println(“Pressed, sending”);*
    * digitalWrite(STATUS_PIN, HIGH);
    _
    sendCode(lastButtonState == buttonState);_
    digitalWrite(STATUS_PIN, LOW);
    _
    delay(50); // Wait a bit between retransmissions*_
    * }*
    * else if (irrecv.decode(&results)) {*
    * digitalWrite(STATUS_PIN, HIGH);
    _
    storeCode(&results);_
    _
    irrecv.resume(); // resume receiver*_
    * digitalWrite(STATUS_PIN, LOW);
    _
    }_
    _
    lastButtonState = buttonState;_
    _
    }*_

Emissor.

I think you mean Emitter.

click the MODIFY button in the upper right of the post window. Highlight all you code. click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button. click SAVE (at the bottom). When you post code on the forum, please make a habit of using the code tags "#" button.