Sari la conținut
ELFORUM - Forumul electronistilor

Telecomanda IR cu arduino LED-uri


gabitzu2006

Postări Recomandate

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 spre comentariu
  • Răspunsuri 58
  • Creat
  • Ultimul Răspuns

Top autori în acest subiect

  • gabitzu2006

    31

  • Liviu M

    28

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.

Editat de Liviu M
Link spre comentariu
  • 3 luni mai târziu...

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!

Editat de gabitzu2006
update
Link spre comentariu

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 spre comentariu

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 spre comentariu

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 spre comentariu

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 

 

Editat de gabitzu2006
update
Link spre comentariu
  • 1 an mai târziu...

Iata ce am mai facut legat de acest proiect. Atasez fisierele EAGLE pentru schema.

 

Codul este cel realizat de @Liviu M

#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 

Montajul l-am realizat deocamdata pe breadboard. In loc de o punte H cu tranzistoare bipolare (mie unul nu a vrut sa imi invarta motorasul de test -unul chinezesc de 3-6V) am folosit un circuit specializat (tot chinezesc). Nu am testat montajul si pe partea de audio. E posibil ca CI de control al motorului sa fie zgomotos. Pe schema apare un al 6-lea releu, pe care as vrea sa-l folosesc pentru functia de stand-by. As vrea sa mai introduc un buton care la fiecare apasare sa-mi incrementeze sursa de semnal. Dar nu ma pricep sa introduc modificarile astea in cod. Deci daca stie cineva si vrea sa continuie, i-as fi recunoscator. La fel, daca vrea cineva sa realizeze PCB-ul, este liber s-o faca, eventual sa posteze PCB-ul. Voi incerca si eu sa proiectez un PCB, insa va dura ceva timp pana il termin.

Codul functioneaza, cu un mic dezavantaj. Daca este cuplata sursa 2 sa zicem, daca apas pe butonul 3, ca sa imi comute pe sursa 3, nu imi comuta pe sursa 3 ci imi decupleaza sursa 2. Pentru a comuta pe sursa 3 trebuie sa mai apas inca o data pe buton 3. La schimbarea surselor din butoanele up si down nu este acest inconvenient.

Asadar, daca e cineva dispus sa codeze...eu sunt dispus sa testez.

Va multumesc si sper sa va fie de folos ce am facut pana acum!

comanda.sch relee.sch

Link spre comentariu
  • 11 luni mai târziu...

Salutare! Am renuntat la proiectul cu arduino si am realizat selectorul de intrari de pe musffy. Merge bine, poate fi comandat atat prin IR cat si din encoder. Ce nu stiu insa sa fac e sa scriu cateva linii de cod pentru a putea controla volumul (prin comanda motorului potentiometrului). Daca poate cineva sa ma ajute...i-as fi recunoscator! Multumesc! atasez codul de pe site-ul respectiv.

 

 

/* Muffsy Relay Input Selector
 *      
 * Control relays using IR and rotary encoder
 * Control external power to amp using IR and push button
 * LM3886
 *
 * Last update with functionality changes: 2023-09-27
 *
 * Introduction:
 * The software works on the concept of "powerState", which determines the behavior of the Input Selector:
 * 
 * 0: Boot
 *   Read last input channel from NVRAM, selecting the same input as when the input selector was turned off or lost power 
 *   The input selector is muted, and mute LED is turned off 
 *   Print startup message
 *   powerState is changed to 2 (off)
 *
   2: Power OFF
 *   Turn off all relays
 *   Set power amp to off (SSR = LOW)
 *   The input selector is muted, and mute LED is turned off
 *   Limited functionality, only IR powerbutton and rotary encoder pushbutton is active
 *   All input events, even those not available, will be shown in the serial monitor
 *        
 * 1: Power ON
 *   Turn on power button LED
 *   Set power amp to on, SSR = HIGH
 *   Activates the input channel read from NVRAM
 *   Will start muted with mute LED turned on
 *   No actions are read until the input selector unmutes after 1500 ms (default)
 *   All functions available, IR Remote and rotary encoder
 *   All input events and actions will be shown in the serial monitor
 */

/*
 * Load libraries
 */
#include <EEPROM.h> // EEPROM Library
#include <IRremote.h> // IR Remote Library
#include <Versatile_RotaryEncoder.h> // Rotary Encoder Library

/*
 * Startup delay
 *
 * When powering on, the input selector is muted to avoid any unwanted pops
 * Set the delay value in milliseconds (default 1500)
 *
 * Change to 0 for no delay
 */
#define startupDelay 1500

/*
 * Enable SSR
 *
 * If you want to use a Solid State Relay to control mains power to an
 * amplifier or similar, set this value to 1
 *
 * Default 0 (disabled), as handling mains power is dangerous
 */
 #define enableSSR 0

/* Mute and Power on / off:
 *
 * The rotary encoder is also a push button, which registers
 * either a short or a long press. These are their functions:
 *
 *    Short press: Power on / off
 *    Long press: Mute
 *
 * To reverse this behaviour, change the value below to 1 (default = 0)
 */
 #define encPush 0

/* 
 * Rotary encoder direction.
 *
 * If the rotary encoder rotation is the wrong way around, change this value to 1
 * Default: 0
 */
 #define encDirection 0

/* 
 * Rotational encoder rate
 *
 * Delay between registering rotational encoder turns, in milliseconds.
 * If the repeat rate when turning the rotational encoder is too fast, change this timer.
 *
 * Note:
 * Introducing a delay in reading turns of the rotational encoder may skip one or more
 * of the encoders "clicks". In turn, it prevents you from skipping channels if you turn
 * the encoder to fast.
 *
 * Lower number: Faster repeat rate
 * Higher number: Slower repeat rate
 * Default: 0
 */
#define rotRate 0

/* 
 * IR rate
 *
 * Delay between registering IR commands, in milliseconds.
 * If the repeat rate when holding down a button on the remote is
 * to slow or too fast, change this timer.
 *
 * Lower number: Faster repeat rate
 * Higher number: Slower repeat rate
 * Default: 175
 */
#define irRate 175

/*
 *IR Commands
 *
 * The following variables map all the buttons on the remote
 * control that comes with the Muffsy Input Selector.
 *
 * For instructions on how to configure your own remote, see:
 * https://www.muffsy.com/muffsy-relay-input-selector-4#configureremote
 *
 * While connecting your Input Selector to the computer, it will display
 * any remote commands in the Arduiono IDE's Serial Monitor like this:
 *
 *    [LM3886]: Received IR code: <IR code of button pushed>
 *
 * There are placeholder commands in the function irRemote{} for the buttons
 * 0 and 5 to 9 that serves as templates for your own IR routines
 *
 */
#define zeroButton 13       // Placeholder command in the function irRemote{}, default: 13
#define oneButton 1        // Input 1, default: 12
#define twoButton 2        // Input 2, default: 24
#define threeButton 3      // Input 3, default: 94
#define fourButton 4        // Input 4, default: 8
#define fiveButton 28       // Placeholder command in the function irRemote{}, default: 28
#define sixButton 90        // Placeholder command in the function irRemote{}, default: 90
#define sevenButton 66      // Placeholder command in the function irRemote{}, default: 66
#define eightButton 82      // Placeholder command in the function irRemote{}, default: 82
#define nineButton 74       // Placeholder command in the function irRemote{}, default: 74
#define eqButton 70         // Default value: 70
#define modeButton 68       // Default value: 68
#define muteButton 16       // Mute, default: 71
#define ffButton 27         // "Fast Forward" button: Channel up, default: 21
#define rewButton 31         // "Rewind" button: Channel down, default: 7
#define powerButton 18      // Power on / off, default: 69
#define volupButton 25      // Default value: 25
#define voldownButton 22    // Default value: 22
#define rptButton 64        // Default value: 64
#define tfuButton 67        // Default value: 67
#define playButton 9        // Default value: 9

/**********************************************************
 * Do not edit below, unless you intend to change the code!
 **********************************************************/

// Rotary encoder pins
#define clk 35  // (A3)
#define dt 34   // (A4)
#define sw 5   // (A5)

// Functions prototyping to be handled on each Encoder Event
void handleRotate(int8_t rotation);
void handlePressRelease();
void handleLongPress();

// Create a global pointer for the encoder object
Versatile_RotaryEncoder *versatile_encoder;

//  EEPROM size: 1 (relayCount)
#define EEPROM_SIZE 1
// Variables, pin definitions

// Onboard LED/Power LED
#define LED 2

// IR Receiver pin and setup
//const byte IR_Recv = 36;
#define IR_Recv 36

// Relays
#define R1 16
#define R2 12
#define R3 27
#define R4 33
#define R5 32

//Solid State Relay
#define SSR 17

// Mute LED
#define muteLed 4

// Relay Array
volatile int relays[] = {16, 12, 27, 33, 32};

// Relay variables
volatile int relayCount;
int previousRelay;
int relayNumber;

/*
 * Power/Mute variables
 *
 * powerState explained in detail at the beginning of this program:
 *  0 = Welcome message, getting ready
 *  1 = Powered on
 *  2 = Powered off
 *
 * mute:
 *  0: Tell toggleMute() to unmute
 *  1: Tell toggleMute() to mute
 *
 * poweronMute:
 *  1: mute for the period defined by startupDelay, then unmute, only when powering on (change powerState to 1)
 *  0: Disable the startupDelay if powerState is unchanged
 */
int powerState = 0;   // Power ready-state
int mute = 1;         // Mute off/on (0/1) -> The circuit starts out muted, tell toggleMute() to stay muted at first run
int poweronMute = 1;  // Mute at poweron, with startupDelay

void setup() {

  Serial.begin(115200); // Set serial monitor transfer rate to 115,200
	versatile_encoder = new Versatile_RotaryEncoder(clk, dt, sw);

  // Load to the rotary encoder functions
  versatile_encoder->setHandleRotate(handleRotate);
  versatile_encoder->setHandlePressRelease(handlePressRelease);
  versatile_encoder->setHandleLongPress(handleLongPress);
  // Modify rotary encoder defualt values (optional)
  versatile_encoder->setReadIntervalDuration(3);    // set 3 ms as long press duration (default is 1 ms)
  // versatile_encoder->setShortPressDuration(35);  // set 35 ms as short press duration (default is 50 ms)
  // versatile_encoder->setLongPressDuration(550);  // set 550 ms as long press duration (default is 1000 ms)

  // Define INPUT pins, make sure the inputs aren't touch enabled
  pinMode (sw,INPUT_PULLUP);
  pinMode (clk,INPUT_PULLDOWN);
  pinMode (dt,INPUT_PULLDOWN);
  pinMode (IR_Recv,INPUT_PULLUP);

  // Define OUTPUT pins
  // Onboard LED
  pinMode (LED,OUTPUT);

  // Mute LED
  pinMode (muteLed,OUTPUT);

  // Relays
  pinMode (R1,OUTPUT);
  pinMode (R2,OUTPUT);
  pinMode (R3,OUTPUT);
  pinMode (R4,OUTPUT);
  pinMode (R5,OUTPUT);
  pinMode (SSR,OUTPUT);

  // Relay variables
  EEPROM.begin(EEPROM_SIZE);
  relayCount = EEPROM.read(0);
  previousRelay = relayCount + 1; // Start out not matching relayCount

  // Enable IR Receiver
  IrReceiver.begin(IR_Recv);
}

/******
 * Main loop
 ******/

void loop() {
  if (powerState == 0) { // Booting, welcome message
    powerOn();

  } else if (powerState == 1){ // Powered on
      relayOn();    // Will automatically change input if a function changes the relayCount variable
      irRemote();   // Allowing all functionality in the irRemote() function
      rotEncoder(); // Read the rotaryencoder 

  } else { // Powered off
      irRemote();   // irRemote() function, react to Power on/off only
      rotEncoder(); // Read the rotaryencoder, react to Power on/off only
  }

} // End Main loop

/******
 * Functions
 ******/

/*
 * Power on amplifier (Welcome/Boot message when first powered on)
 */
void powerOn() { // Only called if powerState is 0 (Ready-status)
  Serial.println("\n       --- LM3886 ---\n");
  Serial.println("The Muffsy Relay Input Selector has woken up!\n");
  Serial.print(" ** Reading saved relay state from NVRAM: ");
  Serial.println(relayCount);
  digitalWrite(relays[4],LOW);

  Serial.println("\n ** Mute Relay turned ON");
  Serial.println(" ** All input relays are turned OFF");
  relayOff();

  Serial.println(" ** Solid State Relay is turned OFF\n");
  digitalWrite (SSR,LOW);
  Serial.println(" ** Startup completed - waiting for Power ON\n");
  Serial.println("       -------------------------\n");

  // Set powerState to 2 (Powered off). This function will not run again.
  powerState = 2;
} // End powerOn()

 /*
  * Turn off all relays
  */
void relayOff() {
    for (int off = 0; off <= 3; off++) {
    digitalWrite(relays[off], LOW);
  } 
} // End relayOff


 /*
  * Turn on or off input relays (mute relay is controlled by toggleMute())
  *
  * relayCount is the actual chosen active relay
  * previousRelay is the previous chosen active relay
  * This function is triggered if the two are unequal
  * The function can be forced to trigger by changing previousRelay: previousRelay = relayCount + 1;
  */
void relayOn() {
  // If relayCount has changed: Turn on the selected relay (next, previous, direct)
  // If previousRelay has changed: Turn on the last selected relay
  if (relayCount != previousRelay) {
    auto localRelayCount = relayCount; // local copy, assuming atomic integer

    /* Rollover 3 or 0, 
     *
     * There are four input relays, 0 to 3 (Status messages prints them as inputs 1 to 4)
     * This part makes sure we can't choose relays lower than 0 or higher than 3
     */
    if (localRelayCount > 3) {
      localRelayCount = 0;
    } else if (localRelayCount < 0) {
      localRelayCount = 3;
    }

    // Turn off all relays, then turn on localRelayCount
    relayOff();
    digitalWrite(relays[localRelayCount], HIGH);
    relayCount = localRelayCount;

    // Write relayCount to memory
    EEPROM.write(0,relayCount);
    EEPROM.commit();
    Serial.print("[LM3886]: Written \"relayCount = ");
    Serial.print(relayCount);
    Serial.println("\" to save slot 0");
      
    // Reset counters, output relayNumber
    previousRelay = relayCount;
    relayNumber = relayCount + 1;
    Serial.print("[LM3886]: Activated relay #");
    Serial.println(relayNumber);
    Serial.println();

    /* 
     * Mute, then unmute if just powered on
     * relayOn() is called at power on, placing the startupDelay mute here is convenient so we don't have to run a separate function
     * If the configurable value startupMute is 0, there will be no delay
     */
    if ((poweronMute == 1) && (startupDelay == 0)) {
      poweronMute = 0; // Set poweronMute to 0, so this mutedelay doesn't occur every time the circuit is muted
      mute = 0; // toggleMute will unmute
      toggleMute;

    } else if (poweronMute == 1) {
      poweronMute = 0; // Set poweronMute to 0, so this mutedelay doesn't occur every time the circuit is muted
      Serial.println("[LM3886]: Turning on mute LED");
      digitalWrite(muteLed,HIGH);
      Serial.print("[LM3886]: Milliseconds delay before turning off mute: ");
      Serial.println(startupDelay);
      delay(startupDelay);
      mute = 0; // toggleMute will unmute when called next
      toggleMute();
    }
  }
} // End relayOn()

/*
 * Mute activate/deactivate
 */
void toggleMute() {
  // This function will mute if unmuted, and vice versa everytime it's called
  if (mute == 1) { // Read mute variable. if unmuted, turn on mute
    Serial.println("[LM3886]: Mute relay turned ON");
    digitalWrite(relays[4],LOW);

    if (powerState == 1){ // If powered on, also turn on mute LED
      Serial.println("[LM3886]: Turning on mute LED\n");
      digitalWrite(muteLed,HIGH);

      // Set mute to 0, next time toggleMute is called, it will turn OFF mute
      // mute = 0 now means we're muted
      mute = 0; // Unmute next time this function is called
    } // End if powered on

  } else { // Mute must be 0, turn off mute
    Serial.println("[LM3886]: Mute relay turned OFF");
    digitalWrite(relays[4],HIGH);
    Serial.println("[LM3886]: Turning off mute LED\n");
    digitalWrite(muteLed,LOW); // Turn off mute LED, no matter if it was turned on or off earlier.

    // Set mute to 1, next time toggleMute is called, it will turn ON mute
    // mute = 1 now means we're unmuted
    mute = 1; // Mute next time this function is called
  } // 
} // End toggleMute()

/*
 * Power on / off
 */
void togglePower() {

  /*
   * If powerState is 1, turn OFF: All relays OFF, power amp OFF
   */
  if (powerState == 1) {
    powerState = 2; // Setting powerState to 2 (off)

    if (mute == 1) { // If unmuted, turn on mute 
      toggleMute(); // mute variable = 1, toggleMute will turn on mute. powerState = 2, mute LED will not turn on
    } else if (mute == 0) { // If already muted, turn off mute LED
      Serial.println("[LM3886]: Mute relay stays turned ON");
      Serial.println("[LM3886]: Turning off mute LED");
      digitalWrite(muteLed,LOW); // Turning off the mute LED
    }
    
    relayOff();
    if (enableSSR == 1) { // Only turn off SSR if "enableSSR" is set to 1
      digitalWrite (SSR,LOW); // Turning off Solid State Relay
      Serial.println("[LM3886]: Solid State Relay OFF");
    }

    Serial.println("[LM3886]: Turning off power LED");
    Serial.println("[LM3886]: Power OFF\n");
    digitalWrite (LED,LOW); // Turning off the power LED
    poweronMute = 1; // Set poweronMute to 1, this will force a mute with delay on next power on

  /*
   * If powerState is 2, turn ON: Last selected relay ON, power amp (Solid State Relay ON)
   */
  } else if (powerState == 2) { // 
    powerState = 1; // Setting powerState to 1 (on)
    Serial.println("[LM3886]: Turning on power LED");
    digitalWrite (LED,HIGH); // Turning on the power lED
    previousRelay = relayCount + 1; // Trigger relayOn()
    Serial.println("[LM3886]: Power ON");

    if (enableSSR == 1) { // Only enable SSR if "enableSSR" is set to 1
      digitalWrite (SSR,HIGH); // Turning on Solid State Relay
      Serial.println("[LM3886]: Solid State Relay ON\n");
    }

    // We have just powered on
    // The circuit is muted, manually in powerOn(), toggleMute() has not been involved to set the mute variable
    // mute = 0; tells toggleMute() that we are muted, and that toggleMute() will unmute the next time it's called
    // relayOn() will call toggleMute if poweronMute = 1
    mute = 0;
    relayOn();

  } 
} // End togglePower()

/*
 * Read the rotary encoder
 */
void rotEncoder() {
  versatile_encoder->ReadEncoder();
}

/*
 * IR Remote
 */
void irRemote() { // Start irRemote function

    // Decode the infrared input
    
  if (IrReceiver.decode()) {
    long int decCode = IrReceiver.decodedIRData.command;

    if (decCode != 0) {
      Serial.print("[LM3886]: Received IR code: ");
      Serial.println(decCode);
    }
    // Read pushed remote control button and take action
    // All actions for remote control buttons are defined here
      switch (decCode) { // Start switch/case
      
        case oneButton: // Relay 1 - Input 1
          {
            Serial.println("[LM3886]: Button \"1\"");
            if (powerState == 1) {
              relayCount = 0;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
            break;
          }

        case twoButton: // Relay 2 - Input 2
          {
            Serial.println("[LM3886]: Button \"2\"");
            if (powerState == 1) {
             relayCount = 1;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
            break;
          }

        case threeButton: // Relay 3 - Input 3
          {
            Serial.println("[LM3886]: Button \"3\"");
            if (powerState == 1) {
             relayCount = 2;
            } else {
             Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
            break;
          }

        case fourButton: // Relay 4 - Input 4
          {
            Serial.println("[LM3886]: Button \"4\"");
            if (powerState == 1) {
              relayCount = 3;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }
          
          case fiveButton: // Button 5 - Info message
          {
            Serial.println("[LM3886]: Button \"5\"");
            if (powerState == 1) {
              ;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }

          case sixButton: // Button 6 - Info message
          {
            Serial.println("[LM3886]: Button \"6\"");
            if (powerState == 1) {
              ;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }

          case sevenButton: // Button 7 - Info message
          {
            Serial.println("[LM3886]: Button \"7\"");
            if (powerState == 1) {
              ;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }

          case eightButton: // Button 8 - Info message
          {
            Serial.println("[LM3886]: Button \"8\"");
            if (powerState == 1) {
              ;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }

          case nineButton: // Button 9 - Info message
          {
            Serial.println("[LM3886]: Button \"9\"");
            if (powerState == 1) {
              ;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }

          case zeroButton: // Button 0 - Info message
          {
            Serial.println("[LM3886]: Button \"0\"");
            if (powerState == 1) {
              ;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }

        case muteButton: // Mute
         {
            Serial.println("[LM3886]: Button \"Mute\"");
            if (powerState == 1) {
              toggleMute();
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
            break;
          }

        case ffButton: // Channel UP
          {
            Serial.println("[LM3886]: Button \"UP\"");
            if (powerState == 1) {
              relayCount++;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
            break;
          }
        
        case rewButton: // Channel DOWN
          {
            Serial.println("[LM3886]: Button \"DOWN\"");
            if (powerState == 1) {
              relayCount--;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            };
            break;
          }

        case powerButton: // Power button
          {
            Serial.println("[LM3886]: Button \"POWER\"");
            togglePower();
	    unsigned long powMillis = millis();
	    // Always wait a bit longer after powerButton to avoid turning on immediately after power off, regardless of irRate
            while (millis() - powMillis < 750) { 
              ;  
            }
            break;
          }

          /* default:
          {
            Serial.println("[LM3886]: Going back to waiting for IR remote keypress\n");
          } */
      }// End switch/case

      unsigned long irMillis = millis();
      while (millis() - irMillis < irRate) {
        ;  
      }
  
  IrReceiver.resume(); // Receives the next value from the button you press

  } // End of if IrReceiver decode
} // End irRemote() 

/*
 * Rotate the rotary encoder
 *
 * Change the encDirection value if you want to reverse the direction
 */
void handleRotate(int8_t rotation) {
  Serial.print("[LM3886]: Rotational encoder turned ");

  // encDirection set to 0
  if ( ((rotation > 0) && (encDirection == 0)) || ((rotation < 0) && (encDirection == 1)) ) { // Channel down (counter-clockwise)

    Serial.println("counter-clockwise");

    if (powerState == 1) {
      // Increase relayCount
      relayCount--;

    } else {
      Serial.println("[LM3886]: Powered off, doing nothing...\n"); // Powered off
    }
  } else if ( ((rotation > 0) && (encDirection == 1)) || ((rotation < 0) && (encDirection == 0)) ) { // Channel up (clockwise) 

	  Serial.println("clockwise");
    
    if (powerState == 1) {
      // Increase relayCount
      relayCount++;

    } else {
      Serial.println("[LM3886]: Powered off, doing nothing...\n"); // Powered off
    } 
  }

  // Wait for the amount of milliseconds defined by rotRate until reading the next IR command
  // This delay uses interrupts, so it won't hold up the rest of the program
  unsigned long rotMillis = millis();
  while (millis() - rotMillis < rotRate) {
    ;  
  }
} // End handleRotate()


/*
 * Press rotary encoder button.
 *
 * Will turn on or off the Muffsy Input selector when the button is released,
 * or mute if you have changed the encPush value and the powerState is 1 (on)
 */
void handlePressRelease() {
  if (encPush == 0) { // Power off
  	Serial.println("[LM3886]: Button \"Power\"");
    togglePower();

  } else { // Mute
    Serial.println("[LM3886]: Button \"Mute\"");
   if (powerState == 1) {
      toggleMute();

   } else {
      Serial.println("[LM3886]: Powered off, doing nothing...\n");
    }
  }
} // End handlePressRelease()

/*
 * Long press the rotary encoder button.
 *
 * It will mute or unmute, as long as powerState is 1 (on),
 * or power on/off if you have changed the encPush value
 */
void handleLongPress() {
  if (encPush == 1) { // Power off
    Serial.println("[LM3886]: Button \"Power\"");
    togglePower();

  } else { // Mute
    Serial.println("[LM3886]: Button \"Mute\"");
   if (powerState == 1) {
      toggleMute();

   } else {
      Serial.println("[LM3886]: Powered off, doing nothing...\n");
    }
  }
} // End handleLongPress()

// END OF THE PROGRAM

 

Link spre comentariu

În câteva momente de inspirație am completat câteva linii de cod. Rămâne să stabilesc ce pini trebuie sa fie x si y. Imi poate spune cineva daca sunt sanse sa functioneze, va rog? sper ca zilele astea sa am timp sa incarc codul si sa vad daca functioneaza si partea de control volum.

 

/* Muffsy Relay Input Selector
 *      
 * Control relays using IR and rotary encoder
 * Control external power to amp using IR and push button
 * LM3886
 *
 * Last update with functionality changes: 2023-09-27
 *
 * Introduction:
 * The software works on the concept of "powerState", which determines the behavior of the Input Selector:
 * 
 * 0: Boot
 *   Read last input channel from NVRAM, selecting the same input as when the input selector was turned off or lost power 
 *   The input selector is muted, and mute LED is turned off 
 *   Print startup message
 *   powerState is changed to 2 (off)
 *
   2: Power OFF
 *   Turn off all relays
 *   Set power amp to off (SSR = LOW)
 *   The input selector is muted, and mute LED is turned off
 *   Limited functionality, only IR powerbutton and rotary encoder pushbutton is active
 *   All input events, even those not available, will be shown in the serial monitor
 *        
 * 1: Power ON
 *   Turn on power button LED
 *   Set power amp to on, SSR = HIGH
 *   Activates the input channel read from NVRAM
 *   Will start muted with mute LED turned on
 *   No actions are read until the input selector unmutes after 1500 ms (default)
 *   All functions available, IR Remote and rotary encoder
 *   All input events and actions will be shown in the serial monitor
 */

/*
 * Load libraries
 */
#include <EEPROM.h> // EEPROM Library
#include <IRremote.h> // IR Remote Library
#include <Versatile_RotaryEncoder.h> // Rotary Encoder Library

/*
 * Startup delay
 *
 * When powering on, the input selector is muted to avoid any unwanted pops
 * Set the delay value in milliseconds (default 1500)
 *
 * Change to 0 for no delay
 */
#define startupDelay 1500

/*
 * Enable SSR
 *
 * If you want to use a Solid State Relay to control mains power to an
 * amplifier or similar, set this value to 1
 *
 * Default 0 (disabled), as handling mains power is dangerous
 */
 #define enableSSR 0

/* Mute and Power on / off:
 *
 * The rotary encoder is also a push button, which registers
 * either a short or a long press. These are their functions:
 *
 *    Short press: Power on / off
 *    Long press: Mute
 *
 * To reverse this behaviour, change the value below to 1 (default = 0)
 */
 #define encPush 0

/* 
 * Rotary encoder direction.
 *
 * If the rotary encoder rotation is the wrong way around, change this value to 1
 * Default: 0
 */
 #define encDirection 0

/* 
 * Rotational encoder rate
 *
 * Delay between registering rotational encoder turns, in milliseconds.
 * If the repeat rate when turning the rotational encoder is too fast, change this timer.
 *
 * Note:
 * Introducing a delay in reading turns of the rotational encoder may skip one or more
 * of the encoders "clicks". In turn, it prevents you from skipping channels if you turn
 * the encoder to fast.
 *
 * Lower number: Faster repeat rate
 * Higher number: Slower repeat rate
 * Default: 0
 */
#define rotRate 0

/* 
 * IR rate
 *
 * Delay between registering IR commands, in milliseconds.
 * If the repeat rate when holding down a button on the remote is
 * to slow or too fast, change this timer.
 *
 * Lower number: Faster repeat rate
 * Higher number: Slower repeat rate
 * Default: 175
 */
#define irRate 175

/*
 *IR Commands
 *
 * The following variables map all the buttons on the remote
 * control that comes with the Muffsy Input Selector.
 *
 * For instructions on how to configure your own remote, see:
 * https://www.muffsy.com/muffsy-relay-input-selector-4#configureremote
 *
 * While connecting your Input Selector to the computer, it will display
 * any remote commands in the Arduiono IDE's Serial Monitor like this:
 *
 *    [LM3886]: Received IR code: <IR code of button pushed>
 *
 * There are placeholder commands in the function irRemote{} for the buttons
 * 0 and 5 to 9 that serves as templates for your own IR routines
 *
 */
#define zeroButton 13       // Placeholder command in the function irRemote{}, default: 13
#define oneButton 1        // Input 1, default: 12
#define twoButton 2        // Input 2, default: 24
#define threeButton 3      // Input 3, default: 94
#define fourButton 4        // Input 4, default: 8
#define fiveButton 28       // Placeholder command in the function irRemote{}, default: 28
#define sixButton 90        // Placeholder command in the function irRemote{}, default: 90
#define sevenButton 66      // Placeholder command in the function irRemote{}, default: 66
#define eightButton 82      // Placeholder command in the function irRemote{}, default: 82
#define nineButton 74       // Placeholder command in the function irRemote{}, default: 74
#define eqButton 70         // Default value: 70
#define modeButton 68       // Default value: 68
#define muteButton 16       // Mute, default: 71
#define ffButton 27         // "Fast Forward" button: Channel up, default: 21
#define rewButton 31         // "Rewind" button: Channel down, default: 7
#define powerButton 18      // Power on / off, default: 69
#define volupButton 25      // Default value: 25
#define voldownButton 22    // Default value: 22
#define rptButton 64        // Default value: 64
#define tfuButton 67        // Default value: 67
#define playButton 9        // Default value: 9

/**********************************************************
 * Do not edit below, unless you intend to change the code!
 **********************************************************/

// Rotary encoder pins
#define clk 35  // (A3)
#define dt 34   // (A4)
#define sw 5   // (A5)

// Functions prototyping to be handled on each Encoder Event
void handleRotate(int8_t rotation);
void handlePressRelease();
void handleLongPress();

// Create a global pointer for the encoder object
Versatile_RotaryEncoder *versatile_encoder;

//  EEPROM size: 1 (relayCount)
#define EEPROM_SIZE 1
// Variables, pin definitions

// Onboard LED/Power LED
#define LED 2

// IR Receiver pin and setup
//const byte IR_Recv = 36;
#define IR_Recv 36

// Relays
#define R1 16
#define R2 12
#define R3 27
#define R4 33
#define R5 32

//Volume Up and down
#define vup x
#define vdwn y

//Solid State Relay
#define SSR 17

// Mute LED
#define muteLed 4

// Relay Array
volatile int relays[] = {16, 12, 27, 33, 32};

// Relay variables
volatile int relayCount;
int previousRelay;
int relayNumber;

/*
 * Power/Mute variables
 *
 * powerState explained in detail at the beginning of this program:
 *  0 = Welcome message, getting ready
 *  1 = Powered on
 *  2 = Powered off
 *
 * mute:
 *  0: Tell toggleMute() to unmute
 *  1: Tell toggleMute() to mute
 *
 * poweronMute:
 *  1: mute for the period defined by startupDelay, then unmute, only when powering on (change powerState to 1)
 *  0: Disable the startupDelay if powerState is unchanged
 */
int powerState = 0;   // Power ready-state
int mute = 1;         // Mute off/on (0/1) -> The circuit starts out muted, tell toggleMute() to stay muted at first run
int poweronMute = 1;  // Mute at poweron, with startupDelay

void setup() {

  Serial.begin(115200); // Set serial monitor transfer rate to 115,200
	versatile_encoder = new Versatile_RotaryEncoder(clk, dt, sw);

  // Load to the rotary encoder functions
  versatile_encoder->setHandleRotate(handleRotate);
  versatile_encoder->setHandlePressRelease(handlePressRelease);
  versatile_encoder->setHandleLongPress(handleLongPress);
  // Modify rotary encoder defualt values (optional)
  versatile_encoder->setReadIntervalDuration(3);    // set 3 ms as long press duration (default is 1 ms)
  // versatile_encoder->setShortPressDuration(35);  // set 35 ms as short press duration (default is 50 ms)
  // versatile_encoder->setLongPressDuration(550);  // set 550 ms as long press duration (default is 1000 ms)

  // Define INPUT pins, make sure the inputs aren't touch enabled
  pinMode (sw,INPUT_PULLUP);
  pinMode (clk,INPUT_PULLDOWN);
  pinMode (dt,INPUT_PULLDOWN);
  pinMode (IR_Recv,INPUT_PULLUP);

  // Define OUTPUT pins
  // Onboard LED
  pinMode (LED,OUTPUT);

  // Mute LED
  pinMode (muteLed,OUTPUT);

  // Relays
  pinMode (R1,OUTPUT);
  pinMode (R2,OUTPUT);
  pinMode (R3,OUTPUT);
  pinMode (R4,OUTPUT);
  pinMode (R5,OUTPUT);
  pinMode (SSR,OUTPUT);

  //volume Up and down
  pinMode (vup,OUTPUT);
  pinMode (vdwn,OUTPUT);

  // Relay variables
  EEPROM.begin(EEPROM_SIZE);
  relayCount = EEPROM.read(0);
  previousRelay = relayCount + 1; // Start out not matching relayCount

  // Enable IR Receiver
  IrReceiver.begin(IR_Recv);
}

/******
 * Main loop
 ******/

void loop() {
  if (powerState == 0) { // Booting, welcome message
    powerOn();

  } else if (powerState == 1){ // Powered on
      relayOn();    // Will automatically change input if a function changes the relayCount variable
      irRemote();   // Allowing all functionality in the irRemote() function
      rotEncoder(); // Read the rotaryencoder 

  } else { // Powered off
      irRemote();   // irRemote() function, react to Power on/off only
      rotEncoder(); // Read the rotaryencoder, react to Power on/off only
  }

} // End Main loop

/******
 * Functions
 ******/

/*
 * Power on amplifier (Welcome/Boot message when first powered on)
 */
void powerOn() { // Only called if powerState is 0 (Ready-status)
  Serial.println("\n       --- LM3886 ---\n");
  Serial.println("The Muffsy Relay Input Selector has woken up!\n");
  Serial.print(" ** Reading saved relay state from NVRAM: ");
  Serial.println(relayCount);
  digitalWrite(relays[4],LOW);

  Serial.println("\n ** Mute Relay turned ON");
  Serial.println(" ** All input relays are turned OFF");
  relayOff();

  Serial.println(" ** Solid State Relay is turned OFF\n");
  digitalWrite (SSR,LOW);
  Serial.println(" ** Startup completed - waiting for Power ON\n");
  Serial.println("       -------------------------\n");

  // Set powerState to 2 (Powered off). This function will not run again.
  powerState = 2;
} // End powerOn()

 /*
  * Turn off all relays
  */
void relayOff() {
    for (int off = 0; off <= 3; off++) {
    digitalWrite(relays[off], LOW);
  } 
} // End relayOff


 /*
  * Turn on or off input relays (mute relay is controlled by toggleMute())
  *
  * relayCount is the actual chosen active relay
  * previousRelay is the previous chosen active relay
  * This function is triggered if the two are unequal
  * The function can be forced to trigger by changing previousRelay: previousRelay = relayCount + 1;
  */
void relayOn() {
  // If relayCount has changed: Turn on the selected relay (next, previous, direct)
  // If previousRelay has changed: Turn on the last selected relay
  if (relayCount != previousRelay) {
    auto localRelayCount = relayCount; // local copy, assuming atomic integer

    /* Rollover 3 or 0, 
     *
     * There are four input relays, 0 to 3 (Status messages prints them as inputs 1 to 4)
     * This part makes sure we can't choose relays lower than 0 or higher than 3
     */
    if (localRelayCount > 3) {
      localRelayCount = 0;
    } else if (localRelayCount < 0) {
      localRelayCount = 3;
    }

    // Turn off all relays, then turn on localRelayCount
    relayOff();
    digitalWrite(relays[localRelayCount], HIGH);
    relayCount = localRelayCount;

    // Write relayCount to memory
    EEPROM.write(0,relayCount);
    EEPROM.commit();
    Serial.print("[LM3886]: Written \"relayCount = ");
    Serial.print(relayCount);
    Serial.println("\" to save slot 0");
      
    // Reset counters, output relayNumber
    previousRelay = relayCount;
    relayNumber = relayCount + 1;
    Serial.print("[LM3886]: Activated relay #");
    Serial.println(relayNumber);
    Serial.println();

    /* 
     * Mute, then unmute if just powered on
     * relayOn() is called at power on, placing the startupDelay mute here is convenient so we don't have to run a separate function
     * If the configurable value startupMute is 0, there will be no delay
     */
    if ((poweronMute == 1) && (startupDelay == 0)) {
      poweronMute = 0; // Set poweronMute to 0, so this mutedelay doesn't occur every time the circuit is muted
      mute = 0; // toggleMute will unmute
      toggleMute;

    } else if (poweronMute == 1) {
      poweronMute = 0; // Set poweronMute to 0, so this mutedelay doesn't occur every time the circuit is muted
      Serial.println("[LM3886]: Turning on mute LED");
      digitalWrite(muteLed,HIGH);
      Serial.print("[LM3886]: Milliseconds delay before turning off mute: ");
      Serial.println(startupDelay);
      delay(startupDelay);
      mute = 0; // toggleMute will unmute when called next
      toggleMute();
    }
  }
} // End relayOn()

/*
 * Mute activate/deactivate
 */
void toggleMute() {
  // This function will mute if unmuted, and vice versa everytime it's called
  if (mute == 1) { // Read mute variable. if unmuted, turn on mute
    Serial.println("[LM3886]: Mute relay turned ON");
    digitalWrite(relays[4],LOW);

    if (powerState == 1){ // If powered on, also turn on mute LED
      Serial.println("[LM3886]: Turning on mute LED\n");
      digitalWrite(muteLed,HIGH);

      // Set mute to 0, next time toggleMute is called, it will turn OFF mute
      // mute = 0 now means we're muted
      mute = 0; // Unmute next time this function is called
    } // End if powered on

  } else { // Mute must be 0, turn off mute
    Serial.println("[LM3886]: Mute relay turned OFF");
    digitalWrite(relays[4],HIGH);
    Serial.println("[LM3886]: Turning off mute LED\n");
    digitalWrite(muteLed,LOW); // Turn off mute LED, no matter if it was turned on or off earlier.

    // Set mute to 1, next time toggleMute is called, it will turn ON mute
    // mute = 1 now means we're unmuted
    mute = 1; // Mute next time this function is called
  } // 
} // End toggleMute()

/*
 * Power on / off
 */
void togglePower() {

  /*
   * If powerState is 1, turn OFF: All relays OFF, power amp OFF
   */
  if (powerState == 1) {
    powerState = 2; // Setting powerState to 2 (off)

    if (mute == 1) { // If unmuted, turn on mute 
      toggleMute(); // mute variable = 1, toggleMute will turn on mute. powerState = 2, mute LED will not turn on
    } else if (mute == 0) { // If already muted, turn off mute LED
      Serial.println("[LM3886]: Mute relay stays turned ON");
      Serial.println("[LM3886]: Turning off mute LED");
      digitalWrite(muteLed,LOW); // Turning off the mute LED
    }
    
    relayOff();
    if (enableSSR == 1) { // Only turn off SSR if "enableSSR" is set to 1
      digitalWrite (SSR,LOW); // Turning off Solid State Relay
      Serial.println("[LM3886]: Solid State Relay OFF");
    }

    Serial.println("[LM3886]: Turning off power LED");
    Serial.println("[LM3886]: Power OFF\n");
    digitalWrite (LED,LOW); // Turning off the power LED
    poweronMute = 1; // Set poweronMute to 1, this will force a mute with delay on next power on

  /*
   * If powerState is 2, turn ON: Last selected relay ON, power amp (Solid State Relay ON)
   */
  } else if (powerState == 2) { // 
    powerState = 1; // Setting powerState to 1 (on)
    Serial.println("[LM3886]: Turning on power LED");
    digitalWrite (LED,HIGH); // Turning on the power lED
    previousRelay = relayCount + 1; // Trigger relayOn()
    Serial.println("[LM3886]: Power ON");

    if (enableSSR == 1) { // Only enable SSR if "enableSSR" is set to 1
      digitalWrite (SSR,HIGH); // Turning on Solid State Relay
      Serial.println("[LM3886]: Solid State Relay ON\n");
    }

    // We have just powered on
    // The circuit is muted, manually in powerOn(), toggleMute() has not been involved to set the mute variable
    // mute = 0; tells toggleMute() that we are muted, and that toggleMute() will unmute the next time it's called
    // relayOn() will call toggleMute if poweronMute = 1
    mute = 0;
    relayOn();

  } 
} // End togglePower()

/*
 * Read the rotary encoder
 */
void rotEncoder() {
  versatile_encoder->ReadEncoder();
}

/*
 * IR Remote
 */
void irRemote() { // Start irRemote function

    // Decode the infrared input
    
  if (IrReceiver.decode()) {
    long int decCode = IrReceiver.decodedIRData.command;

    if (decCode != 0) {
      Serial.print("[LM3886]: Received IR code: ");
      Serial.println(decCode);
    }
    // Read pushed remote control button and take action
    // All actions for remote control buttons are defined here
      switch (decCode) { // Start switch/case
      
        case oneButton: // Relay 1 - Input 1
          {
            Serial.println("[LM3886]: Button \"1\"");
            if (powerState == 1) {
              relayCount = 0;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
            break;
          }

        case twoButton: // Relay 2 - Input 2
          {
            Serial.println("[LM3886]: Button \"2\"");
            if (powerState == 1) {
             relayCount = 1;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
            break;
          }

        case threeButton: // Relay 3 - Input 3
          {
            Serial.println("[LM3886]: Button \"3\"");
            if (powerState == 1) {
             relayCount = 2;
            } else {
             Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
            break;
          }

        case fourButton: // Relay 4 - Input 4
          {
            Serial.println("[LM3886]: Button \"4\"");
            if (powerState == 1) {
              relayCount = 3;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }
          
          case fiveButton: // Button 5 - Info message
          {
            Serial.println("[LM3886]: Button \"5\"");
            if (powerState == 1) {
              ;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }

          case sixButton: // Button 6 - Info message
          {
            Serial.println("[LM3886]: Button \"6\"");
            if (powerState == 1) {
              ;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }

          case sevenButton: // Button 7 - Info message
          {
            Serial.println("[LM3886]: Button \"7\"");
            if (powerState == 1) {
              ;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }

          case eightButton: // Button 8 - Info message
          {
            Serial.println("[LM3886]: Button \"8\"");
            if (powerState == 1) {
              ;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }

          case nineButton: // Button 9 - Info message
          {
            Serial.println("[LM3886]: Button \"9\"");
            if (powerState == 1) {
              ;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }

          case zeroButton: // Button 0 - Info message
          {
            Serial.println("[LM3886]: Button \"0\"");
            if (powerState == 1) {
              ;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }

          case volupButton: // Volume up - Info message
          {
            Serial.println("[LM3886]: Button \"vup\"");
            if (powerState == 1) {
              digitalWrite (vup, HIGH)
              ;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }

          case voldownButton: // Volume down - Info message
          {
            Serial.println("[LM3886]: Button \"vdwn\"");
            if (powerState == 1) {
              digitalWrite (vdwn, HIGH)
              ;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
           break;
          }

          case muteButton: // Mute
         {
            Serial.println("[LM3886]: Button \"Mute\"");
            if (powerState == 1) {
              toggleMute();
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
            break;
          }

          case ffButton: // Channel UP
          {
            Serial.println("[LM3886]: Button \"UP\"");
            if (powerState == 1) {
              relayCount++;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            }
            break;
          }
        
          case rewButton: // Channel DOWN
          {
            Serial.println("[LM3886]: Button \"DOWN\"");
            if (powerState == 1) {
              relayCount--;
            } else {
              Serial.println("[LM3886]: Powered off, doing nothing...\n");
            };
            break;
          }

        case powerButton: // Power button
          {
            Serial.println("[LM3886]: Button \"POWER\"");
            togglePower();
	    unsigned long powMillis = millis();
	    // Always wait a bit longer after powerButton to avoid turning on immediately after power off, regardless of irRate
            while (millis() - powMillis < 750) { 
              ;  
            }
            break;
          }

          /* default:
          {
            Serial.println("[LM3886]: Going back to waiting for IR remote keypress\n");
          } */
      }// End switch/case

      unsigned long irMillis = millis();
      while (millis() - irMillis < irRate) {
        ;  
      }
  
  IrReceiver.resume(); // Receives the next value from the button you press

  } // End of if IrReceiver decode
} // End irRemote() 

/*
 * Rotate the rotary encoder
 *
 * Change the encDirection value if you want to reverse the direction
 */
void handleRotate(int8_t rotation) {
  Serial.print("[LM3886]: Rotational encoder turned ");

  // encDirection set to 0
  if ( ((rotation > 0) && (encDirection == 0)) || ((rotation < 0) && (encDirection == 1)) ) { // Channel down (counter-clockwise)

    Serial.println("counter-clockwise");

    if (powerState == 1) {
      // Increase relayCount
      relayCount--;

    } else {
      Serial.println("[LM3886]: Powered off, doing nothing...\n"); // Powered off
    }
  } else if ( ((rotation > 0) && (encDirection == 1)) || ((rotation < 0) && (encDirection == 0)) ) { // Channel up (clockwise) 

	  Serial.println("clockwise");
    
    if (powerState == 1) {
      // Increase relayCount
      relayCount++;

    } else {
      Serial.println("[LM3886]: Powered off, doing nothing...\n"); // Powered off
    } 
  }

  // Wait for the amount of milliseconds defined by rotRate until reading the next IR command
  // This delay uses interrupts, so it won't hold up the rest of the program
  unsigned long rotMillis = millis();
  while (millis() - rotMillis < rotRate) {
    ;  
  }
} // End handleRotate()


/*
 * Press rotary encoder button.
 *
 * Will turn on or off the Muffsy Input selector when the button is released,
 * or mute if you have changed the encPush value and the powerState is 1 (on)
 */
void handlePressRelease() {
  if (encPush == 0) { // Power off
  	Serial.println("[LM3886]: Button \"Power\"");
    togglePower();

  } else { // Mute
    Serial.println("[LM3886]: Button \"Mute\"");
   if (powerState == 1) {
      toggleMute();

   } else {
      Serial.println("[LM3886]: Powered off, doing nothing...\n");
    }
  }
} // End handlePressRelease()

/*
 * Long press the rotary encoder button.
 *
 * It will mute or unmute, as long as powerState is 1 (on),
 * or power on/off if you have changed the encPush value
 */
void handleLongPress() {
  if (encPush == 1) { // Power off
    Serial.println("[LM3886]: Button \"Power\"");
    togglePower();

  } else { // Mute
    Serial.println("[LM3886]: Button \"Mute\"");
   if (powerState == 1) {
      toggleMute();

   } else {
      Serial.println("[LM3886]: Powered off, doing nothing...\n");
    }
  }
} // End handleLongPress()

// END OF THE PROGRAM

 

Link spre comentariu

Creează un cont sau autentifică-te pentru a adăuga comentariu

Trebuie să fi un membru pentru a putea lăsa un comentariu.

Creează un cont

Înregistrează-te pentru un nou cont în comunitatea nostră. Este simplu!

Înregistrează un nou cont

Autentificare

Ai deja un cont? Autentifică-te aici.

Autentifică-te acum



×
×
  • Creează nouă...

Informații Importante

Am plasat cookie-uri pe dispozitivul tău pentru a îmbunătății navigarea pe acest site. Poți modifica setările cookie, altfel considerăm că ești de acord să continui.Termeni de Utilizare si Ghidări