Need to increase code efficiency

Thread Starter

Dj Leon

Joined Jan 18, 2020
19
Hi, this is my code to convert a weird type of analogue multiplex to DMX using a DMX interface board on an Arduino UNO. however, it currently doesn't work. It is too slow. im pretty sure I need too as a 70 us interrupt but I am quite new to programming so I therefore do not know how to do that.
Any help adding that or just generally cleaning up my code would be appreciated.

thanks.

C:
/*
  [URL]http://www.arduino.cc/en/Tutorial/Fade[/URL]
*/
#include <DMXSerial.h>

const int leb = 6;
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int param = 0;
#define LebDefaultLevel 225
// the setup routine runs once when you press reset:
void setup() {
DMXSerial.init(DMXReceiver);

  // declare pin 9 to be an output:
pinMode(led, OUTPUT);
pinMode(leb, OUTPUT);
}
void loop() {
// set the brightness of pin 9:
param=1;

analogWrite(led, 255);
delayMicroseconds (140);


analogWrite(led, 0);
  delayMicroseconds (420);
chanmidp1();

for (int a=0; a<16; a++){
chanloop();
chanmid();
  }

}

//the loop of channel info with before pulse
void chanloop(){
for (int i=0; i<6; i++){

analogWrite(led, 255);
delayMicroseconds (70);


analogWrite(led, 0);
delayMicroseconds (70);

brightness = 0.725*(255-DMXSerial.read(param));
analogWrite(leb, brightness);
delayMicroseconds(280);
param = param + 1;


analogWrite(leb, 0);
delayMicroseconds (140);
}
}
void chanmid(){
chanmidp1();
chanmidp1();
}
void chanmidp1(){

analogWrite(led, 255);
  delayMicroseconds (70);
  analogWrite(led,0);
delayMicroseconds (490);
}
Moderators note : used codetags for C
 
Last edited by a moderator:

xox

Joined Sep 8, 2017
838
Maybe start by substituting DMXSerial.read() with a "dummy" function to see if the bottleneck lies there. Otherwise it might be something as simple as shortening the delay lengths. (Anyway instead of hard-coded constants you should be using variables declared at the top of the program to help simplify timing control. )
 

Thread Starter

Dj Leon

Joined Jan 18, 2020
19
Maybe start by substituting DMXSerial.read() with a "dummy" function to see if the bottleneck lies there. Otherwise it might be something as simple as shortening the delay lengths. (Anyway instead of hard-coded constants you should be using variables declared at the top of the program to help simplify timing control. )
Hi, what do you mean by dummy function? Just replace it with something? Could you give me an example? Sorry to bother you. I am unsure what you mean by using variables, would that not be slower than just writing in the raw numbers for delays?
 

xox

Joined Sep 8, 2017
838
Hi, what do you mean by dummy function? Just replace it with something?
Precisely. The function just has to return "sensible" values.

I am unsure what you mean by using variables, would that not be slower than just writing in the raw numbers for delays?
Variables help simplify coding. Rather than have contants scattered about, you have a centralized point to modify initial settings and state for your program.

I also notice that you keep incrementing "param", the channel number. But why?
 

Thread Starter

Dj Leon

Joined Jan 18, 2020
19
I also notice that you keep incrementing "param", the channel number. But why?
[/QUOTE]
Because i need to read 96 dmx channels, and param is the variable im using to identify which channel to read from
 
Top