Loop stops after few iterations

hello everyone,
i have a project where i want to read displacements from a linear displacement sensor, and temperature and humidity from dht11 sensor and writing everything on SD card with datalogger while initiating the reading and logging by a button and stopping with the same button ( when i press the button the program starts, and when i press again it stops).

i had a problem with precision (it didn't go from 0 to 100 as i wanted but from values in between, so i added a calibration part in the setup void() (lines 113 to 136) and used a constrain() and map() functions in the loop ( lines 206 to 210), now the program works but stops after around 18 iterations without me pressing the button.

question is: what may caused this problem and how to fix that?

my full code is :

[code]
#include <ezButton.h>
#include <RTClib.h>
#include <DHT.h>
#include <DHT_U.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

#define DHTPIN 3  // Digital pin connected to the DHT sensor dht DHT11; //Sensor object named as DHT
#define DHTTYPE DHT11   // DHT 11


#define LOOP_STATE_STOPPED 0
#define LOOP_STATE_STARTED 1
ezButton button(2);  // create ezButton object that attach to pin 2;

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal_I2C lcd(0x27, 16, 2);

RTC_DS1307 RTC;

double timer;
double startTime;
double elapsedTime;

int loopState = LOOP_STATE_STOPPED; //button state
int samplingfreq; //sampling frequency [milliseconds]
int sensorValue = 0; //for the raw data
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
float distance = 0; //for the calibrated distance
float h; // Humidity
float t;//  Temperature as Celsius (the default)

const char* filename = "Disp.txt";

const int OUTPUT_PIN = 4;

File dataFile;
DateTime now;

// the setup routine runs once when you press reset:

void setup() {

  // initialize serial communication at 115200 bits per second:
  Serial.begin(115200);


  // some info to know what is on the Arduino
  Serial.println("KTR-100mm Displacement sensor, DHT11, LCD, Buttons.");
  Serial.print("\n"); //linebreak
  Serial.println("--------------------------------------------------");

  //-----------------Taking care of LCD-------------------
  //NOTE: if you cannot see the text on the LCD, try to change the potmeter on the back of it.
  //Sometimes you just have wrong contrast settings and nothing shows up on the screen because of it.
  lcd.begin(16, 2);                     // initialize the lcd
  lcd.backlight(); //initialize backlight
  //------------------------------------------------------
  lcd.clear(); //clear the LCD
  lcd.setCursor(0, 0); //Defining positon to write from first row,first column .
  lcd.print("KTR-100mm"); //some message
  lcd.setCursor(0, 1); //Cursor is moved to the 2nd line of the LCD
  lcd.print("Reading displ."); //You can write 16 Characters per line .
  delay(3000); //wait 3 sec

  //  seting pinMode for pin 10 (SD Chipselect)
  pinMode(10, OUTPUT);

  //  seting pinMode for pin 04 (Reset Pin)
  digitalWrite(OUTPUT_PIN, HIGH);
  pinMode(OUTPUT_PIN, OUTPUT);

  //----Set default------
  dht.begin();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) ) {
    Serial.println(F("Failed to read from DHT sensor!"));
    lcd.clear(); //clear the LCD
    lcd.setCursor(0, 0); //Defining positon to write from first row,first column .
    lcd.print("DHT failed"); //some message
    // Restart:
    delay(3000);
    digitalWrite(OUTPUT_PIN, LOW);
    return;
  }

  RTC.begin();
  //check or the Real Time Clock is on
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // uncomment it & upload to set the time, date and start run the RTC!
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

  // see if the card is present and can be initialized:
  if (!SD.begin(10)) {
    Serial.println("Error : Push the reset button");
    lcd.clear(); //clear the LCD
    lcd.setCursor(0, 0); //Defining positon to write from first row,first column .
    lcd.print("SD Card failed"); //some message
    // Restart:
    delay(3000);
    digitalWrite(OUTPUT_PIN, LOW);
    for (;;);
  }

  // calibrate during the first five seconds
  timer = millis();
  lcd.clear(); //clear the LCD
  lcd.setCursor(0, 0); //Defining positon to write from first row,first column .
  lcd.print("Calibration..."); //some message
  while ((millis() - timer) < 10000) {
    sensorValue = analogRead(A0);

    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }

    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }

  // signal the end of the calibration period
  lcd.clear(); //clear the LCD
  lcd.setCursor(0, 0); //Defining positon to write from first row,first column .
  lcd.print("Calibration Done"); //some message
  delay(500);

  lcd.clear(); //clear the LCD
  lcd.setCursor(0, 0); //Defining positon to write from first row,first column .
  lcd.print("Press Button to"); //some message
  lcd.setCursor(0, 1); //Defining positon to write from first row,first column .
  lcd.print("Start Reading!"); //some message
  //----------------Initializing SD Card---------------------
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.


  //write down the date (year / month / day)
  //prints only the start, so if the logger runs for sevenal days you only findt the start back at the begin.
  now = RTC.now();
  dataFile = SD.open(filename, FILE_WRITE);
  dataFile.print("Start logging on: ");
  dataFile.print(now.year(), DEC);
  dataFile.print('/');
  dataFile.print(now.month(), DEC);
  dataFile.print('/');
  dataFile.print(now.day(), DEC);
  dataFile.print(":");
  dataFile.print(now.hour(), DEC);
  dataFile.print(":");
  dataFile.print(now.minute(), DEC);
  dataFile.print(":");
  dataFile.print(now.second(), DEC);
  dataFile.println("\n"); //linebreak
  dataFile.print("Time [Sec]");
  dataFile.print(",");
  dataFile.print("Displacement [mm]");
  dataFile.print(",");
  dataFile.print("Temperature [C°]");
  dataFile.print(",");
  dataFile.print("Humidity [%]");

  dataFile.close();


  button.setDebounceTime(50); // set debounce time to 50 milliseconds

}


// the loop routine runs over and over again forever:

void loop() {


  button.loop(); // MUST call the loop() function first

  if (button.isPressed()) {
    if (loopState == LOOP_STATE_STOPPED)
      loopState = LOOP_STATE_STARTED;
    else // if(loopState == LOOP_STATE_STARTED)
      loopState = LOOP_STATE_STOPPED;
  }

  if (loopState == LOOP_STATE_STARTED) {

    startTime = millis(); //define the time NOW. This will be useful when we need some time readings

    //--------------------------Reading data and sending to PC-----------------------------------------

    elapsedTime = millis() - startTime; //start another timer

    // read the input on analog pin 0:
    sensorValue = analogRead(A0);

    // in case the sensor value is outside the range seen during calibration
    sensorValue = constrain(sensorValue, sensorMin, sensorMax);

    // apply the calibration to the sensor reading
    sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 1023);

    // Convert the analog reading (which goes from 0 - 1023) to a distance (0 - 100 mm):
    distance = sensorValue * (100.0 / 1023);
    // Read Humidity
    h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    t = dht.readTemperature();
    // Read      


    //---------------------------------Serial printout-------------------------------------------------

    Serial.print("\n"); //linebreak

    Serial.print("Displacement = "); //tab for separation
    Serial.print(distance, 4); //calibrated data (displacement), 4 decimals
    Serial.print(" mm");
    Serial.print("\n"); //linebreak

    Serial.print("Current humidity = ");
    Serial.print(h);
    Serial.print("%  ");
    Serial.print("\t"); //tab for separation
    Serial.print("temperature = ");
    Serial.print(t);
    Serial.print("C  ");
    Serial.print("\n"); //linebreak

    Serial.print("--------------------------------------------------------------------");

    //-----------------------------------------LCD Printout------------------------------------------

    lcd.clear(); //clear LCD
    lcd.setCursor(0, 0); //Defining positon to write from first row,first column .
    lcd.print("   ");
    lcd.print(distance, 4); // calibrated data (displacement), 4 decimals
    lcd.print(" mm");
    lcd.setCursor(0, 1); //Defining positon to write from second row,first column .
    lcd.print(t);
    lcd.print(" C  ");
    lcd.print(h);
    lcd.print(" %");

    //-------------------------Writing on SD Card---------------------------------------------------
    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.

    dataFile = SD.open(filename, FILE_WRITE);

    // if the file is available, write to it:
    // log the temperature and time.
    if (dataFile) {

      dataFile.print("\n"); //linebreak

      dataFile.print(elapsedTime);
      dataFile.print(",");

      dataFile.print(distance);
      dataFile.print(",");

      dataFile.print(t);
      dataFile.print(",");

      dataFile.print(h);

      dataFile.close();
      // print to the serial port too:
      Serial.print("\n"); //linebreak
      Serial.println("data stored");
    }
    // if the file isn't open, pop up an error:
    else {
      Serial.print("\n"); //linebreak
      Serial.println("error opening Disp.txt");
    }

    delay(50);

  }
}
[/code]

Thanks for the perfect posting of code and a good description of the issue.
No obvious reason found why the execution runs into trouble.
I suggest using Serial monitor and Serial.print in the code. That way You can find out where in the code the execution takes place. Printing like "position n", "at time ", millis() could bring us closer to the answer.

As professional I faced the same scenario more than once.... They all got cracked.

thank you sir, i'm a complete noob so help is appreciated.
where exactly in the code should i add the Serial.print to check for this?

I focused at this issue. Good or bad values, calibration, Is not the number one issue as I see it.

To make a suggestion, put one Serial.print before opening the SD file and one when the file is opened. Print the status of the SD.open, "datafile".

How is that button wired? I'm thinking about pullup, or pulldown, resistors.....

Which Arduino is this.
All millis() time must be declared unsigned long.
Leo..

This is my button



i have one end linked to GND and the other to Pin 2.

I will try the Serial.print and get back to you

it's an arduino uno, i will try and change it to unsigned long

Add in a serial print to prove the loop has stopped. Does it print more than 18 times, less than 18 times or exactly 18 times?

i have tried and not always 18, sometimes more sometimes less, i will post the result

Update :
I tried adding a count to the loop and turns out not exactly 18 times, everytime it loops for a certain number than stops.
one new problem seems to occur : the file is created and written on in the setup void(), but i get an error when it's supposed to open and write in the loop, what didn't happened before

KTR-100mm Displacement sensor, DHT11, LCD, Buttons.

--------------------------------------------------
1

Displacement = 0.0978 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
2

Displacement = 0.2933 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
3

Displacement = 0.1955 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
4

Displacement = 0.1955 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
5

Displacement = 0.2933 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
6

Displacement = 0.1955 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
7

Displacement = 0.0978 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
8

Displacement = 0.0978 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
9

Displacement = 0.0978 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
10

Displacement = 0.0978 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
11

Displacement = 0.0978 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
12

Displacement = 1.0753 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
13

Displacement = 11.4370 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
14

Displacement = 15.4448 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
15

Displacement = 22.0919 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
16

Displacement = 31.1828 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
17

Displacement = 35.5816 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
18

Displacement = 40.9580 mm
Current humidity = 51.00%  	temperature = 21.00C  
--------------------------------------------------------------------
error opening Disp.txt
19

Displacement = 46.3343 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
20

Displacement = 50.9286 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
21

Displacement = 56.8915 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
22

Displacement = 60.4106 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
23

Displacement = 62.9521 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
24

Displacement = 66.8622 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
25

Displacement = 69.8925 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
26

Displacement = 73.3138 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
27

Displacement = 76.1486 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
28

Displacement = 79.1789 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
29

Displacement = 82.5025 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
30

Displacement = 86.1193 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
31

Displacement = 89.1496 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
32

Displacement = 91.3979 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
33

Displacement = 93.9394 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
34

Displacement = 96.2854 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
35

Displacement = 98.5337 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
36

Displacement = 100.0000 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
37

Displacement = 99.8045 mm
Current humidity = nan%  	temperature = nanC  
--------------------------------------------------------------------
error opening Disp.txt
38

i posted the result in an update in comments

result in update on comment

After the write, when did your code close the file? If the file is to be opened all the time then the file needs to be closed so it can be opened, right?

there is a dataFile.close(); command both in the setup void() and in the loop void() after writing on the file

Oi I missed that. Have you considered using append instead of open?

#include "FS.h"
#include "SD.h"
#include "SPI.h"

void fSendToFile( void * parameter )
{
  if (!SD.begin())
  {
    log_i("Card Mount Failed");
  } else {
    xSemaphoreGive( sema_SD );
  }
  String toFile = "";
  toFile.reserve( toFileSize );
  for (;;)
  {
    if ( xQueueReceive(Q_toFIle, &toFile, portMAX_DELAY) == pdTRUE )
    {
      //log_i( "%s %d\n", toFile.c_str(), toFile.length() );
      xSemaphoreTake( sema_SD, portMAX_DELAY );
      appendFile(SD, "/data.txt", toFile.c_str() + '\n');
      xSemaphoreGive( sema_SD );
      toFile = "";
    }
    //log_i( " high watermark %d",  uxTaskGetStackHighWaterMark( NULL ) );
  }
  vTaskDelete( NULL );
}

void appendFile(fs::FS &fs, const char * path, const char * message) {
  //log_i("Appending to file: %s\n", path);
  File file = fs.open(path, FILE_APPEND);
  if (!file)
  {
    log_i("Failed to open file for appending");
    return;
  } else {
    file.print(message);
    file.close();
  }
}
////

Will try and see

I don't know that button lib. I suggest declaring pin 2 as INPUT_PULLUP. A floating input generates trouble.

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