Autostart

A Mega 2560 was equipped with an Adafruit barometer BMP180. I would like the sketch to start automatically on power-up to a battery so the data can be logged to an on-board SD card later. But... on power-up by a battery or a USB cable no pressures are measured, witnessing the absence of a blinking LED on the Mega (the one close to the USB connection). The sketch (which works properly but does not autostart at power-up) is:

#include <Wire.h>
#include <Adafruit_BMP085.h>

/*************************************************** 
  This is based on an example for the BMP085 Barometric Pressure & Temp Sensor by Adafruit
  ----> https://www.adafruit.com/products/391
*/

Adafruit_BMP085 bmp;
  
void setup() {
  Serial.begin(115200);
  if (!bmp.begin()) {
	Serial.println("Could not find a valid BMP085 sensor, check wiring!");
	while (1) {}
  }
}
  
void loop() {
  long Time=millis();
  
  /*
    Serial.print("T = ");
    Serial.print(bmp.readTemperature());
    Serial.print(" *C,\t");
  */
  
    Serial.print("B = ");
    Serial.print(bmp.readPressure());
    Serial.print(" Pa,\t  ");

    Serial.print(" dt=");Serial.print(millis()-Time);Serial.print(" mSec.");

    Serial.println();
    delay(65);//This delay makes it easy to see the LED blink    
}

Any suggestions? Thanks.

See if you can work out what is happening. Is the sketch running at all? (How would you know?) Has it hung or failed trying to access one of the devices? It may be that you need to enable the brownout detector so the processor doesn't run until the voltage is high enough, or delay the startup code long enough for the devices to finish initialising, or remove code that is waiting for the serial port to be opened - but rather than just trying stuff at random I suggest you narrow down what it's actually doing when the problem occurs. Even a simple LED diagnostic light can give you a valuable insight into what's happening.

Peter,
Thanks for the suggestions. I took them as home work and I got results.

In addition to a barometer, the final project should have bluetooth and a GPS unit attached that require start up time. Because of that I may not have seen the last of that problem. I have taken your response, numbered the suggestions and inserted my actions, results (or the lack of it) into the text as appropriate:

  1. Is the sketch running at all? (How would you know?)
    The only way I could think of is to insert “Blink” code. Are there better or more effective ways?

  2. Has it hung or failed trying to access one of the devices?
    Other than inserting “Blink” code, I do not know how to determine if a device is hung. What is a good way to determine if code is hung?

  3. It may be that you need to enable the brownout detector so the processor doesn't run until the voltage is high enough. I ran the sketch listed in the forum http://gammon.com.au/forum/?id=11497 in the section “Detecting low voltage”.

void setup(void)
  {
  Serial.begin(115200);
  }
    
void loop(void)
  {
  Serial.println (getBandgap ());
  delay(1000);
  }

const long InternalReferenceVoltage = 1062;  // Adjust this value to your board's specific internal BG voltage
 
// Code courtesy of "Coding Badly" and "Retrolefty" from the Arduino forum
// results are Vcc * 100
// So for example, 5V would be 500.
int getBandgap () 
  {
  // REFS0 : Selects AVcc external reference
  // MUX3 MUX2 MUX1 : Selects 1.1V (VBG)  
   ADMUX = _BV (REFS0) | _BV (MUX3) | _BV (MUX2) | _BV (MUX1);
   ADCSRA |= _BV( ADSC );  // start conversion
   while (ADCSRA & _BV (ADSC))
     { }  // wait for conversion to complete
   int results = (((InternalReferenceVoltage * 1024) / ADC) + 5) / 10; 
   return results;
  } // end of getBandgap

Although the sketch compiles and runs on the Mega, no credible answers result, such as 0, -22323, 21750 etc. The sketch is obviously not intended for the Mega. But it seems to me the best way to see if power levels are OK. Would you know of similar code suitable for the Mega?

  1. or delay the startup code long enough for the devices to finish initialising,
    I introduced a delay in the setup routine:
void setup() {
  delay(3000);
  Serial.begin(115200);
  if (!bmp.begin()) {
	Serial.println("Could not find a valid BMP085 sensor, check wiring!");
	while (1) {}
  }
}

This effort failed, it did not cause the Mega to start after battery power was connected.

  1. or remove code that is waiting for the serial port to be opened -
    I took out all the statements that relate to the serial port, I inserted on-board LED blinks in those spots where appropriate and tried again.
#include <Wire.h>
#include <Adafruit_BMP085.h>

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

Adafruit_BMP085 bmp;
  
void setup() {
  if (!bmp.begin()) {
	//Serial.println("Could not find a valid BMP085 sensor, check wiring!");
	while (1) {}
  }
    // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
}
  
void loop() {
  float x=bmp.readTemperature();
  x=bmp.readPressure();
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(100);               // wait for a second
    
}

It now starts up also on battery power, witness the blinking LED.
The Serial.* was the apparent problem.
Thanks for the suggestions!

  1. I suggest you narrow down what it's actually doing when the problem occurs. Even a simple LED diagnostic light can give you a valuable insight into what's happening.

Do you mean probing with an LED probe?

Again, THANKS for the suggestions!

Sounds like you have got to the bottom of it but yes, I was referring to just adding an LED to your project and blinking it in a recognisable pattern at significant points in your startup sequence.