Inconsistent behavior

I am new to using Arduino but spend hours and hours trying to get it to work. I often get my project working fine after which I make small improvements after which it fails to write to the SD card. Standard sketches for SD cards works without a problem. If I find that the Arduino Uno using a Data logging Shield V1.0 stops working, I try a Arduino Mega which sometimes work and sometimes it fails to work with the Data logging Shield V1.0 . What could be the problem?

We can't see your wiring or your code.

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

How to make a schematic that you can post.

edit: added link to schematic tutorial

Possibly your code, maybe other circuitry. Please post the code (using code tags) and a schematic / wiring diagram; the latter needs to have all wiring including power so we can see the power distribution as well.

I read 5 temperatures and two analog inputs to an Arduino Uno with the Data logging Shield V1.0 supplied by a 7 V supply. The data is sent to a SD card.

[code]
#include <RTClib.h>

/*

21-09-13 Nou W5.
W4 gebruik nou Strs en skryf net een keer as geiser
geskakel het
21-09-12: W4 Verkort program. W5 het eenvoudiger berekening van temperature
21-09-11: Verbeter MegaW7. Nou UnoW3
21-09-04: Dit werk nou reg en kan gebruik word met Mega.
21-09-01: Werk met Com 10 en onthou draadjies van SDA/SCL
21-08-29 Stel tyd reg by pomp aan/af, dit werk nou reg en ook vir pomp
21-08-23: Afgelaai by Arduino Data logging shield V1.0 - YouTube
*/
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"

String progNaam = "GeiserMonitorUnoW5";
//float minInterval = 10; // Stel aantal minute tussen lesings
float minInterval = .1;
int tempReg1 = 50, tempReg2 = -4;

const int chipSelect = 10; //cs or the save select pin from the sd shield is connected to 10.
String geiserNota, pompNota;

RTC_DS1307 RTC;

int VGo, VSo, VWo, VPo, VBo;
float geiserT, sonpaneelT, wandT, plafonT, buiteT;
int geiser2Aan, pomp2Aan; // status van geiser en pomp

const int geiserAan = 6; // geiser is gekoppel aan pen 9
const int pompAan = 7; // pomp is gekoppel aan pen 10
int pompAanStatus = 0; // current state of the button
int geiserAanStatus = 0; // current state of the button
int lastButtonStateGeiser = 0; // previous state of the button
int lastButtonStatePomp = 0; // previous state of the button
int i = 0;

File dataFile;
DateTime now; // Einde begin opstelling

void setup(void) {
Serial.begin(9600);
pinMode(pompAan, INPUT);
//------------------- setup clock ------------------
Wire.begin();
RTC.begin();
Serial.println(" ");
if (! RTC.isrunning()) { // check or the Real Time Clock is on
Serial.println("RTC is NOT running!");

}
//------------------- setup SD card ---------------
Serial.println("Initializing SD card...");

// see if the SD card is present and can be initialized:
if (!SD.begin(10, 11, 12, 13)) {
Serial.println("Card failed, or not present");
return; // don't do anything more:
}
Serial.println("card initialized.");
//----------------------
now = RTC.now(); // Skryf opskrif na skerm
Serial.println("---------------------------------------");
Serial.print("Begin meet op: ");
Serial.print(now.year(), DEC);Serial.print('-');
Serial.print(now.month(), DEC);Serial.print('-');
Serial.println(now.day(), DEC);
Serial.print("Progam naam = "); Serial.println(progNaam);
Serial.print("Lees temperature elke "); Serial.print(minInterval); Serial.println(" minute.");
Serial.println(" --------- Temperature -------");
Serial.println("Tyd Gei Son Wand Plaf Buite Gaan Paan");
Serial.println("-------------------------------------------------");
// Skryf opskrif na SD kaart
dataFile = SD.open("datalog.csv", FILE_WRITE);
dataFile.print("Begin meet op: ");
dataFile.print(now.year(), DEC);dataFile.print('-');
dataFile.print(now.month(), DEC);dataFile.print('-');
dataFile.print(now.day(), DEC);dataFile.println(" ");
dataFile.print("Progam naam = "); dataFile.println(progNaam);
dataFile.print("Lees temperature elke "); dataFile.print(minInterval); dataFile.println(" minute.");
dataFile.print(" --------------Temperature--------------");
dataFile.println("Tyd Gei Son Wand Plaf Buite Gaan Paan");
dataFile.close();

} // -------------- Einde void setup

void loop(void) { // -----------Begin herhaling

// ---------------- geiser temperatuur bereken
// VGo = analogRead(A2); // geiser temperatuur
VGo = analogRead(A2); // geiser temperatuur
geiserT = 0.115*VGo -53 + tempReg1;
// geiserT = 24;

VSo = analogRead(A3); // sonpaneel temperatuur
sonpaneelT = 0.115*VSo -53 + tempReg1;
// sonpaneelT = 14;

VWo = analogRead(A0); // wand temperatuur
wandT = 0.115*VWo -53 + tempReg2;
// wandT = 11;

VPo = analogRead(A6); // plafon temperatuur
// plafonT = 0.115*VPo -53+ tempReg2;
plafonT = 99;

VBo = analogRead(A1); // buite temperatuur
// buiteT = 22;
buiteT = 0.115*VBo -53 + tempReg2;

// ----------------- temperature klaar bereken

String tempStr, tempSdStr, timeStr;
now = RTC.now(); //read the time
timeStr = "";
timeStr = String(now.hour(), DEC) + ":";
timeStr += String(now.minute(), DEC) + ":";
timeStr += String(now.second(), DEC);

tempStr = timeStr + " "; // Om temperature na skerm te skryf
tempStr += String(geiserT,1) + " " + String(sonpaneelT,1) + " ";
tempStr += String(wandT,1) + " " + String(plafonT,1) + " ";
tempStr += String(buiteT,1) + " ";

tempSdStr = timeStr + ","; // Om temperature na SD kaart te skryf
tempSdStr += String(geiserT,1) + "," + String(sonpaneelT,1) + ",";
tempSdStr += String(wandT,1) + "," + String(plafonT,1) + ",";
tempSdStr += String(buiteT,1) + ",";
// Skryf temperature na skerm en SD kaart
Serial.print(tempStr);
Serial.println(String(geiser2Aan) + " " + String(pomp2Aan));

dataFile = SD.open("datalog.csv", FILE_WRITE);
// dataFile.print(tempSdStr);
dataFile.print("tempSdStr");
dataFile.print(tempStr);
dataFile.println(String(geiser2Aan) + "," + String(pomp2Aan));
dataFile.close();

//------------------------ begin herhaling2 ----------------

while (i < 60 * minInterval) { // Begin vertraging
delay(998); // minder as 1000 omdat progam tyd neem om te loop
now = RTC.now(); //read the time
timeStr = "";
timeStr = String(now.hour(), DEC);
timeStr += ":";
timeStr += String(now.minute(), DEC);
timeStr += ":";
timeStr += String(now.second(), DEC);

//  ------------------------------ Lees of Geiser Aan/Af geskakel het
// read the pushbutton input pin vir Geiser
geiserAanStatus = digitalRead(geiserAan);
// compare the buttonState to its previous state

if (geiserAanStatus != lastButtonStateGeiser) {
  // if the state has changed,
  if (geiserAanStatus  == HIGH) {     // geiser aan
    // if the current state is HIGH then the button went from off to on:
    geiser2Aan = 9;
    geiserNota = "    Geiser aan";
  } else {    // if the  state LOW then the button went from on to off:
    geiser2Aan = 0;
    geiserNota = "    Geiser af";
  }                      // Einde if (geiserAanStatus == LOW) {
  lastButtonStateGeiser = geiserAanStatus;     // hier is die probleem

  // Skryf temperature na skerm en SD kaart
  Serial.print(tempStr);
  Serial.print(String(geiser2Aan) + "    " + String(pomp2Aan));     
  Serial.println(geiserNota);
  dataFile = SD.open("datalog.csv", FILE_WRITE);    
  dataFile.print(tempSdStr);
  dataFile.print(String(geiser2Aan) + "," + String(pomp2Aan));
  dataFile.print(",");
  dataFile.println(geiserNota);
  dataFile.close();
}    // Einde  if (geiserAanStatus != lastButtonStateGeiser)

//-------------------------------------- Lees of pomp Aan/Af geskakel het

pompAanStatus = digitalRead(pompAan);
if (pompAanStatus != lastButtonStatePomp) {  // Toets of iets verander het
  if (pompAanStatus == HIGH) {
    // if the current state is HIGH then the button went from off to on:
    pomp2Aan = 5;
    pompNota = "    Pomp aan";
  } else {          // Pomp is af
    // if the current state is LOW then the button went from on to off:
    pomp2Aan = 0;
    pompNota = "    Pomp af"; 
  }       // Einde if the current state is LOW
  lastButtonStatePomp = pompAanStatus;
  // Skryf temperature na skerm en SD kaart
  Serial.print(tempStr);
  Serial.print(String(geiser2Aan) + "    " + String(pomp2Aan));      
  Serial.println(pompNota);
  dataFile = SD.open("datalog.csv", FILE_WRITE);
  dataFile.print(tempSdStr);
  dataFile.print(String(geiser2Aan) + "," + String(pomp2Aan));    
  dataFile.print(",");
  dataFile.println(pompNota);
  dataFile.close();
}          // Einde  if (pompAanStatus == HIGH) {

//------------------------------- Einde pomp aan
i++;                              // Einde van vertraging loop

}
i = 0;
dataFile.close();
//------------------------------------------ einde vertraging

} // Einde void loop(void)
[/code]type or paste code here

A good try with the code tags but no longer seems to work on the new forum. Use the </> button.

#include <RTClib.h>

/*
  
  21-09-13 Nou W5. 
  W4 gebruik nou Strs en skryf net een keer as geiser
  geskakel het
  21-09-12: W4 Verkort program. W5 het eenvoudiger berekening van temperature
  21-09-11: Verbeter MegaW7. Nou UnoW3
  21-09-04: Dit werk nou reg en kan gebruik word met Mega.
  21-09-01: Werk met Com 10 en onthou draadjies van SDA/SCL
  21-08-29 Stel tyd reg by pomp aan/af, dit werk nou reg en ook vir pomp
  21-08-23: Afgelaai by https://www.youtube.com/watch?v=nCQZWdL3BGE
*/
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"

String progNaam = "GeiserMonitorUnoW5";
//float minInterval = 10;         // Stel aantal minute tussen lesings
float minInterval = .1;
int tempReg1 = 50, tempReg2 = -4;

const int chipSelect = 10; //cs or the save select pin from the sd shield is connected to 10.
String geiserNota, pompNota;

RTC_DS1307 RTC;

int VGo, VSo, VWo, VPo, VBo;
float geiserT, sonpaneelT, wandT, plafonT, buiteT;
int geiser2Aan, pomp2Aan;   // status van geiser en pomp

const int  geiserAan = 6;   // geiser is gekoppel aan pen 9
const int  pompAan = 7;     // pomp is gekoppel aan pen 10
int pompAanStatus = 0;         // current state of the button
int geiserAanStatus = 0;         // current state of the button
int lastButtonStateGeiser = 0;     // previous state of the button
int lastButtonStatePomp = 0;     // previous state of the button
int i = 0;

File dataFile;
DateTime now;     // Einde begin opstelling

void setup(void) {
  Serial.begin(9600);
  pinMode(pompAan, INPUT);
  //------------------- setup clock ------------------
  Wire.begin();
  RTC.begin();
  Serial.println(" ");
  if (! RTC.isrunning()) {          // check or the Real Time Clock is on
    Serial.println("RTC is NOT running!");
    
  }
  //------------------- setup SD card ---------------
  Serial.println("Initializing SD card...");

  // see if the SD card is present and can be initialized:
  if (!SD.begin(10, 11, 12, 13)) {
    Serial.println("Card failed, or not present");
    return;     // don't do anything more:
  }
  Serial.println("card initialized.");
  //----------------------
  now = RTC.now();        // Skryf opskrif na skerm
  Serial.println("---------------------------------------");
  Serial.print("Begin meet op: ");
  Serial.print(now.year(), DEC);Serial.print('-');
  Serial.print(now.month(), DEC);Serial.print('-');
  Serial.println(now.day(), DEC);
  Serial.print("Progam naam = "); Serial.println(progNaam);
  Serial.print("Lees temperature elke "); Serial.print(minInterval); Serial.println(" minute.");
  Serial.println("          --------- Temperature -------");
  Serial.println("Tyd       Gei   Son   Wand  Plaf  Buite Gaan Paan");
  Serial.println("-------------------------------------------------");
  // Skryf opskrif na SD kaart
  dataFile = SD.open("datalog.csv", FILE_WRITE);
  dataFile.print("Begin meet op: ");
  dataFile.print(now.year(), DEC);dataFile.print('-');
  dataFile.print(now.month(), DEC);dataFile.print('-');
  dataFile.print(now.day(), DEC);dataFile.println(" ");
  dataFile.print("Progam naam = "); dataFile.println(progNaam);
  dataFile.print("Lees temperature elke "); dataFile.print(minInterval); dataFile.println(" minute.");
  dataFile.print("                --------------Temperature--------------");
  dataFile.println("Tyd       Gei   Son   Wand  Plaf  Buite Gaan Paan");
  dataFile.close();

}                           // -------------- Einde void setup

void loop(void) {           //  -----------Begin herhaling

  // ---------------- geiser temperatuur bereken
//  VGo = analogRead(A2);        // geiser temperatuur
    VGo = analogRead(A2);        // geiser temperatuur
  geiserT = 0.115*VGo -53 + tempReg1;
//  geiserT = 24;
  
  VSo = analogRead(A3);        // sonpaneel temperatuur
  sonpaneelT = 0.115*VSo -53 + tempReg1;
//  sonpaneelT = 14;
  
  VWo = analogRead(A0);        // wand temperatuur
  wandT = 0.115*VWo -53 + tempReg2;
//  wandT = 11;

  VPo = analogRead(A6);        // plafon temperatuur
//  plafonT = 0.115*VPo -53+ tempReg2;
  plafonT = 99;
  
  VBo = analogRead(A1);        // buite temperatuur
//  buiteT = 22;
  buiteT = 0.115*VBo -53 + tempReg2;

  // ----------------- temperature klaar bereken

  String tempStr, tempSdStr, timeStr;
  now = RTC.now();    //read the time
  timeStr = "";
  timeStr = String(now.hour(), DEC) + ":";
  timeStr += String(now.minute(), DEC) + ":";
  timeStr += String(now.second(), DEC);

  tempStr = timeStr + "  ";   // Om temperature na skerm te skryf
  tempStr += String(geiserT,1) + "  " + String(sonpaneelT,1) + "  ";
  tempStr += String(wandT,1) + "   " + String(plafonT,1) + "  ";
  tempStr += String(buiteT,1) + "   ";

  tempSdStr = timeStr + ",";   // Om temperature na SD kaart te skryf
  tempSdStr += String(geiserT,1) + "," + String(sonpaneelT,1) + ",";
  tempSdStr += String(wandT,1) + "," + String(plafonT,1) + ",";
  tempSdStr += String(buiteT,1) + ",";
  // Skryf temperature na skerm en SD kaart
  Serial.print(tempStr);
  Serial.println(String(geiser2Aan) + "    " + String(pomp2Aan)); 
 
  dataFile = SD.open("datalog.csv", FILE_WRITE);
//  dataFile.print(tempSdStr);
  dataFile.print("tempSdStr");
  dataFile.print(tempStr);
  dataFile.println(String(geiser2Aan) + "," + String(pomp2Aan)); 
  dataFile.close();

  //------------------------ begin herhaling2 ----------------

  while (i < 60 * minInterval) {       // Begin vertraging
    delay(998);                          // minder as 1000 omdat progam tyd neem om te loop
    now = RTC.now();    //read the time
    timeStr = "";
    timeStr = String(now.hour(), DEC);
    timeStr += ":";
    timeStr += String(now.minute(), DEC);
    timeStr += ":";
    timeStr += String(now.second(), DEC);

    //  ------------------------------ Lees of Geiser Aan/Af geskakel het
    // read the pushbutton input pin vir Geiser
    geiserAanStatus = digitalRead(geiserAan);
    // compare the buttonState to its previous state

    if (geiserAanStatus != lastButtonStateGeiser) {
      // if the state has changed,
      if (geiserAanStatus  == HIGH) {     // geiser aan
        // if the current state is HIGH then the button went from off to on:
        geiser2Aan = 9;
        geiserNota = "    Geiser aan";
      } else {    // if the  state LOW then the button went from on to off:
        geiser2Aan = 0;
        geiserNota = "    Geiser af";
      }                      // Einde if (geiserAanStatus == LOW) {
      lastButtonStateGeiser = geiserAanStatus;     // hier is die probleem

      // Skryf temperature na skerm en SD kaart
      Serial.print(tempStr);
      Serial.print(String(geiser2Aan) + "    " + String(pomp2Aan));     
      Serial.println(geiserNota);
      dataFile = SD.open("datalog.csv", FILE_WRITE);    
      dataFile.print(tempSdStr);
      dataFile.print(String(geiser2Aan) + "," + String(pomp2Aan));
      dataFile.print(",");
      dataFile.println(geiserNota);
      dataFile.close();
    }    // Einde  if (geiserAanStatus != lastButtonStateGeiser)

    //-------------------------------------- Lees of pomp Aan/Af geskakel het

    pompAanStatus = digitalRead(pompAan);
    if (pompAanStatus != lastButtonStatePomp) {  // Toets of iets verander het
      if (pompAanStatus == HIGH) {
        // if the current state is HIGH then the button went from off to on:
        pomp2Aan = 5;
        pompNota = "    Pomp aan";
      } else {          // Pomp is af
        // if the current state is LOW then the button went from on to off:
        pomp2Aan = 0;
        pompNota = "    Pomp af"; 
      }       // Einde if the current state is LOW
      lastButtonStatePomp = pompAanStatus;
      // Skryf temperature na skerm en SD kaart
      Serial.print(tempStr);
      Serial.print(String(geiser2Aan) + "    " + String(pomp2Aan));      
      Serial.println(pompNota);
      dataFile = SD.open("datalog.csv", FILE_WRITE);
      dataFile.print(tempSdStr);
      dataFile.print(String(geiser2Aan) + "," + String(pomp2Aan));    
      dataFile.print(",");
      dataFile.println(pompNota);
      dataFile.close();
    }          // Einde  if (pompAanStatus == HIGH) {

    //------------------------------- Einde pomp aan
    i++;                              // Einde van vertraging loop
  }
  i = 0;
  dataFile.close();
  //------------------------------------------ einde vertraging

}           // Einde void loop(void)type or paste code here
1 Like

Here is a page explaining why we do not use the String class in small memory processors like on the Uno, Nano, Mega unless we really know what we are doing.

You should go back and edit your original post to put the code properly in code tags, please.

It still works, but the two tags have to be on a line for their own.
So this text after the closing tag:

is the problem :wink:

That does not compile on my system. I'm not sure why you are using that form.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.