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:-
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;
}
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.
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;
}
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.
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