Go Down

Topic: Duemilanove hangs (Read 980 times) previous topic - next topic

PaulNoel

I am writing a simple progam to read in temperature and a hall effect sensor.  This isnt complicated at all.
It makes use of and LCD.  The problem I have is that it runs fine for an hour or 2 and then it needs to be reset because it hangs.  For now this isn't a serious problem becuase my use allows me to do this without a worry but this has to be a problem if I expand my use to other purposes I intend to use.  I would love some suggestions. :)
Code: [Select]
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
long secs = 0;
int hallPin = A1; // Select the #1 pin for the hall effect sensor
char bufd[16];
char bufe[16];
char *bd;
char  *be;
int hallValOld = 0;
int maxHallVal = 0;
int minHallVal = 1023;
void setup() {
// initialize the string string to nothing
 bufd[0] = '/0';
 bufe[0] = '/0';
 bd = bufd;
 be = bufe;
 // Tell the LCD to turn on.
 lcd.begin(16, 2);
}

void loop() {
 long modSecs = 0;
 double digTemp = 0;
 int hallVal = 0;
 int digOut  = 0;
 int dispTemp = 0;
 int cTemp = 0;
 int hallOut = 0;
 bufd[0] = '/0';
 bufe[0] = '/0';
 digTemp = analogRead(sensorPin);
 hallVal = analogRead(hallPin);
 digTemp = digTemp/5.2;
 dispTemp = digTemp -22;
 cTemp = ((digTemp - 54)/1.8);

 if (hallVal < minHallVal) {
   minHallVal = hallVal;
 }
 if (maxHallVal < hallVal) {
   maxHallVal = hallVal;
 }
 //sprintf(bd,"Min:%d, Max:%d\n",minHallVal,maxHallVal);
 //lcd.print(bd);
 lcd.setCursor(0,0);
 sprintf(bd,"TempF = %d   ",dispTemp);
 lcd.print(bd);
 lcd.setCursor(0,1);
 hallOut = hallVal;
 hallOut = hallOut - 515;
 sprintf(be,"Gauss = %d    ",hallOut);
 lcd.print(be);
}

Coding Badly

#1
Nov 21, 2010, 04:47 pm Last Edit: Nov 21, 2010, 04:48 pm by bcook Reason: 1

First, some general comments...

Code: [Select]
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
long secs = 0;
int hallPin = A1; // Select the #1 pin for the hall effect sensor
char bufd[16];
char bufe[16];
[glow]char *bd;  // Get rid of this.  You don't need it.[/glow]
[glow]char  *be;  // Get rid of this.  You don't need it.[/glow]
int hallValOld = 0;
int maxHallVal = 0;
int minHallVal = 1023;
void setup() {
// initialize the string string to nothing
 [glow]bufd[0] = '/0';  // Get rid of this.  You don't need it.  It's not correct; it should be '\0'.[/glow]
 [glow]bufe[0] = '/0';  // Get rid of this.  You don't need it.  It's not correct; it should be '\0'.[/glow]
 [glow]bd = bufd;  // Get rid of this.  You don't need it.  [/glow]
 [glow]be = bufe;  // Get rid of this.  You don't need it.  [/glow]
 // Tell the LCD to turn on.
 lcd.begin(16, 2);
}

void loop() {
 long modSecs = 0;
 double digTemp = 0;
 int hallVal = 0;
 int digOut  = 0;
 int dispTemp = 0;
 int cTemp = 0;
 int hallOut = 0;
 [glow]bufd[0] = '/0';  // Get rid of this.  You don't need it.  [/glow]
 [glow]bufe[0] = '/0';  // Get rid of this.  You don't need it.  [/glow]
 digTemp = analogRead(sensorPin);
 hallVal = analogRead(hallPin);
 digTemp = digTemp/5.2;
 dispTemp = digTemp -22;
 cTemp = ((digTemp - 54)/1.8);

 if (hallVal < minHallVal) {
   minHallVal = hallVal;
 }
 if (maxHallVal < hallVal) {
   maxHallVal = hallVal;
 }
 //sprintf(bd,"Min:%d, Max:%d\n",minHallVal,maxHallVal);
 //lcd.print(bd);
 lcd.setCursor(0,0);
 sprintf([glow]bufd[/glow],"TempF = %d   ",dispTemp);
 lcd.print([glow]bufd[/glow]);
 lcd.setCursor(0,1);
 hallOut = hallVal;
 hallOut = hallOut - 515;
 sprintf([glow]bufe[/glow],"Gauss = %d    ",hallOut);
 lcd.print([glow]bufe[/glow]);
}


Now to the lock-up...

Code: [Select]
char bufd[16];
char bufe[16];


You can safely store up to 15 characters in each buffer.  Let's look at what you are trying to store...

123456789.1235|
TempF = 0   |
Gauss = 0    |


Looks good so far.

123456789.1235|
TempF = -99   |
Gauss = -99    |


Uh oh.  Gauss doesn't fit.  Does your application ever get Gauss values below -9?  (even it if doesn't, it would be a good idea to both clamp hallOut to an absolute minimum value and make bufe larger)

Let's try another one...

123456789.1235|
TempF = 100   |
Gauss = 100    |


Uh oh.  Gauss doesn't fit.  Does your application ever get Gauss values above 100?  (even it if doesn't, it would be a good idea to both clamp hallOut to an absolute maximum value and make bufe larger)

Go Up