Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <dimmable_light_linearized.h>
- const unsigned char channel_1 = 4; // Output to Opto Triac pin, channel 1
- const unsigned char channel_2 = 5; // Output to Opto Triac pin, channel 2
- const unsigned char channel_3 = 6; // Output to Opto Triac pin, channel 3
- const unsigned char channel_4 = 7; // Output to Opto Triac pin, channel 4
- const unsigned char channel_5 = 8; // Output to Opto Triac pin, channel 5
- const unsigned char channel_6 = 9; // Output to Opto Triac pin, channel 6
- const unsigned char channel_7 = 10; // Output to Opto Triac pin, channel 7
- const unsigned char channel_8 = 11; // Output to Opto Triac pin, channel 8
- const unsigned char ZeroCrossPin = 3;
- String rxSerial;
- // Usiamo questo array per memorizzare il valore di luminosità per ciascun canale (0-100)
- #define NUM_CH 8
- unsigned char CH_Values[NUM_CH];
- DimmableLightLinearized channel1(channel_1);
- DimmableLightLinearized channel2(channel_2);
- DimmableLightLinearized channel3(channel_3);
- DimmableLightLinearized channel4(channel_4);
- DimmableLightLinearized channel5(channel_5);
- DimmableLightLinearized channel6(channel_6);
- DimmableLightLinearized channel7(channel_7);
- DimmableLightLinearized channel8(channel_8);
- bool updateVal = false;
- void setup() {
- // Inizializzo la classe per controllare il dimming
- DimmableLightLinearized::setSyncPin(ZeroCrossPin);
- // VERY IMPORTANT: Call this method to start internal light routine
- DimmableLightLinearized::begin();
- Serial.begin(115200);
- }
- void loop()
- {
- // Se ci sono caratteri sulla seriale, li leggiamo uno per volta salvandoli in rxSerial
- while(Serial.available()){
- char inByte = (char)Serial.read();
- rxSerial += inByte;
- // Ho ricevuto il carattere di fine linea. La stringa è completa e posso analizzarla
- if(inByte == '#') {
- // L'ultimissimo carattere di rxSerial è '\n'; il canale sarà quindi il precedente
- // Alcuni software inviano come terminatori di stringa solo il carattere di fine linea,
- // altri aggiungono anche il carattere di "carriage return", quindi valutiamo.
- int fineLineaPos = rxSerial.indexOf('#') - 1;
- char channel = (char) rxSerial[fineLineaPos];
- if (channel < 'h'){
- Serial.print("Imposto il canale '");
- Serial.print(channel);
- }
- else {
- Serial.print("Canale non valido");
- rxSerial = "";
- // Interrompo l'esecuzione
- return;
- }
- // Per ottenere il nuovo valore del canale, estraiamo la substring per la parte numerica
- // e convertiamo la string in intero
- int valoreLum = rxSerial.substring(0, fineLineaPos).toInt();
- valoreLum = constrain(valoreLum, 0, 255);
- Serial.print("' al valore ");
- Serial.println(valoreLum);
- // Arrivati a questo punto, sappiamo se il canale è a, b, c etc etc...
- // Dobbiamo però convertire a, b, c in numeri perché abbiamo usato
- // l'array CH_Values[NUM_CH] per memorizzare i valori di ciascun canale
- // Il carattere ASCII 'a' equivale al decimale 97, il 'b' è uguale a 98.
- // Se sottraiamo il valore 97 avremo quindi a=0, b=1, c=2 etc
- CH_Values[char(channel) - 97] = valoreLum;
- Serial.println("I nuovi valori aggiornati sono: ");
- for(int i=0; i<NUM_CH; i++){
- Serial.print("Canale ");
- Serial.print(char(i+97));
- Serial.print(": ");
- Serial.println(CH_Values[i]);
- }
- // Resettiamo la stringa per la prossima lettura
- rxSerial= "";
- updateVal = true;
- }
- }
- // Aggiorniamo i valori di uscita per ciascun canale
- if(updateVal){
- updateVal = false;
- channel1.setBrightness(CH_Values[0]);
- channel2.setBrightness(CH_Values[1]);
- channel3.setBrightness(CH_Values[2]);
- channel4.setBrightness(CH_Values[3]);
- channel5.setBrightness(CH_Values[4]);
- channel6.setBrightness(CH_Values[5]);
- channel7.setBrightness(CH_Values[6]);
- channel8.setBrightness(CH_Values[7]);
- delay(100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement