Advertisement
TolentinoCotesta

Multi AC dimmer (serial)

Mar 19th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1.  
  2. #include <dimmable_light_linearized.h>
  3.  
  4. const unsigned char channel_1 = 4;  // Output to Opto Triac pin, channel 1
  5. const unsigned char channel_2 = 5;  // Output to Opto Triac pin, channel 2
  6. const unsigned char channel_3 = 6;  // Output to Opto Triac pin, channel 3
  7. const unsigned char channel_4 = 7;  // Output to Opto Triac pin, channel 4
  8. const unsigned char channel_5 = 8;  // Output to Opto Triac pin, channel 5
  9. const unsigned char channel_6 = 9;  // Output to Opto Triac pin, channel 6
  10. const unsigned char channel_7 = 10; // Output to Opto Triac pin, channel 7
  11. const unsigned char channel_8 = 11; // Output to Opto Triac pin, channel 8
  12. const unsigned char ZeroCrossPin = 3;
  13.  
  14. String rxSerial;
  15.  
  16. // Usiamo questo array per memorizzare il valore di luminosità per ciascun canale (0-100)
  17. #define NUM_CH 8
  18. unsigned char CH_Values[NUM_CH];
  19.  
  20. DimmableLightLinearized  channel1(channel_1);
  21. DimmableLightLinearized  channel2(channel_2);
  22. DimmableLightLinearized  channel3(channel_3);
  23. DimmableLightLinearized  channel4(channel_4);
  24. DimmableLightLinearized  channel5(channel_5);
  25. DimmableLightLinearized  channel6(channel_6);
  26. DimmableLightLinearized  channel7(channel_7);
  27. DimmableLightLinearized  channel8(channel_8);
  28.  
  29. bool updateVal = false;
  30.  
  31. void setup() {
  32.   // Inizializzo la classe per controllare il dimming
  33.   DimmableLightLinearized::setSyncPin(ZeroCrossPin);
  34.   // VERY IMPORTANT: Call this method to start internal light routine
  35.   DimmableLightLinearized::begin();
  36.  
  37.   Serial.begin(115200);
  38. }
  39.  
  40.  
  41.  
  42. void loop()
  43. {
  44.    // Se ci sono caratteri sulla seriale, li leggiamo uno per volta salvandoli in rxSerial
  45.    while(Serial.available()){
  46.      char inByte = (char)Serial.read();
  47.      rxSerial += inByte;
  48.  
  49.      // Ho ricevuto il carattere di fine linea. La stringa è completa e posso analizzarla
  50.      if(inByte == '#') {
  51.       // L'ultimissimo carattere di rxSerial è '\n'; il canale sarà quindi il precedente
  52.       // Alcuni software inviano come terminatori di stringa solo il carattere di fine linea,
  53.       // altri aggiungono anche il carattere di "carriage return", quindi valutiamo.
  54.      
  55.       int fineLineaPos = rxSerial.indexOf('#') - 1;
  56.          
  57.       char channel = (char) rxSerial[fineLineaPos];
  58.  
  59.       if (channel < 'h'){
  60.         Serial.print("Imposto il canale '");
  61.         Serial.print(channel);
  62.       }
  63.       else {
  64.         Serial.print("Canale non valido");
  65.         rxSerial = "";
  66.         // Interrompo l'esecuzione
  67.         return;
  68.       }
  69.      
  70.       // Per ottenere il nuovo valore del canale, estraiamo la substring per la  parte numerica
  71.       // e convertiamo la string in intero
  72.       int valoreLum = rxSerial.substring(0, fineLineaPos).toInt();
  73.       valoreLum = constrain(valoreLum, 0, 255);
  74.       Serial.print("' al valore ");
  75.       Serial.println(valoreLum);
  76.      
  77.       // Arrivati a questo punto, sappiamo se il canale è a, b, c etc etc...
  78.       // Dobbiamo però convertire a, b, c in numeri perché abbiamo usato
  79.       // l'array CH_Values[NUM_CH]  per memorizzare i valori di ciascun canale
  80.       // Il carattere ASCII 'a' equivale al decimale 97, il  'b' è uguale a 98.
  81.       // Se sottraiamo il valore 97 avremo quindi a=0, b=1, c=2 etc
  82.       CH_Values[char(channel) - 97] = valoreLum;
  83.  
  84.       Serial.println("I nuovi valori aggiornati sono: ");
  85.       for(int i=0; i<NUM_CH; i++){
  86.         Serial.print("Canale ");
  87.         Serial.print(char(i+97));
  88.         Serial.print(": ");
  89.         Serial.println(CH_Values[i]);    
  90.       }
  91.       // Resettiamo la stringa per la prossima lettura
  92.       rxSerial= "";
  93.       updateVal = true;
  94.      }
  95.   }
  96.  
  97.  
  98.   // Aggiorniamo i valori di uscita per ciascun canale
  99.   if(updateVal){
  100.     updateVal = false;
  101.     channel1.setBrightness(CH_Values[0]);
  102.     channel2.setBrightness(CH_Values[1]);
  103.     channel3.setBrightness(CH_Values[2]);
  104.     channel4.setBrightness(CH_Values[3]);
  105.     channel5.setBrightness(CH_Values[4]);
  106.     channel6.setBrightness(CH_Values[5]);
  107.     channel7.setBrightness(CH_Values[6]);
  108.     channel8.setBrightness(CH_Values[7]);
  109.     delay(100);
  110.   }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement