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.
I have facing the problem in engine rpm display/Serial print. When I run my program I read in my serial monitor Its showing the only last digit of rpm or erratic value.
I have used
1/Arduino mega 2560,
2/RTC Module
3/Temp sensor
4/LM393 Optical coupling speed sensor.
Only i have facing the problem in RPM reading.
Here is my code
#include <EEPROM.h>
#include <Wire.h>
#include <Adafruit_GFX.h>////////////////oled
#include <Adafruit_SSD1306.h>////////////oled
#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
int interval = 1000;
int counter = 0 ;
int counter1 = 0 ;
uint32_t frameStartAtMs ;
int Pre;
int Pre1;
const uint8_t Key = 3;
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
const uint8_t EngineCommand = 8;
float temp;
unsigned long time_now = 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;
const byte MOTOR1 = 18; // Motor 1 Interrupt Pin - INT 0
const byte MOTOR2 = 19;
int period = 300;
unsigned long previousMillis = 0;
unsigned long currentMillis;
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 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(MOTOR1, INPUT);
pinMode(MOTOR2, INPUT);
Pre = digitalRead(18);
Pre1 = digitalRead(19);
frameStartAtMs = millis() ;
pinMode(Key, INPUT);
pinMode(EngineCommand, INPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), HourMeter, FALLING);
setSQW(0x00);
}
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;
}
void loop() {
if ( millis() - frameStartAtMs > interval ) {////ENGINE RPM
Serial.println(counter * 60 / 20);
Serial.println(counter1 * 60 / 20);
frameStartAtMs = millis() ;
counter = 0;
counter1 = 0;
}
if (digitalRead(MOTOR1) != Pre) ///ENGINE1
{
counter++;
Pre = digitalRead(MOTOR1);
}
if (digitalRead(MOTOR2) != Pre1)////ENGINE2
{
counter1++;
Pre1 = digitalRead(MOTOR2);
}
if ( digitalRead(Key) == HIGH)//KEY OFF- DISPALY off
{
// oled1.clearDisplay();
//oled1.display();
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();
}
// digitalWrite(ledPin, state);
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++;
}
}
}
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();
}
Serial monitor data
0
3
0
0
0
3
3
3
3
3
3
3
6
6
I am not well expert in programming so could somebody please tell me where am I wrong?