User avatar
By blackbird.0360
#92832 Hello Richard,
Thank you for this code that works perfectly. I built a board with a PCA9685; But the code induces a larger delay than 1 second (test with Vixen) for the ignition of the LEDs. I modified the code for a single PCA9685 and the ignition is without delay.
If this can help someone, I mention the modified code:
-----------------------------------------------------
#include <ESP8266WiFi.h>
#include <E131.h>
#include <Wire.h>

//==================== Setup infomation section start ====================//
//==== WiFi & Universe ====//
const char ssid[] = "???????"; /* Replace ???????? with your SSID */
const char passphrase[] = "???????"; /* Replace ???????? with your WPA2 passphrase */
const int universe = 1; /* Replace with your universe number */
const int channel = 16; /* Replace with your channel number */

//==== Control boards ====//
//channel numbers for output 1 -> 16 in that order, 0 = unused output.
int board_40[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; // if fitted.

//if using RGB strip use outputs 2 to 16 to match PTC fuses.
//==== Debug - may cause lag ====//
const int debug = 0; //set value 1= debug 0 = normal
//==================== Setup infomation section end ====================//

int gamma_val[256] = {0, 1, 1, 1, 1, 2, 2, 3, 4, 5, 6, 8, 9, 11, 12, 14, 16, 18, 20,
23, 25, 28, 30, 33, 36, 39, 43, 46, 49, 53, 57, 61, 64, 69, 73, 77, 82, 86, 91, 96,
101, 106, 111, 116, 122, 128, 133, 139, 145, 151, 157, 164, 170, 177, 184, 191, 197,
205, 212, 219, 227, 234, 242, 250, 258, 266, 274, 283, 291, 300, 309, 317, 326, 336,
345, 354, 364, 373, 383, 393, 403, 413, 423, 434, 444, 455, 466, 477, 488, 499, 510,
522, 533, 545, 556, 568, 580, 593, 605, 617, 630, 642, 655, 668, 681, 694, 708, 721,
735, 748, 762, 776, 790, 804, 818, 833, 847, 862, 877, 892, 907, 922, 937, 953, 968,
984, 1000, 1016, 1032, 1048, 1064, 1081, 1097, 1114, 1131, 1148, 1165, 1182, 1199, 1217,
1234, 1252, 1270, 1288, 1306, 1324, 1342, 1361, 1379, 1398, 1417, 1436, 1455, 1474, 1494,
1513, 1533, 1552, 1572, 1592, 1612, 1632, 1653, 1673, 1694, 1715, 1735, 1756, 1777, 1799,
1820, 1841, 1863, 1885, 1907, 1929, 1951, 1973, 1995, 2018, 2040, 2063, 2086, 2109, 2132,
2155, 2179, 2202, 2226, 2250, 2273, 2297, 2322, 2346, 2370, 2395, 2419, 2444, 2469, 2494,
2519, 2544, 2570, 2595, 2621, 2647, 2672, 2698, 2725, 2751, 2777, 2804, 2830, 2857, 2884,
2911, 2938, 2965, 2993, 3020, 3048, 3076, 3104, 3132, 3160, 3188, 3217, 3245, 3274, 3303,
3331, 3360, 3390, 3419, 3448, 3478, 3507, 3537, 3567, 3597, 3627, 3658, 3688, 3719, 3749,
3780, 3811, 3842, 3873, 3905, 3936, 3968, 3999, 4031, 4063, 4095}; //gamma corrected channel value.
int OUTPUT_NUMBER;
int CHANNEL_NUMBER;
int CHANNEL_VALUE;
int OUTPUT_VALUE;
int OE = 3; //OE = GPIO 3.
E131 e131;

void setup() {
Wire.begin(0,2); //start Wire and set SDA - SCL
Serial.begin(115200); //start serial for debug.

//========== PCA setup ==========//

Wire.beginTransmission(0x40); //start comms with board 0x40.
Wire.write(00); //move pointer to MODE 1 register.
Wire.write(0xa1); //enables ALL CALL ADDRESS, Register Auto-Increment & Restart.
Wire.endTransmission(); //end comms with board.

if(debug) {Serial.println("board 0x40 first setup done");}

Wire.beginTransmission(0xE0); //start comms with all boards (ALLCALLADR).
Wire.write(01); //move pointer to MODE 2 register.
Wire.write(0x02); //(0x04) invert outputs for use with FETs - (0x02) to set as source.
Wire.endTransmission(); //end comms with board.
if(debug) {Serial.println(); Serial.println("all boards second setup done");}
Wire.beginTransmission(0xE0); //start comms with all boards (ALLCALLADR).
Wire.write(0x06); //move pointer to LED 0 register.
for (OUTPUT_NUMBER = 0; OUTPUT_NUMBER < 64; OUTPUT_NUMBER++) { //loop 64 times.
Wire.write(0x0F); //write on time 0, off time 0 to all LEDs (all off).
} if(debug) {Serial.println("all boards third setup done");}
Wire.endTransmission(); //end comms with board.

//========== ESP8266-01 setup ==========//
pinMode(3, FUNCTION_3); //change pin 3 from Rx to GPIO 3 (OE output enable).
pinMode(3, OUTPUT);
digitalWrite(3, LOW); //set OE low to enable outputs.
if(debug) {Serial.println("all boards setup complete ");}
//========== e131 setup ==========//
/* Choose one to begin listening for E1.31 data */
e131.begin(ssid, passphrase); /* via Unicast on the default port */
//e131.beginMulticast(ssid, passphrase, universe); /* via Multicast for Universe 1 */
if(debug) {Serial.println("if paused - waiting for e1.31 data ");}
}

void loop() {
/* Parse a packet */
uint16_t num_channels = e131.parsePacket();

/* Process channel data if we have it */
if (num_channels) {

//========== board 0x40 main loop ==========//

Wire.beginTransmission(0x40); //start comms with board 0x40.
if(debug) {Serial.print("board "); Serial.println(0x40);}
Wire.write(0x06); //move pointer to LED 0 register.
for (OUTPUT_NUMBER = 0; OUTPUT_NUMBER < 16; OUTPUT_NUMBER++) { //cycle through outputs.
if(debug) {Serial.print(" | OUTPUT_NUMBER "); Serial.print(OUTPUT_NUMBER);}
CHANNEL_NUMBER = board_40[OUTPUT_NUMBER]; //lookup channel number for output.
if(debug) { Serial.print(" | CHANNEL_NUMBER "); Serial.print(CHANNEL_NUMBER);}
WRITE_FUNCTION(); //call function to finish lookups and write data.
}
Wire.endTransmission();
//0x40 end
if(debug) { Serial.println("End of loop");}
}} //if & void loop end

//========== write output function ==========//
void WRITE_FUNCTION() { //function to finish lookups and write data to PCA9685. Registers set to auto increment.
switch (CHANNEL_NUMBER) {
case 0: //check if channel unused (0).
OUTPUT_VALUE = 0; //set value to 0.
if(debug) { Serial.print(" | UN-USED_OUTPUT ");}
break;
default: //if here channel must bee used so get value.
CHANNEL_VALUE = e131.data[CHANNEL_NUMBER - 1]; //lookup value of channel.
if(debug) { Serial.print(" | CHANNEL_VALUE "); Serial.print(CHANNEL_VALUE);}
OUTPUT_VALUE = gamma_val[CHANNEL_VALUE]; //lookup gamma corrected value.
}
Wire.write (0x00); //switch on time LSB - switches on at zero.
if(debug) { Serial.print(" | LSB ON "); Serial.print(0x00);}
Wire.write (0x00); //switch on time MSB.
if(debug) { Serial.print(" | MSB ON "); Serial.print(0x00);}
Wire.write (OUTPUT_VALUE); //switch off time LSB - switches off after OUTPUT_VALUE cycles.
if(debug) { Serial.print(" | LSB OFF "); Serial.print((OUTPUT_VALUE));}
OUTPUT_VALUE = OUTPUT_VALUE >> 8; //bit shift to remove LSB.
Wire.write (OUTPUT_VALUE); //switch off time MSB.
if(debug) { Serial.print(" | MSB OFF "); Serial.println(OUTPUT_VALUE);}
} //write output end

-----------------------------------------------------------------------------
Thank you again for the code, without this base, I would probably never have reached a result. :roll: