/* 25/01/2024 * * PCA9685GasLightsV1 * Single PCA9685 board * */ #include "Wire.h" #include "Adafruit_PWMServoDriver.h" //pca9685 settings Adafruit_PWMServoDriver ledBoard1 = Adafruit_PWMServoDriver(0x40); 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 < 8;q++){ if(currentMillis - streetgasTimerArray[q] >= streetgasChangeLength[q][0]){ streetgasTimerArray[q] = currentMillis; if(streetgasChangeLength[q][1] < 1){//currently at max brightness ledBoard1.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{ ledBoard1.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=8;q < 16;q++){ if(currentMillis - housegasTimerArray[q] >= housegasChangeLength[q][0]){ housegasTimerArray[q] = currentMillis; if(housegasChangeLength[q][1] < 1){//currently at max brightness ledBoard1.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{ ledBoard1.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("PCA9685GasLightsV1"); //start led board ledBoard1.begin(); ledBoard1.setPWMFreq(1600); } void loop() { currentMillis = millis(); //get current time since board started in milliseconds streetGasLights(); //calls the gas function houseGasLights(); //calls the gas function }