I'm writing a library for Charlieplexed LED display (trying to write a common one actually). The library is working quite fine. In my arduino sketch, I'm using an integer to keep track of the LED, currently on. The integer value increases with time and the LED(s) glow one by one. The problem is, after couple of iterations, this integer value just goes out of chart (in Serial monitor, it has garbage values).
MatrixCharlieplex mch(Pins, 5, MXCHARLIE_CA); // instance of Class
int i = 0, j = 0;
boolean busy = false;
unsigned long time;
void setup() {
//Serial.begin(9600);
time=millis();
}
void loop() {
if(millis() - time > 100){ // runs every 100 ms interval // corrected from (millis() > (time + 100))
time=millis();
blink();
}
}
void blink(){
if(!busy){
busy = true;
//Serial.print("i:\t");
//Serial.println(i);
//Serial.print("Node:\t");
//Serial.println(i+1);
mch.TurnOn(i+1);
increase();
busy = false;
}
}
void increase(){
i = (++i % 20); // resets after every 20 intervals
}
Output:
i: 16
Node: 17
i: 17
Node: 18
i: 18
Node: 19
i: 19
Node: 20
i:16 <-- Stopped working here. it should be "i: 0" & "Node: 1"
16
Node: 17
17
Node: 18
18
Node: 19
19
Node: 20
16
16
Node: 17
17
Node: 18
18
Node: 19
19
Node: 20
The sketch can also be found here
Update: There were no hardware-fault. The problem lies in the struct variable declaration as pointers.updated the question & posted answer. The files in the github links have also been updated.
3 Answers 3
If you have a Charliplexing board like mine (or even if you made your own) the problem could be electrical. If you turn on a lot of LEDs you may exceed the current consumption allowed and then the processor hangs or resets.
I suggest current-limiting resistors like this:
-
Thanks @Nick for replying. Yes, I've made one myself and there are resistors for all pins. The library itself doesn't allow turning on more than one LED at a time. i.e. it keep tracks of which one is currently on and a new request will turn the current one off & then turn the new LED on. Hence, there is no chance of over-current consumption. :(Tamal Patra– Tamal Patra2016年04月11日 05:08:39 +00:00Commented Apr 11, 2016 at 5:08
First thing to do is find out whether your problem is electrical or not.
This can be done by removing the electrical parts not crucial to run the code.
If the problem is code related try using another board to eliminate some hardware malfunction on your board.
Doing these steps will give you some insight on the root cause of the problem.
After much trial and error effort, I've found the solution.
There was no problem in the hardware side (I ran the sketch with serial monitor after detaching the LED-matrix and all non-crutial hardwares. Thanks @jantje and @NickGammon fot the tips).
Apparently Arduino is not capable of handling pointers in advanced Oops fashion. The library itself used it heavily, specially "struct pointers" as function parameters and as return types (Arduino messed up these pointer values with other variable declarations in memory). After removing those pointers from the code and replace them with normal variables, the code runs well.
// correct definition
typedef struct {
uint8_t vcc;
uint8_t gnd;
} DiodeNode;
// wrong implementation
DiodeNode* node = new DiodeNode();
node->vcc = x;
node->gnd = y;
// correct implementation
DiodeNode node = (DiodeNode){x, y};
// or
DiodeNode node;
node.vcc = x;
node.gnd = y;
return node; // from functions etc.
millis() > time + 100
, you should test formillis() - time > 100
instead. Otherwise your code will fail when millis rolls over. Simple rule: do not compare timestamps, compare duration instead.