Calling function inside setup() fails / Strange behavior

Hi!

Again, I have a problems and its solution may be quit simple.
Are there any limitations / restrictions for calling a function from within the setup() funktion?
I have the following problem and I don’t understand why! :frowning:

  1. This code works
#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;
const int ledPin =  2; 

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  Serial.begin(9600);

  if (!SD.begin(chipSelect)) 
  {
    Serial.println("SD card error!!");
    return;
  }
  
  File dataFile = SD.open("settings.txt");
  if (dataFile) 
  {
    while (dataFile.available()) 
    {
      Serial.write(dataFile.read());
    }
    dataFile.close();
  }
  else 
  {
    Serial.println("error opening settings.txt");
  }
}
  1. This code produces a “Error opening settings.txt” message
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;
const int ledPin =  2; 

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  Serial.begin(9600);

  if (!SD.begin(chipSelect)) {
    Serial.println("SD card error!!");
    return;
  }
  
  readSDSettings();
}

void readSDSettings() {
  File dataFile = SD.open("settings.txt");
  if (dataFile) 
  {
    while (dataFile.available()) 
    {
      Serial.write(dataFile.read());
    }
    dataFile.close();
  }
  else 
  {
    Serial.println("error opening settings.txt");
  }
}

If you return from setup(), you immediately go on to start loop().

AWOL:
If you return from setup(), you immediately go on to start loop().

if indeed you have a loop() function

BulldogLowell:
if indeed you have a loop() function

Well, of course you'll have a. . . Oh.

Snippets-r-us?

I found the cause!

It seems that

#include <Wire.h>

together with

#include <SD.h>

is not working when calling a functions from within setup() that uses the SD library!! O.O
If I remove the #inlcude<Wire.h> line it works!

That’s strange!! But I need both. SD library and Wire!. :frowning:

Ok, please ignore this thread. It seems that I had a memory issue. O.O
It is a mystery to me how to write a Arduino program that does more than just blink some LED without running out of memory… O.O

Maybe I should replace my ATMEGA 328P with a STM32?! :confused:

It is a mystery to me how to write a Arduino program that does more than just blink some LED without running out of memory... O.O

No mystery at all, if the rest of your code wastes RAM like thisSerial.println("error opening settings.txt");

ikarisan:
Maybe I should replace my ATMEGA 328P with a STM32?! :confused:

No... Yeah, the have more ram but are way more complex as well. You just need to learn not to piss away memory :wink:

I enclosed all strings with the F() macro. That made it.

But it is hard not to "waste" memory. For my sketch I need the Wire library, the SD library and the DallasTemperature Library together with some variables holding the data parsed from the SD card.

Now, after reading the SD card my free memory is somewhat around 200 bytes. O.o
And most of the sketch is still in develop. :frowning:

First, shorten the message to a minimum. Now uses less memory.

// SDoe = SD card file open error

Serial.println("SDoe");

Or, try this. Now down to one byte.

/* Error codes
First nibble is type of error, second is specific
1 = SD errors
    1,0 = error opening SD card file "settings.txt"
    1,1 = some other error
2 = analogue problems
    2,0 = reading unexpected 0
    2,1 = reading unexpected 1023
    2,2 =  some other error  
3 = digital pin problems
    3,0 = reading unexpected HIGH
    3,1 = reading unexpected LOW
    3,2 = some other error  
4 = some other problem
*/

Serial.println( 0x10, HEX ); // See error list for details

Ok, after commenting out EVERY single Serial.print() I am having 351 bytes free, instead of 229 bytes.
And after replacing some

String wifiPassword;
[...]
wifiPassword = settingValue;

with

char* wifiPassword;
[...]
wifiPassword = (char*)malloc(settingValue.length()*sizeof(char));
wifiPassword = const_cast<char*>(settingValue.c_str());

I am at 456 bytes of free RAM.

EDIT: Replaced some more String()… 766 bytes :smiley:

For a guy that is used to write JAVA and/or C++ code it is really hard to use this native C style code just to save one or two byte of RAM. :o

ikarisan:
For a guy that is used to write JAVA and/or C++ code it is really hard to use this native C style code just to save one or two byte of RAM. :o

yes, micro-controllers have less RAM than PC’s

:wink:

Having more ram thus using more ram thus buying more ram is one big vicious circle. If every programmer would program as ram conservative as 20 years ago then it wouldn't have been necessary to have PC's with 8GB+ of ram. But then again, programming wouldn't be as easy as it is now where you can just store everything you like.