DMX 512 3 channel rgb; I have a flash problem, after a few seconds when the flash is fast or slow, the flash speed is flashing slowly in a steady state. How can I fix this problem?
mult = DMXSerial.read(dmxaddr);
if (( DMXSerial.read(dmxaddr+4)> 11 ))
{ strobe(); }
else {
for (int i = 0; i < 3; i++)
{ analogWrite(PWM[i], (DMXSerial.read(dmxaddr+i+1)*mult/256)); }
void strobe()
{
unsigned long strobeRate = (DMXSerial.read(dmxaddr+4)); // Set the strobe rate
strobeRate = map(strobeRate, 11, 255, 255, 12); // mappage stroberate
unsigned long tempoMillis = millis();
if (tempoMillis - previousMillis >= strobeRate)
{ previousMillis = tempoMillis; strobeOn = !strobeOn; }
if (strobeOn > 0)
{ for (int i=0; i<3; i++)
{ analogWrite(PWM[i],0); } }
if (strobeOn < 1)
{
analogWrite(PWM[0], (DMXSerial.read(dmxaddr+1)*mult/256));
analogWrite(PWM[1], (DMXSerial.read(dmxaddr+2)*mult/256));
analogWrite(PWM[2], (DMXSerial.read(dmxaddr+3)*mult/256));
}
}
This snippet only shows how you use the library, not which library you use. Post a full code.For Dmx reception i actually dont use any library just simple direct frame by frame reception.
Sorry im not online and dont have much oversight as i am on my phone now, not on my laptop. I saw a delay(700) that most likely shouldnt be there... ill have a better look later.
So there are some strange opening and closing braces in you code (btw it is not common to mark your own post as solution if you still want help)
And there is a strange double if statement, which i am not even quite sure what that results into, but my guess is the issue is cause by this
if (millis() - last_disp >= KEEPDMXDISP * 300) {
display.clearDisplay();
display.display();
setaddr = false;
} // clear screen if no buttons pressed
which clears the screen repeatedly after the first time those 30000 ms have elapsed, without ever updating 'last-disp' within this conditional bit of code, slowing down your strobe effect.
Regardless of course this shows up a major fault in your code that the strobe is part of the main program loop and therefore depends on the part that changes it from light to dark to be called fast enough for the strobe effect to work.
I guess you should switch to a timer for calling strobe(), but initially you should update last_disp within the condition that test for the elapsed time.
A request to clean the code up a bit, mainly in regards to the closing and opening braces, and the cascading if statement (without an opening brace ..) Of course your whole loop() function is quite long and parts should probably be moved into separate functions, but that should not effect functionality, though it will make it easier to find bugs.