26 PWM ports on 2 mega's running as master and slave

Hi,

I made a wall of glass "bricks" all with their own high power LED behind it. I can drive them with PWM.

What I did now is kind of a complex system, just because I didn't know better: I drive 13 pwm ports on the "master" arduino mega, and the other 13 on the "slave". If i want to trigger the leds on the slave device, the master sends data (port number and pwm value, both in one line) over the serial port, and the slave interprets this line and parses port number and pwm value.

Isn't there a better and simpler way to do this?

For example, is there a way to say: analogWrite(ledPins, value); where ledPins comes from an array, and the code 'knows' whereto to output? So for example i say:

analogWrite(ledPins[5], 255); //goed to 5th pwm port on master arduino
analogWrite(ledPins[15], 255); //goes to 2nd pwm port on slave arduino

and the code knows that ledPins[14] and up needs to address the slave arduino.

Is this somewhat understandable by the way? I'm a beginning programmer...

Many thanks!

Best, Mauirts

A simple if statement? Test if number greater than or equal to 13 and send to 2nd Arduino,

MauritsWoudenberg:
For example, is there a way to say: analogWrite(ledPins, value); where ledPins comes from an array, and the code ‘knows’ whereto to output? So for example i say:

analogWrite(ledPins[5], 255); //goed to 5th pwm port on master arduino
analogWrite(ledPins[15], 255); //goes to 2nd pwm port on slave arduino

You can’t do it quite like that but you could write your own function to have that effect. Something like

void myCustomAnalogWrite( byte pinNum, byte pwmVal) {
   if (pinNum < 15) {
      analogWrite(pinNum, pwmVal);
   }
   else {
      // code to send data to other Mega
   }
}

…R

sterretje, yes thats exactly how I did it now. But it got really messy when I was programming light patterns where I had thing like:

analogWrite(ledPins[count] + 1, 255);
analogWrite(ledPins[count] + 2, 255);
analogWrite(ledPins[count] + 3, 255);
etc.

So I think I'll have a look at creating a function. Never done it, so I think I should be able to :slight_smile:

thanks both

One day you will discover the PCA9685 (ebay).
16 channels of 12-bit PWM, and boards can be daisy-chained.
A Nano could drive that.
Leo..

MauritsWoudenberg:
sterretje, yes thats exactly how I did it now. But it got really messy when I was programming light patterns where I had thing like:

If you show your current code for both master and slave, we can see what and how you implemented. Do not forget to use code tags around your code ([code] and [/code]).

Or as your using megas STOP using hardware PWM and doit in software (See blink w/o delay) and then you will not need the second arduino and you can get rin of all that comms code.

Mark

Yes I tried using the PCA9685. But the thing is they have another duty cycle, so my hardware was reacting differently.

Since I had to power up to 20W and the LED’s that I used didn’t take PWM I needed to make a circuit for each of them (active low pass filter into an op-amp to have the right gain, and then another op-amp to offset it to the desired range of 6 - 10V, driving a power mosfet to feed the LED’s stable DC).
I made this circuit 26 times (which took me a lot of time) just to find out that it didn’t work with the PCA9685. So either I’d have to recalculate and change all of the resistors in 26 circuits, or I did it with 2 mega’s. I chose the last.

Thanks to the Robin2 I made a function which works perfectly. Now I can make all kinds of cool looking light patterns.

Thanks all!

Power LEDs need ofcourse switching LED drivers, not linear control with opamps (inefficient/hot).
This is my home-grown attempt, with PT4115E drivers.
24volt in, 16*12watt LED channels out (no other wiring).
Has onboard ESP8266 WiFi.
16 more channels with a slave board.
Leo..