So, I'm definitely a newbie to all of this, coding, Arduino, everything.
My goal starting out was to write a program to light LED's inside my drums when they are hit. I have this part pretty well figured out, and honestly I would be all set with my project except that I need 15 PWM OUT pins, and even if I bought a Mega (I have an Uno), I wouldn't have enough.
(Worth noting, I am using 12v LED RGB strips (non-addressable), and cannot change that now to addressable strips as I have invested too much coin into this setup. I am able to run my current program using MOSFETs, and it works just as I want it to. )
So, research research, come to the Adafruit TLC5947. Great, perfect! Just what I need, multiple PWM outputs from any board. I purchase one, and start to look in to the example code. None of it makes any sense to me. I try to read about it, but I just am not understanding from a practical application standpoint, how to use it. I don't understand shift registers (I get the concept, but not how they actually work in practice or in code). I don't know how to just get from (purposefully not using code tags to save space for this example):
"analogWrite(redPin_1, redVal);
analogWrite(grnPin_1, grnVal);
analogWrite(bluPin_1, bluVal);"
to (paraphrased psuedocode):
"analogWrite(tlc5947.redPin_1, redVal);
analogWrite(tlc5947.grnPin_1, grnVal);
analogWrite(tlc5947.bluPin_1, bluVal);"
I realize that obviously there's a lot more to the function than a simple analogWrite, but that's just to illustrate what my end goal is with each output from the TLC.
On Adafruit's website, their example code is extremely cryptic to me. I cannot make sense of it. On their programming page, they show their library reference as:
Adafruit_TLC5947(uint8_t n, uint8_t c, uint8_t d, uint8_t l)
Call the constructor to create an instance of the TLC5947 PWM breakout driver.
n = Number of Drivers (>1 if the drivers are chained)
c = Clock pin
d = Data pin
l = Latch pin
boolean begin(void);
Call begin just once in your setup() function to initialize the devices.
void setPWM(uint8_t chan, uint16_t pwm);
Call this to set the PWM level for a channel.
chan = Channel
pwm = PWM level (0 = minimum, 4095 = maximum)
void setLED(uint8_t lednum, uint16_t r, uint16_t g, uint16_t b);
Call this to set the RGB value for a group of 3 channels
lednum = LED number (channel number of the "red" pin divided by 3)
r = red level
g = green level
b = blue level
void write(void);
Call this after every change to write the new PWM levels to the device.
but then their attached example in their library (as far as I can tell) doesn't use half of those functions:
#include "Adafruit_TLC5947.h"
// How many boards do you have chained?
#define NUM_TLC5974 1
#define data 4
#define clock 5
#define latch 6
#define oe -1 // set to -1 to not use the enable pin (its optional)
Adafruit_TLC5947 tlc = Adafruit_TLC5947(NUM_TLC5974, clock, data, latch);
void setup() {
Serial.begin(9600);
Serial.println("TLC5974 test");
tlc.begin();
if (oe >= 0) {
pinMode(oe, OUTPUT);
digitalWrite(oe, LOW);
}
}
void loop() {
colorWipe(4095, 0, 0, 100); // "Red" (depending on your LED wiring)
delay(200);
colorWipe(0, 4095, 0, 100); // "Green" (depending on your LED wiring)
delay(200);
colorWipe(0, 0, 4095, 100); // "Blue" (depending on your LED wiring)
delay(200);
rainbowCycle(10);
}
// Fill the dots one after the other with a color
void colorWipe(uint16_t r, uint16_t g, uint16_t b, uint8_t wait) {
for(uint16_t i=0; i<8*NUM_TLC5974; i++) {
tlc.setLED(i, r, g, b);
tlc.write();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint32_t i, j;
for(j=0; j<4096; j++) { // 1 cycle of all colors on wheel
for(i=0; i< 8*NUM_TLC5974; i++) {
Wheel(i, ((i * 4096 / (8*NUM_TLC5974)) + j) & 4095);
}
tlc.write();
delay(wait);
}
}
// Input a value 0 to 4095 to get a color value.
// The colours are a transition r - g - b - back to r.
void Wheel(uint8_t ledn, uint16_t WheelPos) {
if(WheelPos < 1365) {
tlc.setLED(ledn, 3*WheelPos, 4095 - 3*WheelPos, 0);
} else if(WheelPos < 2731) {
WheelPos -= 1365;
tlc.setLED(ledn, 4095 - 3*WheelPos, 0, 3*WheelPos);
} else {
WheelPos -= 2731;
tlc.setLED(ledn, 0, 3*WheelPos, 4095 - 3*WheelPos);
}
}
I don't see the use of the "setPWM" loop, and I don't really understand the "write" function (is this just essentially doing "analogWrite" for all three RGB pins simultaneously?
The biggest confusion for me, is just how to simply write the PWM values to all the pins as I want to. I'll post the code for my project in a following comment (combined with everything it exceeds maximum post length), as it's written for my testing using just 3 PWM pins on the UNO, just for reference so maybe it will be a little clearer to understand what I'm trying to do. Hopefully my code is easy enough to follow, I tried to adhere to robin2's guide as best I could, but I'm definitely extremely new to this.
What I'm not understanding is how to reference the TLC outputs as they relate to the UNO Outputs. In other words, what's the simplest way to just make this an "add 24 PWM channels." I know it isn't that simple, but ultimately that's what I would like the end result to resemble, even if there are more steps in between than that concept illustrates.
I apologize if this is a stupid question, I certainly feel very dumb not being able to understand how this all works. I have a feeling it's quite simple, as I've seen it described that way among the many links I've followed trying to understand it, but I just cannot "get" it. If anyone has some sample code that shows the variable definitions, setup and loops that would be great, just so I could grasp how to set this up for my use.
Any help is extremely appreciated.