2

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.

9
  • I tried running your code, but I couldn't reproduce your problem. – Gerben Apr 10 '16 at 16:11
  • 1
    Unrelated to the question... but: you should not test for millis() > time + 100, you should test for millis() - time > 100 instead. Otherwise your code will fail when millis rolls over. Simple rule: do not compare timestamps, compare duration instead. – Edgar Bonet Apr 10 '16 at 18:41
  • @Gerben: Without changing any part of the code? WOW... but mine still freezes. :( – Tamal Patra Apr 11 '16 at 5:50
  • Thanks @Edgar for the millis rollover correction. Corrected in question & github also. – Tamal Patra Apr 11 '16 at 6:26
  • How is the RAM usage? – Paul Apr 11 '16 at 12:12
1

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:

Charliplexing

1
  • 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 Apr 11 '16 at 5:08
0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.