0

I have the following code:

#include "LCD12864RSPI.h"
#include "DFrobot_bmp.h"
#include "DFrobot_char.h"

#define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )
unsigned char wangzhi[]=" www.DFRobot.com ";
unsigned char fused[1024];

void setup()
{
  pinMode(11, OUTPUT);     
  for(int i=0;i<4;i++){
  digitalWrite(11,HIGH);
  delay(1000);
  digitalWrite(11,LOW);
  delay(1000);
  }
  LCDA.initDriverPin(2,7,10); 
  LCDA.Initialise(); // INIT SCREEN  
}
unsigned char* fuse3(unsigned char* a ,unsigned char* b,unsigned char* c){

  int i=0;
  int ac=0;//a counter 
  int bc=0; //b counter 
  int cc=0;//c counter 

  for(int n=0;n<64;n++){
    for(int k=0;k<5;k++){
      fused[i]=a[ac];
       ac++;
       i++;
    }
       for(int k=0;k<5;k++){
        fused[i]=b[bc];
       bc++;
        i++;
    }
        for(int k=0;k<5;k++){
            fused[i]=c[cc];
            cc++;
             i++;
    }
    fused[i]=0x00;
    i++;
  }

  return fused;
}
unsigned char* getMatch(int input){
  input = input & 0x000f;
  switch (input){
   case 0: return g0;
   case 1: return g1;
   case 2: return g2;
   case 3: return g3;
   case 4: return g4;
   case 5: return g5;
   case 6: return g6;
   case 7: return g7;
   case 8: return g8;
   case 9: return g9;
   case 10: return plus;
   case 11: return minus;
   case 12: return times;
   case 13: return g1;
   case 14: return g1;
   case 15: return g1;
   default: return g1;
  }

}
void displayfuse(int number){
// LCDA.DrawFullScreen(fuse3(getMatch(1),getMatch(2),getMatch(3)));
}
void loop(){
  displayfuse(0x1234);

}

This code is a stripped version of the code I need to run to get a bigger project working. But if it somewhat works than the setup should begin by flashing a LED 4 times before continuing. Currently this works. However as you can see I have commented out the line LCDA.DrawFullScreen(fuse3(getMatch(1),getMatch(2),getMatch(3)));. uncommenting this line causes the LED's to flash continuously rather then just 4 times. The library I'm using can be found here. Does anybody know what is going on? To me the first few lines of a program can be affected by a change somewhere this deep inside the code is a form of dark magic.

Weirdly when I change the setup to the following: void setup()

{
  pinMode(11, OUTPUT);     
  for(int i=0;i<15;i++){
  digitalWrite(11,HIGH);
  delay(100);
  digitalWrite(11,LOW);
  delay(100);
  }
  for(int i=0;i<4;i++){
  digitalWrite(11,HIGH);
  delay(1000);
  digitalWrite(11,LOW);
  delay(1000);
  }
  LCDA.initDriverPin(2,7,10); 
  LCDA.Initialise(); // INIT SCREEN  
}

it stops flashing completely.

Edit having just reuploaded the code again this time the led just remains on all the time.

edit edit Now with the following code:

#include "LCD12864RSPI.h"
#include "DFrobot_bmp.h"
#include "DFrobot_char.h"

unsigned char fuse[1024];
void setup()
{

  LCDA.initDriverPin(2,7,10); 

  LCDA.Initialise(); // INIT SCREEN  
  pinMode(11, OUTPUT); 
  digitalWrite(11,HIGH);  

}
unsigned char* fuse3(unsigned char* a ,unsigned char* b,unsigned char* c){
 unsigned char* fused =(unsigned char*)malloc(sizeof(char)*1024);

  int i=0;
  int ac=0;//a counter 
  int bc=0; //b counter 
  int cc=0;//c counter 

  for(int n=0;n<64;n++){
    for(int k=0;k<5;k++){
      fused[i]=a[ac];
      ac++;
       i++;
    }
        for(int k=0;k<5;k++){
      fused[i]=b[bc];
      bc++;
       i++;
    }
        for(int k=0;k<5;k++){
      fused[i]=c[cc];
      cc++;
       i++;
    }
    fused[i]=0x00;
    i++;
  }
  return fused;
}
unsigned char* getMatch(int input){

  input = input & 0x000f;

  switch (input){
   case 0: return g0;
   case 1: return g1;
   case 2: return g2;
   case 3: return g3;
   case 4: return g4;
   case 5: return g5;
   case 6: return g6;
   case 7: return g7;
   case 8: return g8;
   case 9: return g9;
   case 10: return plus;
   case 11: return minus;
   case 12: return times;
   case 13: return g1;
   case 14: return g1;
   case 15: return g1;
   default: return g1;
  }
}
void displayfuse(int number){
 LCDA.CLEAR();
   delay(100);


 int mask1 = 0xF000;
 int mask2 = 0x0F00;
 int mask3 = 0x00F0;

 int firstnumber = number&mask1>>12;
 int operation = number&mask2>>8;
 int secondnumber= number&mask3>>4;

  LCDA.DrawFullScreen(fuse3(getMatch(firstnumber),getMatch(operation),getMatch(secondnumber)));
  delay(5000);
}
void loop(){
  displayfuse(0x1234);
  delay(1000)
}

This causes the led to not turn on but moving the lines where I make pin 11 output and high one up (beyond the LCDA.Initialise(); // INIT SCREEN ) then the led will turn on. It can also be turned on by commenting out the LCDA.DrawFullScreen(fuse3(getMatch(firstnumber),getMatch(operation),getMatch(secondnumber))); line. Magic is happening...

  • 1
    Short answer: You're out of SRAM. – Ignacio Vazquez-Abrams Dec 9 '14 at 9:41
  • Memory usage is at around 6 kB leaving quite a bit around. – Thijser Dec 9 '14 at 9:51
  • Are you running this on a Mega? – Ignacio Vazquez-Abrams Dec 9 '14 at 9:52
  • This is on the "funduino" clone but I have 32 kB avaible and 6 in use. – Thijser Dec 9 '14 at 9:55
  • Is it the Funduino Mega2560? – Ignacio Vazquez-Abrams Dec 9 '14 at 10:14
1

The 'dark magic' is going to be your program rebooting every time it gets to that line. I would imagine (this isn't a library I know) that you're exceeding an array bound & generating a 'segfault', although it could be that you light up your LCD array & the power draw causes a reboot.. Because the LED flash is at the start of setup, it executes quickly, so generating the appearance of continual flashing when it re-enters repeatedly. Your code is going to assign g1[0] when the line in question executes - is g1[] defined to point somewhere valid?

| improve this answer | |
  • Sounds plausible is there a good way of discovering if a segfault occurred? Normally I would use valgrind but what is the arduino option to check for this? – Thijser Dec 9 '14 at 9:52
  • Headwork ;) Where is the code going when this line executes - see edit. – Mark Williams Dec 9 '14 at 10:16
  • I'm not sure about it anymore I added in a second flash pattern (flash fast-flash slow) and now it doesn't even begin to flash anymore. – Thijser Dec 9 '14 at 10:22
  • Oh and g[] is defined in a header file as a 320 chars array. – Thijser Dec 9 '14 at 10:34
  • What about g1[]? – Mark Williams Dec 9 '14 at 10:49

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.