Problem facing in write the value in Micro SD Card using SD card module

Hi

i have tried to make engine monitoring dispaly. This Oled display will shows Engine rpm,engine working hours,Current date & time and Engine RPM and stored the value in SD Card.

I have facing the problem in writing the value (working hours) in micro SD card.I have taking two engine's rpm.

I have used,Arduino mega 2560,RTC Module,'Temp sensor,LM393 Optical coupling speed sensor,SD Card module.

i need to record the working hours of Engine1 in micro sd card only for a time, if the below condition is true,

Engine1 rpm>0,Engine2 rpm<0 & gear in nuetral

but after run the program sd card recorded nothing.My code is here:-

Please advice me, if i have done anything wrong.

Please advice me, if i have done anything wrong.

Yes, you did not post your code

but after run the program sd card recorded nothing.My code is here:-

https://mail.google.com/mail/u/0?ui=2&ik=bd2b52005b&attid=0.1&permmsgid=msg-a:r-6871289144633801549&view=att&disp=safe&realattid=f_kckdlemp0

UKHeliBob:
Yes, you did not post your code

Code is little bit lengthy so i have posted link.
thank you

If it is too long to post inline then it would have been better to attach it to a post rather than linking to an external site which many people will be reluctant to visit

UKHeliBob:
If it is too long to post inline then it would have been better to attach it to a post rather than linking to an external site which many people will be reluctant to visit

My code part-1

#include <EEPROM.h>
#include <Wire.h>
#include <Adafruit_GFX.h>////////////////oled
#include <Adafruit_SSD1306.h>////////////oled

#include <SPI.h>
#include <SD.h> //Load SD library
int chipSelect = 53; //chip select pin for the MicroSD Card Adapter
File sdcard_file;////////////SD CARD
File sdcard_file1;//////////SD CARD


#include <OneWire.h>/////////////////////Temp sensor
#include <DallasTemperature.h>///////////Temp sensor
#define ONE_WIRE_BUS A0//////////////////Temp sensor
OneWire oneWire(ONE_WIRE_BUS);//////////Temp sensor
DallasTemperature sensors(&oneWire);////Temp sensor
#define ONE_WIRE_BUS A0////////////////Temp sensor

#define OLED_Address 0x3C///////////////oled
Adafruit_SSD1306 oled(1);///////////////oled
//#include "Wire.h"
#define WIDTH     128//////////////////oled
#define HEIGHT     64/////////////////oled
#define NUM_PAGE    8/////////////////oled

#include "RTClib.h"///////////////////// rtc
RTC_DS3231 rtc;////////////////////////// rtc
#define deviceAddress 0b1101000   //0x68

#define NUMFLAKES 10/////////////////oled
#define XPOS 0///////////////////////oled
#define YPOS 1///////////////////////oled
#define DELTAY 2///////////////////oled
#define LOGO16_GLCD_HEIGHT 16///////oled
#define LOGO16_GLCD_WIDTH  16//////oled

static unsigned long ActualMillis;
static unsigned long LastPeriodMillis;
static unsigned long TimeDifferenceSinceLastPeriod;
unsigned long pulsesOfPeriod;
unsigned long the_real_RPM;
unsigned long pulsesOfPeriod1;
unsigned long the_real_RPM1 = 0;


volatile int RPM_pulses;//RPM_pulses is shared between ISR and main-code therefore defined as volative to prevent the variable from beeing optimized away by the compiler
volatile int RPM_pulses1;//RPM_pulses is shared between ISR and main-code therefore defined as volative to prevent the variable from beeing optimized away by the compiler
unsigned long RPM_Timer = 0;
const byte interruptPin1 = 18;
const byte interruptPin2 = 19;
const uint8_t Key = 3;
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
const uint8_t EngineCommand = 8;
const byte NueTral = 6;

float temp;
unsigned long time_now = 0;

int TempState = 0;
int TempState1 = 0;
int TempState2 = 0;
int TempState3 = 0;
int TempState4 = 0;

int RpmState1 = 0;


int EventState = 0;
int addr_secnd = 0;
int addr_Minute = 1;
int addr_hrs1 = 2;
int addr_hrs2 = 3;
int addr_hrs3 = 4;
int addr_hrs4 = 5;
int addr_hrs5 = 6;

int secnd = 0;
int Minute = 0;
int hrs1 = 0;
int hrs2 = 0;
int hrs3 = 0;
int hrs4 = 0;
int hrs5 = 0;
int value_secnd;
int value_Minute;
int value_hrs1;
int value_hrs2;
int value_hrs3;
int value_hrs4;
int value_hrs5;
int x6;
int x7;
int x8;

long lastTime = 1;
long seconds = EEPROM.read(addr_secnd);
long minutes = EEPROM.read(addr_Minute);
long hours = EEPROM.read(addr_hrs2);

void setSQW(uint8_t value)
{
  Wire.beginTransmission(deviceAddress); //device address and STSRT command are queued
  Wire.write(0x0E); //Control Register Address is queued
  Wire.write(0x00); //Data for Control Register is queued
  Wire.endTransmission(); //queued information are transferred under ACK; STOP
}
void isr_counting_pulses()
{
  RPM_pulses++;  // count up by 1
}
void isr_counting_pulses1()
{
  RPM_pulses1++;  // count up by 1
}
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod)
{
  unsigned long currentMillis  = millis();
  if ( currentMillis - expireTime >= TimePeriod )
  {
    expireTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  }
  else return false;            // not expired

}


void setup()
{
  oled.begin(SSD1306_SWITCHCAPVCC, OLED_Address);
  rtc.begin();
  sensors.begin();
  Serial.begin(9600);
  Wire.begin();
  DateTime now = rtc.now();
  if (! rtc.begin())
  {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (rtc.lostPower())
  {
    Serial.println("RTC lost power, lets set the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  pinMode(chipSelect, OUTPUT); // chip select pin must be set to OUTPUT mode
  if (!SD.begin(chipSelect)) { // Initialize SD card
    Serial.println("Could not initialize SD card."); // if return value is false, something went wrong.

    return;
  }

  else
  {
    Serial.println("initialization done.");

  }
  if (TempState3 < 1)
  {
    sdcard_file = SD.open("EN.txt", FILE_WRITE);
    if (sdcard_file   ) {
      sdcard_file.print("   Date  ");
      sdcard_file.print("     ");
      sdcard_file.print("   Time  ");
      sdcard_file.print("     ");
      sdcard_file.print("   Strt hmr   ");
      sdcard_file.print("     ");
      sdcard_file.print("Stp Hmr");
      sdcard_file.println("     ");
      sdcard_file.close(); // close the file

    }
    TempState3++;
  }
  if (TempState4 < 1)
  {
    sdcard_file1 = SD.open("mac run.txt", FILE_WRITE);
    if (sdcard_file1 ) {
      sdcard_file.print("   Date  ");
      sdcard_file.print("     ");
      sdcard_file.print("   Time  ");
      sdcard_file.print("     ");
      sdcard_file.print("   mcStrt hmr   ");
      sdcard_file.print("     ");
      sdcard_file.print("mcStp Hmr");
      sdcard_file.println("     ");
      sdcard_file.close(); // close the file

    }
    TempState4++;
  }

  pinMode (NueTral, INPUT);
  pinMode(Key, INPUT);
  pinMode(EngineCommand, INPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  pinMode(interruptPin1, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), HourMeter, FALLING);
  attachInterrupt(digitalPinToInterrupt(interruptPin1), isr_counting_pulses, RISING);
  attachInterrupt(digitalPinToInterrupt(interruptPin2), isr_counting_pulses1, RISING);
  setSQW(0x00);
  oled.clearDisplay();
  oled.display();
}

String time()
{
  String dataString = "";
  DateTime now = rtc.now();
  dataString += String(now.year(), DEC);
  dataString += String('.');
  dataString += String(now.month(), DEC);
  dataString += String('.');
  dataString += String(now.day(), DEC);

  dataString += String("       ");
  dataString += String(now.hour(), DEC);
  dataString += String(':');
  dataString += String(now.minute(), DEC);
  dataString += String(':');
  dataString += String(now.second(), DEC);
  return dataString;
}

My code Part-2

void loop() {


  if (  TimePeriodIsOver(RPM_Timer, 1000)  ) // 1000 means 1000 milliseconds = 1 second
  {
    //Serial.println(RPM_Timer);
    pulsesOfPeriod = RPM_pulses; // make copy of counted pulses
    pulsesOfPeriod1 = RPM_pulses1;
    RPM_pulses = 0;              // immediately set RPM_pulses to zero to not miss any pulses
    RPM_pulses1 = 0;
    ActualMillis = millis();
    Serial.println(pulsesOfPeriod);
    Serial.println(ActualMillis);
    Serial.println( LastPeriodMillis);
    TimeDifferenceSinceLastPeriod = ActualMillis - LastPeriodMillis; //calculate the real timedifference
    LastPeriodMillis = ActualMillis;
    Serial.println( TimeDifferenceSinceLastPeriod);
    the_real_RPM = pulsesOfPeriod * 3000 / TimeDifferenceSinceLastPeriod;
    the_real_RPM1 = pulsesOfPeriod1 * 3000 / TimeDifferenceSinceLastPeriod;
    Serial.print("RPM:");
    Serial.println(the_real_RPM);
    Serial.println(the_real_RPM1);
    //RPM_pulses = 0;


    //Serial.print("RPM:");
    //Serial.println(RPM_pulses * 60 / 20);/// 20 SLOTS PER REVOLUTION
    //Serial.println(RPM_pulses1 * 60 / 20);
    oled.setTextSize(1);
    oled.setTextColor(WHITE, BLACK);
    oled.setCursor(60, 14);
    oled.print("       ");
    oled.setCursor(60, 14);
    oled.print(the_real_RPM);
    oled.setCursor(90, 14);
    oled.print("    ");
    oled.setCursor(90, 14);
    oled.print(the_real_RPM1);
    oled.display();




  }

  if ( digitalRead(Key) == HIGH)//KEY OFF NO DISPALY
  {
    oled.clearDisplay();
    oled.display();
  }
  else ////////////////////////////KEY ON DISPLAY ON
  {
    oled.setTextSize(1);
    oled.setTextColor(BLACK, WHITE);
    oled.setCursor(60, 24);
    oled.print(" KEY ON ");
    oled.display();
  }
  display();



  if (digitalRead(EngineCommand) == HIGH)/////at the time of engine shutoff ,Current reading registerd into eeprom
  {
    if (EventState < 1)
    {
      EEPROM.update(addr_secnd, seconds);
      EEPROM.update(addr_Minute, minutes);
      EEPROM.update(addr_hrs2, hours);
      Serial.print(EEPROM.read(addr_hrs2));
      Serial.print(EEPROM.read(addr_Minute));
      Serial.println(EEPROM.read(addr_secnd));
      EventState++;
    }
  }
  x6 = (EEPROM.read(addr_hrs2)); //3
  x7 = (EEPROM.read(addr_Minute)); //2
  x8 = (EEPROM.read(addr_secnd)); //1

  String val = String(x6) + String(x7) + String(x8);// + String(x6) + String(".") + String(x7);
  Serial.println(val);
  float value1 = (val.toFloat());
  Serial.println(value1);
  if (the_real_RPM1 > 0 && digitalRead(NueTral) == HIGH && the_real_RPM < 0)
  {
    while (TempState2 = 0)
    {

      sdcard1();

    }
  }


}

void HourMeter() {
  if (digitalRead(EngineCommand) == LOW)////Engine ON-- Hour counting started.
  {

    EventState = 0;

    if (lastTime > 0)
    {
      seconds++;

    }
    if (seconds > 59)
    {

      minutes++;
      seconds = 0;

    }
    if (minutes > 59)
    {

      hours++;
      minutes = 0;
    }

    state = !state;
  }

}



void display()////oled will display the coolant temperature ,date&time and hour counting.
{
  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
  //oled.clearDisplay();
  oled.setTextSize(1);
  oled.setTextColor(WHITE, BLACK);
  oled.setCursor(0, 0);
  oled.print(time());
  oled.drawLine(0, 10, 127, 10, WHITE);
  oled.drawLine(0, 11, 127, 11, WHITE);

  oled.setCursor(0, 14);
  oled.print((hours));//,":",(minutes));
  oled.print(":");
  oled.print((minutes));//,":",(minutes));
  oled.print(":");
  oled.print((seconds));//,":",(minutes));
  oled.setCursor(0, 24);
  oled.print((temp));

  oled.display();

}
void sdcard1()
{

  x6 = (EEPROM.read(addr_hrs2)); //3
  x7 = (EEPROM.read(addr_Minute)); //2
  x8 = (EEPROM.read(addr_secnd)); //1

  String val = String(x6) + String(x7) + String(x8);// + String(x6) + String(".") + String(x7);
  Serial.println(val);
  float value1 = (val.toFloat());
  Serial.println(value1);
  sdcard_file = SD.open("EN.txt", FILE_WRITE);
  if (sdcard_file)
  {
    sdcard_file.print(time());
    sdcard_file.print("   1  ");
    
    sdcard_file.print(value1);
    sdcard_file.print("     ");
    sdcard_file.print("      ");
    sdcard_file.println("     ");
    sdcard_file.close(); // close the file

  }
  else
  {
    Serial.println("error opening test1.txt");
  }

  RpmState1 = 1;
  TempState2++;
}

Please advise me ,if i have done mistakes on my code

== instead of = for comparison

if (the_real_RPM1 > 0 && digitalRead(NueTral) == HIGH && the_real_RPM < 0)
  {
    //while (TempState2 = 0)
       while (TempState2 == 0)
       //if(TempState2 == 0)
    {

      sdcard1();

    }
  }

probably better to write with if instead of while

cattledog:
== instead of = for comparison

if (the_real_RPM1 > 0 && digitalRead(NueTral) == HIGH && the_real_RPM < 0)

{
   //while (TempState2 = 0)
      while (TempState2 == 0)
      //if(TempState2 == 0)
   {

sdcard1();

}
 }




probably better to write with if instead of while

I will check and let you know the result.

thank you,

If the code is too long...

Break it down to a tiny RUNNABLE program that exhibits the problem. IE a small simple program that has the problem. Then you will be able to post it and people will be able to help easier.

-jim lee

cattledog:
== instead of = for comparison

if (the_real_RPM1 > 0 && digitalRead(NueTral) == HIGH && the_real_RPM < 0)

{
   //while (TempState2 = 0)
      while (TempState2 == 0)
      //if(TempState2 == 0)
   {

sdcard1();

}
 }




probably better to write with if instead of while

I have use if() inplace of while().Now its recorded the w.hours of engine.But my main purpose did not happen

i need,Whenever the Engine1 is switched ON and OFF working hours should be written in SD Card.While i run the program it will record the w.Hours only one time (ENG ON and ENG.OFF).That is, if switch off and ON the engine twice, it will record W.hours only one time.

Here is my part of code:

 if (digitalRead(EngineCommand) == HIGH && the_real_RPM1<=0  &&  the_real_RPM <= 0)///Engine off both the engine RPM is zero
  {
    
   if(RpmState1==1)
    
    {

      sdcard3();//Engine stop w.hrs writting to sd card 
 
    }
 
  }
  
  
  if (digitalRead(NueTral) == LOW && the_real_RPM1 > 0  &&  the_real_RPM <= 0)///Machine Nuetral ,Engine1 rpm greater than zero means ENGINE1=ON
  {
   
  
   
   if(TempState2 == 0)
   
    {

      sdcard1();//Engine start w.hrs writting to sd card

    }
  }

void sdcard1()
{

  x6 = (EEPROM.read(addr_hrs2)); //3
  x7 = (EEPROM.read(addr_Minute)); //2
  x8 = (EEPROM.read(addr_secnd)); //1

  String val = String(x6) + String(x7) + String(x8);// + String(x6) + String(".") + String(x7);
  Serial.println(val);
  float value1 = (val.toFloat());
  Serial.println(value1);
  sdcard_file = SD.open("EN.txt", FILE_WRITE);
  if (sdcard_file)
  {
    sdcard_file.print(time());
    sdcard_file.print("   1    ");
    
    sdcard_file.print(value1);
    sdcard_file.print("     ");
    sdcard_file.print("      ");
    sdcard_file.println("     ");
    sdcard_file.close(); // close the file

  }
  else
  {
    Serial.println("error opening test1.txt");
  }

  RpmState1 = 1;
  TempState2 = 1;

}
void sdcard3(){

  x6 = (EEPROM.read(addr_hrs2)); //3
  x7 = (EEPROM.read(addr_Minute)); //2
  x8 = (EEPROM.read(addr_secnd)); //1
  
  String val = String(x6) + String(x7) + String(x8);// + String(x6) + String(".") + String(x7);
  Serial.println(val);
  float value1 = (val.toFloat());
Serial.println(value1);
sdcard_file = SD.open("EN.txt", FILE_WRITE);
  if (sdcard_file)
  {
    sdcard_file.print(time());
   // sdcard_file.print("   1   ");
    
    //sdcard_file.print("     ");
    sdcard_file.print("                       ");
    sdcard_file.println(value1);
    //sdcard_file.println("     ");
    sdcard_file.close(); // close the file
    
  }
  else
  {
    Serial.println("error opening test2.txt");
  }

RpmState1 = 0;
 
}

Please advice me if i have done any mistakes.

Thank you

I have use if() inplace of while().Now its recorded the w.hours of engine.But my main purpose did not happen

i need,Whenever the Engine1 is switched ON and OFF working hours should be written in SD Card.While i run the program it will record the w.Hours only one time (ENG ON and ENG.OFF).That is, if switch off and ON the engine twice, it will record W.hours only one time.

please help me

Your conditional statements and the logic are very hard for me to follow but I would check that the control variables RpmState1 and TempState2 are being set correctly such that the sd card functions are actually being called after the first time.

I would add a Serial print of their values right before they are checked. For example

 Serial.print("RpmState1 =  ");
 Serial.println(RpmState1);
if(RpmState1==1)