Strange crashing behaviour

Hello,

I am working on a project were I am reading pressure and temperature from a sensor (BMP085) and put out a pwm signal to dim a lamp based on my readings. I am doing the project on an Arduino Uno board.

The code is very simple and use two standard Arduino libraries, wire.h for I2C communication with the sensor and BMP085.h for sensor commands.

But for some reason the code decides to crash sometimes for no obvious reason.

Can someone please take a look on my code and help me find any mistakes I may have done?

#include <Wire.h>
#include "BMP085.h"
 
BMP085 bmp;

static int ledPin = 9;    // LED connected to digital pin 9
static int ledPinTemp = 10;    // LED connected to digital pin 10
static int brightness = 0;
static int brightnessPrevious = 0;
static int on=0;


 
void setup() {
//Serial.begin(9600);
  bmp.begin(); 
 pinMode(13, OUTPUT);   /*I am using a flashing LED to tell me if the processor is still running*/
}
 
void loop() {
  float currentPressure, currentTemperature, firstTemp, firstPressure;
  int index;
  
  
  
  firstTemp = bmp.readTemperature();
  firstPressure = bmp.readPressure();
  while(true)
  {
      
        currentPressure = bmp.readPressure();
        currentTemperature = bmp.readTemperature();
        
        if(on)
        {
          on=0;
          digitalWrite(13, LOW);
        }
        else
        {
          on=1;
          digitalWrite(13, HIGH);
        }
        /*
          y = k * x
        
          k = (y2 - y1)/(x2-x1)
        
          79.69 = (255 - 0)/(27.9 - 24.7)
          
          tmax = 27.9
          tmin = 24.7
        
        */
        
        brightness = (currentTemperature - firstTemp)*80;

        
        if(brightness > (brightnessPrevious +5) )
        {
          brightness = brightnessPrevious +5;
        }
        else if(brightness < (brightnessPrevious -5))
        {
          brightness = (brightnessPrevious -5);
        }
        
        if(brightness < 0)
        {
          
          brightness =0;
          firstTemp = bmp.readTemperature();
          
        }
        if(brightness > 255)
        {
          brightness =255;
        }
       
          analogWrite(ledPin, brightness);
          Serial.println(brightness);
        
        if(currentPressure < (firstPressure +50)  )
        {
            firstTemp = bmp.readTemperature();
            firstPressure = bmp.readPressure();
            
        }  
       brightnessPrevious = brightness;
        //Serial.println();
        delay(100);
  }
  
}

The first question that comes to mind is why are you running an infinite loop inside an infinite loop?

It appears that you do not know that loop() gets called over and over again.

If you're going to leave

          Serial.println(brightness);

in then perhaps you should un-comment

//Serial.begin(9600);

PaulS:
The first question that comes to mind is why are you running an infinite loop inside an infinite loop?

It appears that you do not know that loop() gets called over and over again.

This might actually be the thing. I actually don't know how the function loop() is handled by the "arduino core". In any normal processor I wouldn't think this would be an issue.
Unfortunately I am not able to test this until later this weekend.

GoForSmoke:
If you're going to leave

          Serial.println(brightness);

in then perhaps you should un-comment

//Serial.begin(9600);

Hm. I do not think this is a problem. Of course both or neither of the should be commented out. But that does not seem to affect the behaviour in any way.

In 1.0 Serial uses interrupts in write() too, not calling begin() but calling write could mess things up at the low level...
Btw, how do you know it "crashes" ?

Right in the code, he blinks the led on pin 13 first on then off using delay(100) per while(true).

We found another clue yesterday that is highly probable cause for the crashing. Apparently the library "wire.h" that is handling the I2C code is very sensitive to noise and may cause the crashing.

So hopefully I will solve the problem by adding some pull- up resistors.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1291403700/7