/* 25/01/2024 * * PCA9685GasLightsV2 * Multiple PCA9685 board...4 houselights, 4 street lights (128 leds) * */ #include "Wire.h" #include "Adafruit_PWMServoDriver.h" //pca9685 settings Adafruit_PWMServoDriver streetLights1 = Adafruit_PWMServoDriver(0x40); Adafruit_PWMServoDriver streetLights2 = Adafruit_PWMServoDriver(0x42); Adafruit_PWMServoDriver streetLights3 = Adafruit_PWMServoDriver(0x41); Adafruit_PWMServoDriver streetLights4 = Adafruit_PWMServoDriver(0x43); Adafruit_PWMServoDriver houselights1 = Adafruit_PWMServoDriver(0x48); Adafruit_PWMServoDriver houselights2 = Adafruit_PWMServoDriver(0x50); Adafruit_PWMServoDriver houselights3 = Adafruit_PWMServoDriver(0x45); Adafruit_PWMServoDriver houselights4 = Adafruit_PWMServoDriver(0x56); unsigned long currentMillis; const int streetnormalBrightness = 4000;// Maximum brightness is 4095 const int streetminBrightness = 1500;// minimum is 0 OFF Gas lights Reduce value to make more aggressive flicker unsigned long streetgasTimerArray[16]; int streetgasChangeLength[16][2];//length //flickerState 0 = normalBrightness 1 = flicker //street gas lamps flicker function void streetGasLights() { int q; for(q=0;q < 16;q++){ if(currentMillis - streetgasTimerArray[q] >= streetgasChangeLength[q][0]){ streetgasTimerArray[q] = currentMillis; if(streetgasChangeLength[q][1] < 1){//currently at max brightness streetLights1.setPWM(q, 0, random(streetminBrightness,streetnormalBrightness)); streetLights2.setPWM(q, 0, random(streetminBrightness,streetnormalBrightness)); streetLights3.setPWM(q, 0, random(streetminBrightness,streetnormalBrightness)); streetLights4.setPWM(q, 0, random(streetminBrightness,streetnormalBrightness)); streetgasChangeLength[q][0] = random(100,500);//flicker will last 0.1 - 0.5 secs streetgasChangeLength[q][1] = 1; }else{ streetLights1.setPWM(q, 0,random(streetnormalBrightness - 300,streetnormalBrightness)); streetLights2.setPWM(q, 0,random(streetnormalBrightness - 300,streetnormalBrightness)); streetLights3.setPWM(q, 0,random(streetnormalBrightness - 300,streetnormalBrightness)); streetLights4.setPWM(q, 0,random(streetnormalBrightness - 300,streetnormalBrightness)); streetgasChangeLength[q][0] = random(1000,5000);//1-5 secs before next flicker streetgasChangeLength[q][1] = 0; } } } } const int housenormalBrightness = 1000;// Maximum brightness is 4095 const int houseminBrightness = 300;// minimum is 0 OFF Gas lights Reduce value to make more aggressive flicker unsigned long housegasTimerArray[16]; int housegasChangeLength[16][2];//length //flickerState 0 = normalBrightness 1 = flicker //house gas lamps flicker function...dimmer than street version void houseGasLights() { int q; for(q=0;q < 16;q++){ if(currentMillis - housegasTimerArray[q] >= housegasChangeLength[q][0]){ housegasTimerArray[q] = currentMillis; if(housegasChangeLength[q][1] < 1){//currently at max brightness houselights1.setPWM(q, 0, random(houseminBrightness,housenormalBrightness)); houselights2.setPWM(q, 0, random(houseminBrightness,housenormalBrightness)); houselights3.setPWM(q, 0, random(houseminBrightness,housenormalBrightness)); houselights4.setPWM(q, 0, random(houseminBrightness,housenormalBrightness)); housegasChangeLength[q][0] = random(100,500);//flicker will last 0.1 - 0.5 secs housegasChangeLength[q][1] = 1; }else{ houselights1.setPWM(q, 0,random(housenormalBrightness - 300,housenormalBrightness)); houselights2.setPWM(q, 0,random(housenormalBrightness - 300,housenormalBrightness)); houselights3.setPWM(q, 0,random(housenormalBrightness - 300,housenormalBrightness)); houselights4.setPWM(q, 0,random(housenormalBrightness - 300,housenormalBrightness)); housegasChangeLength[q][0] = random(1000,5000);//1-5 secs before next flicker housegasChangeLength[q][1] = 0; } } } } void setup() { Serial.begin(115200); Serial.println("PCA9685GasLightsV2"); //start led boards streetLights1.begin(); streetLights1.setPWMFreq(1600); streetLights2.begin(); streetLights2.setPWMFreq(1600); streetLights3.begin(); streetLights3.setPWMFreq(1600); streetLights4.begin(); streetLights4.setPWMFreq(1600); houselights1.begin(); houselights1.setPWMFreq(1600); houselights2.begin(); houselights2.setPWMFreq(1600); houselights3.begin(); houselights3.setPWMFreq(1600); houselights4.begin(); houselights4.setPWMFreq(1600); } void loop() { currentMillis = millis(); //get current time since board started in milliseconds streetGasLights(); //calls the street lights function houseGasLights(); //calls the house lights function }