1

I stored "28328630" in sd card but at output i getting wrong value. on serial "5566" getting this value.

#include "HX711.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
int data = 0;
int curr_rate = 0;  // current flow rate in L/M

File dataFile;
HX711 scale(A1, A0);
LiquidCrystal_I2C lcd(0x3F, 16, 2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
const int buttonPin = 8;
int total;
int buttonState = 0;
void setup()
{
  lcd.init();
  lcd.init();
  lcd.backlight();

  lcd.setCursor(0, 0);
  lcd.print("KWALITY PVT.LTD.");
  Serial.begin(38400);

  while (!Serial)
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(4))
  {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    // return;
  }
  Serial.println("card initialized.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt"); //<<<<<<<<<<<<<< set the name

  // if the file is available, write to it:
  if (dataFile)
  {
    while (dataFile.available())
    {
      Serial.write(dataFile.read());      //<<<<<<<<<<<<< remove
      total = total + dataFile.parseInt();  //<<<<<<<<<<<<<<<< new
    }
    lcd.clear();
    dataFile.close();
    Serial.print("Total: ");
    lcd.setCursor(0, 1);
    lcd.print("Total:");
    Serial.println(total);
    lcd.setCursor(6, 1);
    lcd.print(total);
    Serial.print("Value in sd card 28328630....");
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening file");
  }

  scale.set_scale(2280.f);
  scale.tare();

}


void loop()
{
  buttonState = digitalRead(buttonPin);
  dataFile = SD.open("datalog.txt", FILE_WRITE);



  if (buttonState == HIGH) {

    if (scale.get_units() > 10 && scale.get_units() < 200)
    {

      //SET1
      lcd.setCursor(0, 0);
      lcd.print("LPM: ");
      lcd.setCursor(5, 0);
      data = (scale.get_units() / 10);
      lcd.print(data);
      scale.power_down();
      delay(5000);
      scale.power_up();

    //  scale.power_down();
      delay(60000);
     // scale.power_up();
      lcd.clear();


      if (dataFile) {
        Serial.print("Writing to datalog.txt...");
        dataFile.println(data);
        // close the file:
        dataFile.close();
        Serial.println("done.");
      } else {
        // if the file didn't open, print an error:
        Serial.println("error opening test.txt");
      }

      curr_rate = (data + total);
      Serial.print("Total: ");
      lcd.setCursor(0, 1);
      lcd.print("TOTAL:");
      lcd.setCursor(7, 1);
      lcd.print(curr_rate);

    }
    else
    {
      lcd.setCursor(0, 0);
      lcd.print("LPM: ");
      lcd.setCursor(5, 0);
      lcd.print("000   ");
      delay(500);
      lcd.print("        ");

    
      if (dataFile) {
        Serial.print("Writing to datalog.txt...");
        dataFile.println("0");
        // close the file:
        dataFile.close();
        Serial.println("done.");
      } else {
        // if the file didn't open, print an error:
        Serial.println("error opening test.txt");
      }



    }
  }
  else {
    lcd.setCursor(0, 0);
    lcd.print("     SENSOR    ");
    lcd.setCursor(0, 1);
    lcd.print("  DISCONNECTED  ");
    delay(2000);
    lcd.clear();

  }




}

6
  • 2 bytes type int can only hold numbers from -32,768 to 32,767. but you have more issues in you code
    – Juraj
    Aug 24 '19 at 8:30
  • forum.arduino.cc/index.php?topic=632947.0
    – Juraj
    Aug 24 '19 at 8:38
  • 1
    i replaced int with long int, on output i get
    – Ravinder
    Aug 24 '19 at 8:49
  • Total: 8328630 Value in sd card 28328630....
    – Ravinder
    Aug 24 '19 at 8:49
  • 2
    the Serial.write(dataFile.read()); stole the 2 and wrote it as invisible character
    – Juraj
    Aug 24 '19 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.