Jump to content
ELFORUM - Forumul electronistilor
gabitzu2006

Telecomanda IR cu arduino LED-uri

Recommended Posts

da, acum merge. Las pe alta data cele 2 butoane. Ca idee, ce trebuie sa fac ca sa imi mearga si code8 similar cu code9? ce secventa ar trebui copiata si adaptata? Si desigur, umblat la total iesiri si idxRel9 (sa va fi introdus si idxRel8?)

Link to comment
Share on other sites

1. Definesti idxRel8 (sau folosesti idxRel9-1, da' suna urat).

2. Adaptezi numarul si pozitia iesirilor.

3. Copiezi case code9 in code8 si adaptezi idxRel...

4. Adaugi code 8 la if((value != code9...

5. Adaugi un bloc if(relayStates[idxRel8] && bRecRepeat){...

6. Ar trebui sa fie suficient.

Edited by Liviu M
Link to comment
Share on other sites

Posted (edited)

Va salut!

Imi cer scuze ca nu am mai avut timp de proiect si postez abia acum. Am incercat sa modific codul cum a spus colegul mai sus, dar nu mi-a iesit. Montajul se comporta in felul urmator: orice buton apas, se aprinde led-ul corespunzator, insa apoi montajul nu mai reactioneaza deloc (led-ul respectiv ramane aprins). Comanda corespunzatoare code 8 nu face nimic, doar blocheaza montajul. In serial monitor, cand apas butonul corespunzator code 9, imi apare in continuu code 9. Cand apas butonul pentru code 8, imi apare o singura data code8. atasez codul pe care l-am incercat.

#include <IRremote.h>

#define NR_TOTAL_IESIRI 7
#define NR_IESIRI_UP_DOWN 5
int RECV_PIN = 2; // the pin where you connect the output pin of sensor
int relay1 = 4; // releu 1
int relay2 = 5; // releu 2
int relay3 = 6; // releu 3
int relay4 = 7; // releu 4
int relay7 = 8; // releu 5
int relay9 = 9;  //releu activat cat butonul stanga e apasat(volume up)
int relay8 = 10; //releu activat cat butonul dreapta e apasat(volume down)
int idxRel9 = 7;
int idxRel8 = 7;
int relayPins[] = {relay1, relay2, relay3, relay4, relay7, relay8, relay9};  //array with all the relays' pins
int relayStates[] = {0,0,0,0,0,0,0}; //the initial state of relays

unsigned long iRepeatCounter = 0;

#define code1 16753245    // code received from button no. 1
#define code2 16736925    // code received from button no. 2
#define code3 16769565    // code received from button no. 3
#define code4 16720605    // code received from button no. 4
#define code7 16712445    // code received from button no. 5
#define code5 16718055    // cod pt up
#define code6 16730805    // cod pt down
#define code9 16716015    // cod stanga (stanga/volume up)
#define code8 16734885    // code dreapta (dreapta/volume down)


IRrecv irrecv(RECV_PIN);
decode_results results;

bool bRecRepeat = false;

void setup() {
  Serial.begin(115200);
  irrecv.enableIRIn(); // Start the receiver  
  for(uint8_t i = 0; i < NR_TOTAL_IESIRI; ++i){
     pinMode(relayPins[i], OUTPUT);
//     pinMode (9, OUTPUT);
     digitalWrite(relayPins[i], LOW);
     
  }

  delay(5);
}


void loop() {
  static int idx = -1; //index iesire

  if (irrecv.decode(&results)) {
    unsigned long value = results.value;

//    Serial.print("Code = ");
//    Serial.print(value);

    iRepeatCounter = millis();          
    switch(value) {
       case code1: 
              idx = 0; //prima iesire, 
              relayStates[idx] ^= 1; //inverseaza starea
           break;
       case code2: 
              idx = 1; 
              relayStates[idx] ^= 1; 
           break;
       case code3: 
              idx = 2; 
              relayStates[idx] ^= 1; 
           break;
       case code4: 
              idx = 3; 
              relayStates[idx] ^= 1; 
           break;
       case code7: 
              idx = 4; 
              relayStates[idx] ^= 1; 
           break;
       case code5: //up 
              idx++; // selecteaza urmatoarea iesire
              idx %= NR_IESIRI_UP_DOWN; //daca e 4, ia-o de la capat
              relayStates[idx] = 1; //fa iesirea 1
           break;
       case code6: //down
              idx--; //selecteaza iesirea precedenta
              if(idx < 0){ //daca e negativa, ia-o pe cea mai mare
                idx = NR_IESIRI_UP_DOWN - 1;
              }
              relayStates[idx] = 1; //fa iesirea 1
           break;
       case code9: 
              relayStates[idxRel9] = HIGH;
              bRecRepeat = true;
              Serial.println("  code9 ");
           break;
       case code8: 
              relayStates[idxRel8] = HIGH;
              bRecRepeat = true;
              Serial.println("  code8 ");
           break;
       case REPEAT : 
              bRecRepeat = true;
//              Serial.print(" repeat ");
           break;
       default : 
              bRecRepeat = false;
    } //switch

//    Serial.print(" idx = ");
//    Serial.println(idx);

    if((value != code9) && (value != REPEAT)){
      bRecRepeat = false;
      relayStates[idxRel9] = LOW;
      digitalWrite(relayPins[idxRel9], LOW);      
    if((value != code8) && (value != REPEAT)){
      bRecRepeat = false;
      relayStates[idxRel8] = LOW;
      digitalWrite(relayPins[idxRel8], LOW); 
      
      for(uint8_t i = 0; i < NR_IESIRI_UP_DOWN; ++i){ // modifica iesirea in functie de testele anterioare.
        if(i == idx){
          digitalWrite(relayPins[i], relayStates[i]);
        } else {
          digitalWrite(relayPins[i], LOW);
        }
      } //for
    } //if(value != code9
    
    irrecv.resume(); // Receive the next value
  } //if (irrecv.decode(&results))

  if(relayStates[idxRel9] && bRecRepeat){
    digitalWrite(relayPins[idxRel9], HIGH);
//    Serial.println("tin apasat 9");
  } else {
    digitalWrite(relayPins[idxRel9], LOW);
    relayStates[idxRel9] = LOW;
//    Serial.println("sting 9");

  if(relayStates[idxRel8] && bRecRepeat){
    digitalWrite(relayPins[idxRel8], HIGH);
//    Serial.println("tin apasat 8");
  } else {
    digitalWrite(relayPins[idxRel8], LOW);
    relayStates[idxRel8] = LOW;
//    Serial.println("sting 8");
  }
    
  if((millis() - iRepeatCounter) > 120){ //mai mult de 120 ms de la ultimul cod receptionat => clear repeat 
    bRecRepeat = 0;
//    Serial.println("nici un buton apasat");
  }
  
  delay(10); 
}
}
} //loop 

@Liviu M daca mai ai timp si bunavointa...te rog, o mana de ajutor! Multumesc!

Edited by gabitzu2006
update
Link to comment
Share on other sites

Am gasit azi un proiect interesant, care face cam ce vreau eu. Nu am inteles de ce a fost nevoie sa foloseasca un ESP. Am incercat sa pun codul pe arduino, insa imi da eroare la compilare (ceva legat de biblioteca EEPROM). Chiar daca ar fi functionat codul, tot as mai fi avut nevoie de control pentru volum, deci codul ar fi trebuit modificat...

Pun aici linkul: https://www.muffsy.com/muffsy-relay-input-selector-4.html

Link to comment
Share on other sites

tot asa...aceleasi simptome

#include <IRremote.h>

#define NR_TOTAL_IESIRI 7
#define NR_IESIRI_UP_DOWN 5
int RECV_PIN = 2; // the pin where you connect the output pin of sensor
int relay1 = 4; // releu 1
int relay2 = 5; // releu 2
int relay3 = 6; // releu 3
int relay4 = 7; // releu 4
int relay7 = 8; // releu 5
int relay9 = 9;  //releu activat cat butonul stanga e apasat(volume up)
int relay8 = 10; //releu activat cat butonul dreapta e apasat(volume down)
int idxRel9 = 6;
int idxRel8 = 5;
int relayPins[] = {relay1, relay2, relay3, relay4, relay7, relay8, relay9};  //array with all the relays' pins
int relayStates[] = {0,0,0,0,0,0,0}; //the initial state of relays

unsigned long iRepeatCounter = 0;

#define code1 16753245    // code received from button no. 1
#define code2 16736925    // code received from button no. 2
#define code3 16769565    // code received from button no. 3
#define code4 16720605    // code received from button no. 4
#define code7 16712445    // code received from button no. 5
#define code5 16718055    // cod pt up
#define code6 16730805    // cod pt down
#define code9 16716015    // cod stanga (stanga/volume up)
#define code8 16734885    // code dreapta (dreapta/volume down)


IRrecv irrecv(RECV_PIN);
decode_results results;

bool bRecRepeat = false;

void setup() {
  Serial.begin(115200);
  irrecv.enableIRIn(); // Start the receiver  
  for(uint8_t i = 0; i < NR_TOTAL_IESIRI; ++i){
     pinMode(relayPins[i], OUTPUT);
//     pinMode (9, OUTPUT);
     digitalWrite(relayPins[i], LOW);
     
  }

  delay(5);
}


void loop() {
  static int idx = -1; //index iesire

  if (irrecv.decode(&results)) {
    unsigned long value = results.value;

//    Serial.print("Code = ");
//    Serial.print(value);

    iRepeatCounter = millis();          
    switch(value) {
       case code1: 
              idx = 0; //prima iesire, 
              relayStates[idx] ^= 1; //inverseaza starea
           break;
       case code2: 
              idx = 1; 
              relayStates[idx] ^= 1; 
           break;
       case code3: 
              idx = 2; 
              relayStates[idx] ^= 1; 
           break;
       case code4: 
              idx = 3; 
              relayStates[idx] ^= 1; 
           break;
       case code7: 
              idx = 4; 
              relayStates[idx] ^= 1; 
           break;
       case code5: //up 
              idx++; // selecteaza urmatoarea iesire
              idx %= NR_IESIRI_UP_DOWN; //daca e 4, ia-o de la capat
              relayStates[idx] = 1; //fa iesirea 1
           break;
       case code6: //down
              idx--; //selecteaza iesirea precedenta
              if(idx < 0){ //daca e negativa, ia-o pe cea mai mare
                idx = NR_IESIRI_UP_DOWN - 1;
              }
              relayStates[idx] = 1; //fa iesirea 1
           break;
       case code9: 
              relayStates[idxRel9] = HIGH;
              bRecRepeat = true;
              Serial.println("  code9 ");
           break;
       case code8: 
              relayStates[idxRel8] = HIGH;
              bRecRepeat = true;
              Serial.println("  code8 ");
           break;
       case REPEAT : 
              bRecRepeat = true;
//              Serial.print(" repeat ");
           break;
       default : 
              bRecRepeat = false;
    } //switch

//    Serial.print(" idx = ");
//    Serial.println(idx);

    if((value != code9) && (value != REPEAT)){
      bRecRepeat = false;
      relayStates[idxRel9] = LOW;
      digitalWrite(relayPins[idxRel9], LOW);      
    if((value != code8) && (value != REPEAT)){
      bRecRepeat = false;
      relayStates[idxRel8] = LOW;
      digitalWrite(relayPins[idxRel8], LOW); 
      
      for(uint8_t i = 0; i < NR_IESIRI_UP_DOWN; ++i){ // modifica iesirea in functie de testele anterioare.
        if(i == idx){
          digitalWrite(relayPins[i], relayStates[i]);
        } else {
          digitalWrite(relayPins[i], LOW);
        }
      } //for
    } //if(value != code9
    
    irrecv.resume(); // Receive the next value
  } //if (irrecv.decode(&results))

  if(relayStates[idxRel9] && bRecRepeat){
    digitalWrite(relayPins[idxRel9], HIGH);
//    Serial.println("tin apasat 9");
  } else {
    digitalWrite(relayPins[idxRel9], LOW);
    relayStates[idxRel9] = LOW;
//    Serial.println("sting 9");

  if(relayStates[idxRel8] && bRecRepeat){
    digitalWrite(relayPins[idxRel8], HIGH);
//    Serial.println("tin apasat 8");
  } else {
    digitalWrite(relayPins[idxRel8], LOW);
    relayStates[idxRel8] = LOW;
//    Serial.println("sting 8");
  }
    
  if((millis() - iRepeatCounter) > 120){ //mai mult de 120 ms de la ultimul cod receptionat => clear repeat 
    bRecRepeat = 0;
//    Serial.println("nici un buton apasat");
  }
  
  delay(10); 
}
}
} //loop 

multumesc pentru interventie!

in alta ordine de idei, in cele din urma as vrea sa adaug si un encoder sau doua push-butoane ca sa ”navighez” printre sursele de semnal (releele 1-5). Deocamdata sa reusesc sa trec de code 8 asta..

Link to comment
Share on other sites

Incearca codul de mai jos, ar trebui sa se apropie de ce vrei, da' nu-mi dau seama daca am "scapat" vreo conditie.

#include <IRremote.h>

#define NR_TOTAL_IESIRI 7
#define NR_IESIRI_UP_DOWN 5
int RECV_PIN = 2; // the pin where you connect the output pin of sensor
int relay1 = 4; // releu 1
int relay2 = 5; // releu 2
int relay3 = 6; // releu 3
int relay4 = 7; // releu 4
int relay7 = 8; // releu 5
int relay8 = 10; //releu activat cat butonul dreapta e apasat(volume down)
int relay9 = 9;  //releu activat cat butonul stanga e apasat(volume up)

int idxRel8 = 5;
int idxRel9 = 6;
int relayPins[] = {relay1, relay2, relay3, relay4, relay7, relay8, relay9};  //array with all the relays' pins
int relayStates[] = {0,0,0,0,0,0,0}; //the initial state of relays


unsigned long iRepeatCounter = 0;

#define code1 16753245    // code received from button no. 1
#define code2 16736925    // code received from button no. 2
#define code3 16769565    // code received from button no. 3
#define code4 16720605    // code received from button no. 4
#define code5 16718055    // cod pt up
#define code6 16730805    // cod pt down
#define code7 16712445    // code received from button no. 5
#define code8 16734885    // code dreapta (dreapta/volume down)
#define code9 16716015    // cod stanga (stanga/volume up)

IRrecv irrecv(RECV_PIN);
decode_results results;

bool bRecRepeat = false;

void setup() {
  Serial.begin(115200);
  irrecv.enableIRIn(); // Start the receiver  
  for(uint8_t i = 0; i < NR_TOTAL_IESIRI; ++i){
     pinMode(relayPins[i], OUTPUT);
//     pinMode (9, OUTPUT);
     digitalWrite(relayPins[i], LOW);
     
  }

  delay(5);
}


void loop() {
  static int idx = -1; //index iesire

  if (irrecv.decode(&results)) {
    unsigned long value = results.value;

//    Serial.print("Code = ");
//    Serial.print(value);

    iRepeatCounter = millis();          
    switch(value) {
       case code1: 
              idx = 0; //prima iesire, 
              relayStates[idx] ^= 1; //inverseaza starea
           break;
       case code2: 
              idx = 1; 
              relayStates[idx] ^= 1; 
           break;
       case code3: 
              idx = 2; 
              relayStates[idx] ^= 1; 
           break;
       case code4: 
              idx = 3; 
              relayStates[idx] ^= 1; 
           break;
       case code5: //up 
              idx++; // selecteaza urmatoarea iesire
              idx %= NR_IESIRI_UP_DOWN; //daca e 4, ia-o de la capat
              relayStates[idx] = 1; //fa iesirea 1
           break;
       case code6: //down
              idx--; //selecteaza iesirea precedenta
              if(idx < 0){ //daca e negativa, ia-o pe cea mai mare
                idx = NR_IESIRI_UP_DOWN - 1;
              }
              relayStates[idx] = 1; //fa iesirea 1
           break;
       case code8: 
              relayStates[idxRel8] = HIGH;
              bRecRepeat = true;
//              Serial.print("  code8 ");
           break;
       case code9: 
              relayStates[idxRel9] = HIGH;
              bRecRepeat = true;
//              Serial.print("  code9 ");
           break;
       case REPEAT : 
              bRecRepeat = true;
//              Serial.print(" repeat ");
           break;
       default : 
              bRecRepeat = false;
    } //switch

//    Serial.print(" idx = ");
//    Serial.println(idx);

    if((value != code8) && (value != code9) && (value != REPEAT)){
      bRecRepeat = false;
      relayStates[idxRel8] = LOW;
      digitalWrite(relayPins[idxRel8], LOW);      
      relayStates[idxRel9] = LOW;
      digitalWrite(relayPins[idxRel9], LOW);      
      
      for(uint8_t i = 0; i < NR_IESIRI_UP_DOWN; ++i){ // modifica iesirea in functie de testele anterioare.
        if(i == idx){
          digitalWrite(relayPins[i], relayStates[i]);
        } else {
          digitalWrite(relayPins[i], LOW);
        }
      } //for
    } //if(value != code9
    
    irrecv.resume(); // Receive the next value
  } //if (irrecv.decode(&results))

  if(bRecRepeat){
    if(relayStates[idxRel8]){
      digitalWrite(relayPins[idxRel8], HIGH);
//    Serial.println("tin apasat 8");
    } else if (relayStates[idxRel9]) {
      digitalWrite(relayPins[idxRel9], HIGH);
//    Serial.println("tin apasat 9");
    } 
  } else {
      digitalWrite(relayPins[idxRel8], LOW);
      relayStates[idxRel8] = LOW;
      digitalWrite(relayPins[idxRel9], LOW);
      relayStates[idxRel9] = LOW;
//    Serial.println("sting 8 si 9");
 }
    
  if((millis() - iRepeatCounter) > 120){ //mai mult de 120 ms de la ultimul cod receptionat => clear repeat 
    bRecRepeat = 0;
//    Serial.println("nici un buton apasat");
  }
  
  delay(10); 
} //loop 

 

Link to comment
Share on other sites

Posted (edited)

acum pare sa functioneze, cu o mica exceptie: led5 (de la pin 8) nu se aprinde daca apas butonul 5 pe telecomanda, insa daca rulez prin led-uri, se aprinde. partea cu idx8 si 9 functioneaza.

LE Am introdus si case7, acum functioneaza!

ar mai fi de adaugat partea cu pushbutoane sau cu encoder....

#include <IRremote.h>

#define NR_TOTAL_IESIRI 7
#define NR_IESIRI_UP_DOWN 5
int RECV_PIN = 2; // the pin where you connect the output pin of sensor
int relay1 = 4; // releu 1
int relay2 = 5; // releu 2
int relay3 = 6; // releu 3
int relay4 = 7; // releu 4
int relay7 = 8; // releu 5
int relay8 = 10; //releu activat cat butonul dreapta e apasat(volume down)
int relay9 = 9;  //releu activat cat butonul stanga e apasat(volume up)

int idxRel8 = 5;
int idxRel9 = 6;
int relayPins[] = {relay1, relay2, relay3, relay4, relay7, relay8, relay9};  //array with all the relays' pins
int relayStates[] = {0,0,0,0,0,0,0}; //the initial state of relays


unsigned long iRepeatCounter = 0;

#define code1 16753245    // code received from button no. 1
#define code2 16736925    // code received from button no. 2
#define code3 16769565    // code received from button no. 3
#define code4 16720605    // code received from button no. 4
#define code5 16718055    // cod pt up
#define code6 16730805    // cod pt down
#define code7 16712445    // code received from button no. 5
#define code8 16734885    // code dreapta (dreapta/volume down)
#define code9 16716015    // cod stanga (stanga/volume up)

IRrecv irrecv(RECV_PIN);
decode_results results;

bool bRecRepeat = false;

void setup() {
  Serial.begin(115200);
  irrecv.enableIRIn(); // Start the receiver  
  for(uint8_t i = 0; i < NR_TOTAL_IESIRI; ++i){
     pinMode(relayPins[i], OUTPUT);
//     pinMode (9, OUTPUT);
     digitalWrite(relayPins[i], LOW);
     
  }

  delay(5);
}


void loop() {
  static int idx = -1; //index iesire

  if (irrecv.decode(&results)) {
    unsigned long value = results.value;

//    Serial.print("Code = ");
//    Serial.print(value);

    iRepeatCounter = millis();          
    switch(value) {
       case code1: 
              idx = 0; //prima iesire, 
              relayStates[idx] ^= 1; //inverseaza starea
           break;
       case code2: 
              idx = 1; 
              relayStates[idx] ^= 1; 
           break;
       case code3: 
              idx = 2; 
              relayStates[idx] ^= 1; 
           break;
       case code4: 
              idx = 3; 
              relayStates[idx] ^= 1; 
           break;
       case code7: 
              idx = 4; 
              relayStates[idx] ^= 1; 
           break;
       case code5: //up 
              idx++; // selecteaza urmatoarea iesire
              idx %= NR_IESIRI_UP_DOWN; //daca e 4, ia-o de la capat
              relayStates[idx] = 1; //fa iesirea 1
           break;
       case code6: //down
              idx--; //selecteaza iesirea precedenta
              if(idx < 0){ //daca e negativa, ia-o pe cea mai mare
                idx = NR_IESIRI_UP_DOWN - 1;
              }
              relayStates[idx] = 1; //fa iesirea 1
           break;
       case code8: 
              relayStates[idxRel8] = HIGH;
              bRecRepeat = true;
//              Serial.print("  code8 ");
           break;
       case code9: 
              relayStates[idxRel9] = HIGH;
              bRecRepeat = true;
//              Serial.print("  code9 ");
           break;
       case REPEAT : 
              bRecRepeat = true;
//              Serial.print(" repeat ");
           break;
       default : 
              bRecRepeat = false;
    } //switch

//    Serial.print(" idx = ");
//    Serial.println(idx);

    if((value != code8) && (value != code9) && (value != REPEAT)){
      bRecRepeat = false;
      relayStates[idxRel8] = LOW;
      digitalWrite(relayPins[idxRel8], LOW);      
      relayStates[idxRel9] = LOW;
      digitalWrite(relayPins[idxRel9], LOW);      
      
      for(uint8_t i = 0; i < NR_IESIRI_UP_DOWN; ++i){ // modifica iesirea in functie de testele anterioare.
        if(i == idx){
          digitalWrite(relayPins[i], relayStates[i]);
        } else {
          digitalWrite(relayPins[i], LOW);
        }
      } //for
    } //if(value != code9
    
    irrecv.resume(); // Receive the next value
  } //if (irrecv.decode(&results))

  if(bRecRepeat){
    if(relayStates[idxRel8]){
      digitalWrite(relayPins[idxRel8], HIGH);
//    Serial.println("tin apasat 8");
    } else if (relayStates[idxRel9]) {
      digitalWrite(relayPins[idxRel9], HIGH);
//    Serial.println("tin apasat 9");
    } 
  } else {
      digitalWrite(relayPins[idxRel8], LOW);
      relayStates[idxRel8] = LOW;
      digitalWrite(relayPins[idxRel9], LOW);
      relayStates[idxRel9] = LOW;
//    Serial.println("sting 8 si 9");
 }
    
  if((millis() - iRepeatCounter) > 120){ //mai mult de 120 ms de la ultimul cod receptionat => clear repeat 
    bRecRepeat = 0;
//    Serial.println("nici un buton apasat");
  }
  
  delay(10); 
} //loop 

 

Edited by gabitzu2006
update
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.






×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.Terms of Use si Guidelines