Hello everyone!
I’m not sure if this should be here in the Arduino Section, or in with Projects and Product questions, but as I’m attempting this with an Uno, I might as well try here first.
Anyways, I’m trying to write my own code to control a single MegaBrite, as I only have one, and [I don’t understand the example code provided in the documentation at all. I get that the packets are 10 bits per colour, and then two control bits, and I believe I’ve constructed my code properly, but in testing, my code works properly up to around 1/4 to 1/2 power and then behaves very oddly, strobing if I’m trying to fade from 0-1023.
[Here’s a link to a youtube video of the current behaviour while running my code
And here, finally, is my code:
/*
Megabrite minus bewildering example code
I can wrap my head around shiftout.
*/
// Pins
const int clockpin = 2; // CI
const int latchpin = 3; // LI
const int datapin = 4; // DI
void setup()
{
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
pinMode(datapin, OUTPUT);
sendToMegabrite(0,0,0); // All off.
}
void loop()
{
for (int i = 0; i < 1024; i++)
{
sendToMegabrite(i,i,i);
delay(10);
}
}
void sendToMegabrite(int red, int green, int blue)
{
unsigned long dataout = 0; // Packet storage
// Assemble color packet
dataout += blue; // Blue level
dataout = dataout << 10; // Make room for red
dataout += red; // Red level
dataout = dataout << 10; // Make room for green
dataout += green; // Green level
dataout = dataout << 2; // Make room for control bits, leave zero!
// Send color packet
digitalWrite(latchpin, LOW);
shiftOut(datapin, clockpin, MSBFIRST, dataout >> 24); // First byte
shiftOut(datapin, clockpin, MSBFIRST, dataout >> 16); // Second byte
shiftOut(datapin, clockpin, MSBFIRST, dataout >> 8); // Third byte
shiftOut(datapin, clockpin, MSBFIRST, dataout); // Fourth byte
digitalWrite(latchpin, HIGH);
}
I have no idea what I’m doing wrong, and if someone could point me in the right direction, and/or explain what the heck is going on in that example code, it would be greatly appreciated.
Small edit: I’m controlling the enable pin with a physical switch, so I don’t blind myself quite so much when I take off the diffuser I made.](megabrite [macetech documentation])](http://www.youtube.com/watch?v=vh3KtGIuaPU)](megabrite [macetech documentation])