best way to find a crash cause

hi everyone,

i have a working project, that is near the complete stage, but i am finding a random crash after leaving it run for an hour or so. what is the best way to track this down??? here is a quick description.

2 x4way switches
1 x2way switch
1 switch that is tied to an interrupt

all are tied to digital inputs, and the logic of which switch is in which position calls a function to drive 3 BlinkM MaxM drivers to a certain color, or to play the internal script.

there are an serial driven LCD (software serial) that shows an update every 0.5 seconds.

all timer vari's are unsigned long int...

the code is about 1000 lines, anyone feel like having a look??

thanks

How many strings are you using?

each lcd.print("something") is a string that takes up RAM.

i have a few lcd.prints.... they only happen every 0.5seconds

lcd.print("?f"); //clears LCD
lcd.print("?x00y0"); //moves cursor
lcd.print("Mode "); //feedback message
lcd.print(lcdfx, DEC);
lcd.print("?x00y01"); //move cursor
lcd.print(cycles); //counter used to see how long the program is running.

another thing i just noticed, i am using software serial on one of the hardware serial ports - (tx3) - i am using one of the newer Mega boards.

through troubleshooting, i think it is more likely to crash when it is specifically transmitting a bit of information on the i2c bus, as opposed to only sending a "start command every 3 seconds...??

i'll post the code when i get home in a few hours.

thanks

here ya go... happy reading. i think it commented pretty well.

includes / defines / setup / loop. functions in next post.

thanks for anyone that takes a look at this :slight_smile:

#include <SoftwareSerial.h>
#include "BlinkM_funcs.h"
#include "Wire.h"

//pin assignments
//purple tag
#define FivePosA_1  41
#define FivePosA_2  39
#define FivePosA_3  22 //nc
#define FivePosA_4  37
#define FivePosA_5  35

//blue tag
#define FivePosB_1  52
#define FivePosB_2  50
#define FivePosB_3  22 //nc
#define FivePosB_4  48
#define FivePosB_5  46

#define AB_Pos 4
#define USDS_Pos 3

//byte code for ID 1, 2, 3
int setA = 0x01; //id '1'
int setB = 0x02; //id '2'
int setC = 0x03;// id '3'

//button state vari's
int AB_State = 0;
volatile int USDS_State = LOW;

//BS switch
int A_1 = 0;
int A_2 = 0;
int A_3 = 1;
int A_4 = 0;
int A_5 = 0;

//FOH Switch
int B_1 = 0;
int B_2 = 0;
int B_3 = 1;
int B_4 = 0;
int B_5 = 0;

//last function stuff. 
int lastfx = 3;
int USDS_Last = 0;

//start testing here
//blinking timers
int fxblink = 300; //how fast to blink the feedback LED's
int ledState = 0;
unsigned long previousMillis = 0;

unsigned long fx3currentMillis = 0;
unsigned long fx3previousMillis = 0;
int fx3send = 3000; //how fast to resend fx3

//setup for LCD screen
int lcd_rxPin = 15;
int lcd_txPin = 14;

SoftwareSerial lcd = SoftwareSerial(lcd_rxPin, lcd_txPin); 

unsigned long currentMillis_lcd = 0; //update the lcd
unsigned long previousMillis_lcd = 0;
unsigned long lcdUpdate = 500;
unsigned long cycles = 0;
int lcdfx = 0;

//------------------------
//color programming is here, 
//not in functions (testing memory management crash idea)

//fx1 colors
byte fx1R = 0x00;
byte fx1G = 0xff;
byte fx1B = 0x00;

//------------------------
//fx2 colors
byte fx2R = 0x80;
byte fx2G = 0x00;
byte fx2B = 0x00;

//color array for fx2
byte fx2color[20] = {
    0x60, 0x80, 
	0xff, 0x80,    
    0xff, 0xff, 
	0x80, 0x60, 
	0x40, 0x20, 
    0x40, 0x60, 
    0x80, 0xff, 
    0x80, 0x60,    
    0x40, 0x20,
    0x20, 0x40
}; //array

//------------------------
//fx3 colors
byte fx3R = 0x00;
byte fx3G = 0x00;
byte fx3B = 0x00;

//------------------------
//fx4 colors
byte fx4R = 0x80;
byte fx4G = 0x00;
byte fx4B = 0x14;

//color array for fx4
byte fx4color[48] = {
    0x0A, 0x0a, 
    0x0A, 0x0a,
    0x0A, 0x0a,
    
    0x14, 0x14, 
    0x14, 0x14,
    0x14, 0x14,
    
    0x1e, 0x1e,
    0x1e, 0x1e,
    0x1e, 0x1e,
    
    0x32, 0x32, 
    0x32, 0x32,
	0x32, 0x32,
	
    0x3c, 0x3c, 
    0x3c, 0x3c,
	0x3c, 0x3c,     
    
    0x46, 0x46,
    0x46, 0x46,
	0x46, 0x46,    
    
    0x50, 0x50, 
    0x50, 0x50,
	0x50, 0x50, 
    
    0x5a, 0x5a,    
    0x5a, 0x5a,
    0x5a, 0x5a
    
    }; //array

//fx5 colors
byte fx5R = 0x00;
byte fx5G = 0x00;
byte fx5B = 0x01;

//------------------------------------------------------------------
//help, stud functions, prints to serial monitor via USB
void help(){
  Serial.println("\r\nAutoLed Controller (and feedback)\n"
    "\n" 
    );

}//help
//------------------------------------------------------------------
//required setup
void setup(){

  BlinkM_beginWithPower();
  
  //switch A setup
  pinMode(FivePosA_1, INPUT);
  pinMode(FivePosA_2, INPUT);  
  pinMode(FivePosA_3, INPUT);
  pinMode(FivePosA_4, INPUT);  
  pinMode(FivePosA_5, INPUT);  

  //turning on pull-ups so no external components
  digitalWrite(FivePosA_1,HIGH);
  digitalWrite(FivePosA_2,HIGH);
  digitalWrite(FivePosA_3,HIGH);
  digitalWrite(FivePosA_4,HIGH);
  digitalWrite(FivePosA_5,HIGH);

  //switch B setup  
  pinMode(FivePosB_1, INPUT);
  pinMode(FivePosB_2, INPUT);  
  pinMode(FivePosB_3, INPUT);
  pinMode(FivePosB_4, INPUT);  
  pinMode(FivePosB_5, INPUT);  

  //turning on pull-ups so no external components
  digitalWrite(FivePosB_1,HIGH);
  digitalWrite(FivePosB_2,HIGH);
  digitalWrite(FivePosB_3,HIGH);
  digitalWrite(FivePosB_4,HIGH);
  digitalWrite(FivePosB_5,HIGH);

  //A B selector setup
  pinMode(AB_Pos,INPUT);    
  digitalWrite(AB_Pos,HIGH);

  //UpStage DownStage position switch,  3 (interrupt 1)
  pinMode(USDS_Pos,INPUT);
  digitalWrite(USDS_Pos,HIGH);

  attachInterrupt(1, USDS_Funct, CHANGE);

  previousMillis = millis();

  //starts feedback USB     
  Serial.begin(19200);  
  help();

  //starts feedback LCD  
  pinMode(lcd_txPin, OUTPUT);
  lcd.begin(9600);      // 9600 baud is chip comm speed
  lcd.print("?G216");   // set display geometry,  2 x 16 characters in this case
  delay(100);	        // pause to allow LCD EEPROM to program
  lcd.print("?Bff");    // set backlight to 40 hex
  delay(100);           // pause to allow LCD EEPROM to program

  //lcd startup loop
  // lcdStartup();
}//setup
//------------------------------------------------------------------
//required loop
void loop(){

  currentMillis_lcd = millis();
  cycles++;
//only update the LCD status screen every 0.5 seconds, max refesh, could be longer, color fades

  if (lcdUpdate < currentMillis_lcd - previousMillis_lcd){ 
    //update the LCD screen
    lcd.print("?f");                   // clear the LCD
    lcd.print("?x00?y0");
	lcd.print("Mode ");
	lcd.print(lcdfx, DEC);

    lcd.print("?x00?y1");

    //feedback to see if it crashes at a count
	lcd.print(cycles);

//	lcd.print("USDS ");
//	lcd.print(USDS_State, DEC);	
//	lcd.print(" AB ");
//    lcd.print(AB_State, DEC);	

//    lcd.print("BS ");
//    lcd.print(A_1, DEC);
//    lcd.print(A_2, DEC);      
//    lcd.print(A_3, DEC);
//    lcd.print(A_4, DEC);
//    lcd.print(A_5, DEC);
   
   previousMillis_lcd = currentMillis_lcd;  
  }//if lcdUpdate

//read AB switch
  AB_State = digitalRead(AB_Pos);
  
//read USDS switch
  USDS_State = digitalRead(USDS_Pos);

//process A or B input pins based on AB switch, call color fx based on logi
  if (AB_State > 0){
    //process all of switch A's pins  
    A_1 = digitalRead(FivePosA_1);
    A_2 = digitalRead(FivePosA_2);
    A_3 = 1; //must be hardvalued for logic to work
//    A_3 = digitalRead(FivePosA_3); //not conected.
    A_4 = digitalRead(FivePosA_4);
    A_5 = digitalRead(FivePosA_5);

    //call function 1 based on switch position
    if (A_1 == 0 && A_2 == 1 && A_3 == 1 && A_4 == 1 && A_5 == 1){
      ledfunction1();
    }
    //call function 2 based on switch position
    if (A_1 == 1 && A_2 == 0 && A_3 == 1 && A_4 == 1 && A_5 == 1){
      ledfunction2();
    }
    //call function 3 based on switch position (this condition should never happen)
    if (A_1 == 1 && A_2 == 1 && A_3 == 0 && A_4 == 1 && A_5 == 1){
      ledfunction3();
    }
    //call function 4 based on switch position      
    if (A_1 == 1 && A_2 == 1 && A_3 == 1 && A_4 == 0 && A_5 == 1){
      ledfunction4();
    }
    //call function 5 based on switch position      
    if (A_1 == 1 && A_2 == 1 && A_3 == 1 && A_4 == 1 && A_5 == 0){
      ledfunction5();
    }
    //this is the default function. this is called if no switch is attached
    if (A_1 == 1 && A_2 == 1 && A_3 == 1 && A_4 == 1 && A_5 == 1){
      ledfunction3();
    }

    //is this causing a problem???
    //ledfunction3();

  }//ab if

//process all of switch B's pins
  else{          
    B_1 = digitalRead(FivePosB_1);
    B_2 = digitalRead(FivePosB_2);
    B_3 = 1; //must be hardvalued for logic to work
//    B_3 = digitalRead(FivePosB_3); //not connected
    B_4 = digitalRead(FivePosB_4);
    B_5 = digitalRead(FivePosB_5);

    //call function 1 based on switch position    
    if (B_1 == 0 && B_2 == 1 && B_3 == 1 && B_4 == 1 && B_5 == 1){
      ledfunction1();      
    }
    //call function 2 based on switch position
    if (B_1 == 1 && B_2 == 0 && B_3 == 1 && B_4 == 1 && B_5 == 1){
      ledfunction2();
    }
    //call function 3 based on switch position (this condition should never happen)
    if (B_1 == 1 && B_2 == 1 && B_3 == 0 && B_4 == 1 && B_5 == 1){
      ledfunction3();
    }
    //call function 4 based on switch position
    if (B_1 == 1 && B_2 == 1 && B_3 == 1 && B_4 == 0 && B_5 == 1){
      ledfunction4();
    }
    //call function 5 based on switch position
    if (B_1 == 1 && B_2 == 1 && B_3 == 1 && B_4 == 1 && B_5 == 0){
      ledfunction5();
    }
    //this is the default function. this is called if no switch is attached
    if (B_1 == 1 && B_2 == 1 && B_3 == 1 && B_4 == 1 && B_5 == 1){
      ledfunction3();
    }

    //mode 3 is default function
    //ledfunction3();

  } //ab ifelse


}//loop

part 2... functions.

(the lcdStartup function is commented out of the current code... not contributing to the problem)

//------------------------------------------------------------------
//fx 1, solid green at full
//good, test this
void ledfunction1(){
  //green
  Serial.println("fx1 - green ");
  lcdfx = 1;
	//if last fx was 3, stop default script, fade to black, continue
	if ( lastfx == 3 ){
	//stop all 
	    BlinkM_stopScript( setA );
	    BlinkM_stopScript( setB );
	    BlinkM_stopScript( setC );
		delay(100);	
		
	//fade to black on all
        BlinkM_fadeToRGB( setA, 0,0,0 );
        BlinkM_fadeToRGB( setB, 0,0,0 );
        BlinkM_fadeToRGB( setC, 0,0,0 );   

		delay( 100 );	
	}

	//the color we are sending to the drivers
//  	byte R = 0x00;
//  	byte G = 0xff;
//  	byte B = 0x00;

  //which drivers are we sending to. USDS mode
  if( USDS_State > 0 ){

	//turnoff set C if we have just changed
		if ( USDS_Last == 0 ){
       	 BlinkM_fadeToRGB( setC, 0,0,0 );   
		}

    //send LED drivers here USDS set 1    
    BlinkM_setRGB( setA, fx1R, fx1G, fx1B );
    BlinkM_setRGB( setB, fx1R, fx1G, fx1B );      

//keeping track of the last USDS mode
	USDS_Last = 1;
  }

  //USDS mode      
  else{

    //send LED drivers here USDS set 2          
    BlinkM_setRGB( setA, fx1R, fx1G, fx1B );
    BlinkM_setRGB( setB, fx1R, fx1G, fx1B );      
    BlinkM_setRGB( setC, fx1R, fx1G, fx1B );      

	//keeping track of the last USDS mode
	USDS_Last = 0;
	
  }
	//last funcation sent to the drivers, so that we are cleaning up what is being sent to them. 
	lastfx = 1;
}//fx1
//------------------------------------------------------------------
//fx 2, flashing red
//good, test this
void ledfunction2(){
  //flashing red
  Serial.println("fx2 - F-red ");
  lcdfx = 2;
/*
//fill this in correctly
  byte color[20] = {
    0x60, 0x80, 
	0xff, 0x80,    
    0xff, 0xff, 
	0x80, 0x60, 
	0x40, 0x20, 
    0x40, 0x60, 
    0x80, 0xff, 
    0x80, 0x60,    
    0x40, 0x20,
    0x20, 0x40
  
  }; //array
*/
    //byte R = 0x80; 
	//byte G = 0x00;
  	//byte B = 0x00;

	//if last fx was 3, stop default script, fade to black, continue
	if (lastfx ==3 ){
	//stop all 
	    BlinkM_stopScript(setA);
	    BlinkM_stopScript(setB);
	    BlinkM_stopScript(setC);	
	    
		delay(100);	
		
	//fade to black on all
        BlinkM_fadeToRGB( setA, 0,0,0);
        BlinkM_fadeToRGB( setB, 0,0,0);
        BlinkM_fadeToRGB( setC, 0,0,0);   

		delay(100);	
	} //3-check

  //USDS mode
  if( USDS_State > 0 ){

	//turnoff set C if we have just changed
		if ( USDS_Last == 0 ){
       	 BlinkM_fadeToRGB( setC, 0,0,0 );   
		} //c-check
 
 
	//reading array values 1-20, pausing for a bit after each send
    for(int i = 0; i < 19; i++){
		//setA, setB
	    BlinkM_setRGB( setA, fx2color[i], fx2G, fx2B );
	    BlinkM_setRGB( setB, fx2color[i], fx2G, fx2B );
		delay(12); //flash rate
    } // for

	//keeping track of the last USDS mode
	USDS_Last = 1;
	
	}//if
  
  	//USDS mode      
  	else{
  
	//reading array values 1-20, pausing for a bit after each send
    for(int i = 0; i < 19; i++){
		//setA, setB
	    BlinkM_setRGB( setA, fx2color[i], fx2G, fx2B );
	    BlinkM_setRGB( setB, fx2color[i], fx2G, fx2B );
  	    BlinkM_setRGB( setC, fx2color[i], fx2G, fx2B );
		delay(12); //flash rate
    }// for

  	//keeping track of the last USDS mode
  	USDS_Last = 0;
  }//else

	//last funcation sent to the drivers, so that we are cleaning up what is being sent to them. 
	lastfx = 2;

}//fx2

part 3... (there's got to be a better way to do this...)

//fx3, default play internal driver script
//testing...
void ledfunction3(){
  //default internal script (stop, play script 0, see example)

  Serial.println("fx3 ");
  lcdfx = 3;
//  lcd.print("fx3 ");

  unsigned long currentMillis = millis(); 

  fx3currentMillis = millis();

  //USDS mode
  if(USDS_State > 0){
	 
	//turnoff set C if we have just changed
		if ( USDS_Last == 0 ){
		 BlinkM_stopScript( setC );   
       	 BlinkM_fadeToRGB( setC, 0,0,0 );   

		 Serial.println("off set C ");
	} //c-check fx3 special case	 
	
	
	//does this work? volitle variables? 
    //start default script for script a, b every x000ms (fx3send duration)
    if(fx3currentMillis - fx3previousMillis > fx3send){
       fx3previousMillis = fx3currentMillis;         

		startscript(setA);
		startscript(setB);

	    Serial.println("send AB");  
 	 } //if fx3send

	//keeping track of the last USDS mode
	USDS_Last = 1;
  
  } //if USDS

  //USDS mode      
  else{

    //start default script for script a, b ,c 
    if(fx3currentMillis - fx3previousMillis > fx3send){
      fx3previousMillis = fx3currentMillis;         

      startscript(setA);
      startscript(setB);
      startscript(setC);                    
    
      Serial.println("send ABC");  
    } //else fx3send
	
	//keeping track of the last USDS mode
	USDS_Last = 0;
	
  }//else USDS

	//last funcation sent to the drivers, so that we are cleaning up what is being sent to them. 
	lastfx = 3;

}//fx3
//------------------------------------------------------------------
//fx 4, pulsing red at half
//good, test this
void ledfunction4(){
//pulsing red, half intensity
  Serial.println("fx4 red h ");
  lcdfx = 4;
  
/*
//raw colors sent from the loop, for fx4, Red valve
  byte color[48] = {
    0x0A, 0x0a, 
    0x0A, 0x0a,
    0x0A, 0x0a,
    
    0x14, 0x14, 
    0x14, 0x14,
    0x14, 0x14,
    
    0x1e, 0x1e,
    0x1e, 0x1e,
    0x1e, 0x1e,
    
    0x32, 0x32, 
    0x32, 0x32,
	0x32, 0x32,
	
    0x3c, 0x3c, 
    0x3c, 0x3c,
	0x3c, 0x3c,     
    
    0x46, 0x46,
    0x46, 0x46,
	0x46, 0x46,    
    
    0x50, 0x50, 
    0x50, 0x50,
	0x50, 0x50, 
    
    0x5a, 0x5a,    
    0x5a, 0x5a,
    0x5a, 0x5a
    
    }; //array
*/
//decimals by 10's
/* decimal to hex, by 10's
10 - 0a
20 - 14
30 - 1e
50 - 32
60 - 3c
70 - 46
80 - 50
90 - 5a
100 - 64
110 - 6e

120 - 78
130 - 82
140 - 8c
150 - 96
160 - a0
170 - aa
180 - b4
190 - be
200 - c8
210 - d2
220 - dc
230 - e6
240 - f0
250 - fa

*/

	//if last fx was 3, stop default script, fade to black, continue
	if (lastfx ==3 ){
	//stop all 

	    BlinkM_stopScript(setA);
	    BlinkM_stopScript(setB);
	    BlinkM_stopScript(setC);	
	    
		delay(100);	
		
	//fade to black on all
        BlinkM_fadeToRGB( setA, 0,0,0);
        BlinkM_fadeToRGB( setB, 0,0,0);
        BlinkM_fadeToRGB( setC, 0,0,0);   

		delay(100);	

	} //3-check

  //USDS mode
  if( USDS_State > 0 ){

	//turnoff set C if we have just changed
		if ( USDS_Last == 0 ){
       	 BlinkM_fadeToRGB( setC, 0,0,0 );   
		} //c-check
 
 
	//fade up, reading array values 1-20, pausing for a bit after each send
    for(int i = 0; i < 47; i++){
		//setA, setB
	    BlinkM_setRGB( setA, fx4color[i], fx4G, fx4B );
	    BlinkM_setRGB( setB, fx4color[i], fx4G, fx4B );
		delay(15);
    } // f_up

	
	//fade down, reading array values 1-20, pausing for a bit after each send
	for(int i = 47; i >0 ; i--){
		//setA, setB
		
		BlinkM_setRGB( setA, fx4color[i], fx4G, fx4B );
	    BlinkM_setRGB( setB, fx4color[i], fx4G, fx4B );
		delay(15);
  	} //f_down


	//keeping track of the last USDS mode
	 	USDS_Last = 1;
	
	}//if
  
  	//USDS mode      
  	else{
  
	//fade up, reading array values 1-20, pausing for a bit after each send
    for(int i = 0; i < 47; i++){
		//setA, setB
	    BlinkM_setRGB( setA, fx4color[i], fx4G, fx4B );
	    BlinkM_setRGB( setB, fx4color[i], fx4G, fx4B );
  	    BlinkM_setRGB( setC, fx4color[i], fx4G, fx4B );
		delay(15);
    } // f_up

	//fade down, reading array values 1-20, pausing for a bit after each send
	for(int i = 47; i > 0 ; i--){
		//setA, setB
		
		BlinkM_setRGB( setA, fx4color[i], fx4G, fx4B );
	    BlinkM_setRGB( setB, fx4color[i], fx4G, fx4B );
  	    BlinkM_setRGB( setC, fx4color[i], fx4G, fx4B );	    
		delay(15);
  	}//f_down

  	//keeping track of the last USDS mode
  	USDS_Last = 0;
  } //else
	
	//last funcation sent to the drivers, so that we are cleaning up what is being sent to them. 
	lastfx = 4;

}//fx4
//------------------------------------------------------------------
//fx 5, show mode  (currently 1% blue )
//good, test this
void ledfunction5(){
//solid green at half
  Serial.println("fx5 grn ");
  lcdfx = 5;
 //if last fx was 3, stop default script, fade to black, continue
	if (lastfx == 3 ){
	//stop all 
	    BlinkM_stopScript( setA );
	    BlinkM_stopScript( setB );
	    BlinkM_stopScript( setC );	
		
		delay(100);	
		
	//fade to black on all
        BlinkM_fadeToRGB( setA, 0,0,0);
        BlinkM_fadeToRGB( setB, 0,0,0);
        BlinkM_fadeToRGB( setC, 0,0,0);   

		delay(100);	
	} //3-check
  
 //solid blue 1% intensity
 // byte R = 0x00;
 // byte G = 0x00;
 // byte B = 0x01;

  //which drivers are we sending to. USDS mode
  if( USDS_State > 0 ){

	//turnoff set C if we have just changed
		if ( USDS_Last == 0 ){ 	 
       	 BlinkM_fadeToRGB( setC, 0,0,0 );   
		}

    //send LED drivers here USDS set 1    
    BlinkM_setRGB( setA, fx5R, fx5G, fx5B );
    BlinkM_setRGB( setB, fx5R, fx5G, fx5B );      

	//keeping track of the last USDS mode
	USDS_Last = 1;
  }//if

  //which drivers are we sending to. USDS mode      
  else{

    //send LED drivers here USDS set 2          
    BlinkM_setRGB( setA, fx5R, fx5G, fx5B );
    BlinkM_setRGB( setB, fx5R, fx5G, fx5B );      
    BlinkM_setRGB( setC, fx5R, fx5G, fx5B );      

	//keeping track of the last USDS mode
	USDS_Last = 0;
	
  }//else

  //last funcation sent to the drivers, so that we are cleaning up what is being sent to them. 
  lastfx = 5;

}//fx5
//fx to handle sycning the internal scripts
void startscript(byte blinkm_addr){
  //stop script
  BlinkM_stopScript( blinkm_addr );
  delay(10);
  //start script
  BlinkM_playScript( blinkm_addr, 0,0,0 );
}//start script fx
//------------------------------------------------------------------
//intterupt function to handle the limit switch
void USDS_Funct(){

  //switch debouce variables, 200ms is the current timing  
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();

  if (interrupt_time - last_interrupt_time > 200)
  {
    USDS_State = !USDS_State; //elegant way to flip the state (multiply by -1) 
  }

  last_interrupt_time = interrupt_time;


}//USDS_Funct
//------------------------------------------------------------------
void lcdStartup(){}

okay, if you've gotten this far, thanks for reading my code and trying to sort out the issue.

i noticed a few things, while reading this in the post...

Anything that is commented can be removed from the program. i had an idea that there was a memory issue surrounding the functions, that's why you'll see some function variables commented out, and moved up to global variables. i could be on the wrong track, it was just a guess... other odd things are just notes to myseld around the area that i needed them.

the BlinkM i2c library... just shortcuts a lot of the Wire library, would it be better to streamline this and only use the Wire library?

again, any help would be way appreciated...

cheers...

 pinMode(FivePosA_1, INPUT);
  pinMode(FivePosA_2, INPUT);  
  pinMode(FivePosA_3, INPUT);
  pinMode(FivePosA_4, INPUT);  
  pinMode(FivePosA_5, INPUT);  

  //turning on pull-ups so no external components
  digitalWrite(FivePosA_1,HIGH);
  digitalWrite(FivePosA_2,HIGH);
  digitalWrite(FivePosA_3,HIGH);
  digitalWrite(FivePosA_4,HIGH);
  digitalWrite(FivePosA_5,HIGH);

(there's got to be a better way to do this...)

Amen.
Arrays and for loops.

Anything that is commented can be removed from the program.

It would have been polite for you to have done that before posting.

i have a working project, that is near the complete stage, but i am finding a random crash after leaving it run for an hour or so. what is the best way to track this down???

  • add a function that monitors free memory (google the forum / playground)
  • add logging of the progress of the applciation (mostly print statements or lighting LED's

Is there a crash

  • after a fixed time (timing overflow?)

  • after a fixed nr of user interactions (memory?)

  • Do a code review by hand and check every line with the question "how could I crash this line" or "if this went wrong what will be the effect? "

i have a few lcd.prints.... they only happen every 0.5seconds

Doesn't matter how often they are called. They are like furniture in a room. Used every day for hours at a time or never used, they still take up the same amount of room.

And, like furniture in a room, you can have too much. When you have too much furniture, you are always bumping into it. No big deal.

When you have too much memory being used, one bump and you crash.

that's why you'll see some function variables commented out, and moved up to global variables.

Global or static local variables take up the same amount of space all the time. Local variables that go out of scope only take up space while the function is running. Making more global variables is generally moving in the wrong direction.

the BlinkM i2c library... just shortcuts a lot of the Wire library, would it be better to streamline this and only use the Wire library?

The great thing about libraries is that only the code that is actually used is linked in. So, replicating the portions of the BlinkM i2c library that you use in your code would not reduce your code footprint at all.

spreading some "critical" serial prints in the code, could help you figure out more or less where the problem is... and then narrow it even further with a serial print between statements. That's one way of debugging it.

Or, you can attach a LED for each object operation, before writing to the LCD, turn on LED X, before writing to serial, turn on LED Y, and so on, and so on... You'd then know which object may be causing the crash.

With 1000 lines of code, I don't think anyone will have enough patience to track down possible problems in the code when it would probably be faster to do what I described above. Do note that you don't, and shouldn't make long serial print statements something like P1, P", p3, etc is enough. Once you narrow this down a bit further, let us know what you found.

PaulS:
The great thing about libraries is that only the code that is actually used is linked in. So, replicating the portions of the BlinkM i2c library that you use in your code would not reduce your code footprint at all.

the bad thing about them is that most have no idea what's inside and what instruction inside them can crash the processor. :\

@AWOL - thanks for at least looking...

re: pinMode / digitalWrite
10 lines of clearly readable code versus deciphering a for loop, i'll choose my method... 100lines, you have a point...

re: comments - if i would have removed them, i wouldn't have learned that function variables use less memory than global memory. plus it shows my process in solving the problem.

the "better way" comment referring to posting a long program.

@PaulS - thanks for the useful comments...
@Robtillaart - free memory tracking, thanks i'll look into that....

program progress is implemented right now, i just need to see if a pattern emerges from what is happening. currently testing which function crashes the program the quickest. fx4 is the winner right now, crashing it with only 15k times through the main loop. fx3 is currently the least likely, with 5mil times though the main loop. this is pointing me towards the i2c stuff??

thanks
_J

10 lines of clearly readable code versus deciphering a for loop, i'll choose my method

Until you decide to change the seventh one...all the way through your sketch.
Good luck.
sp "deciphering a single array declaration"

 B_1 = digitalRead(FivePosB_1);
    B_2 = digitalRead(FivePosB_2);
    B_3 = 1; //must be hardvalued for logic to work
//    B_3 = digitalRead(FivePosB_3); //not connected
    B_4 = digitalRead(FivePosB_4);
    B_5 = digitalRead(FivePosB_5);

    //call function 1 based on switch position    
    if (B_1 == 0 && B_2 == 1 && B_3 == 1 && B_4 == 1 && B_5 == 1){
      ledfunction1();      
    }
    //call function 2 based on switch position
    if (B_1 == 1 && B_2 == 0 && B_3 == 1 && B_4 == 1 && B_5 == 1){
      ledfunction2();
    }
..............

Yuk!

Combine al the B_x bits into a single byte and have a simple case statement.


Rob