Hello, i am working on a project to make an electricity energy meter, for which i have created a hardware (using arduino uno, CT, voltage sensor) and code which is working properly, so then i wanted to add a RTC module. The code for the RTC module also works independentally. But when i combined the code for energy meter & RTC module DS1302 , then, it shows compiling error. Any help would be appreciated. Thank you in advance.
[Code]
#include <LiquidCrystal.h>
#include <ThreeWire.h>
#include <RtcDS1302.h>
ThreeWire myWire(D4,D5,D2); // IO, SCLK, CE
RtcDS1302 Rtc(myWire);
#include <Wire.h>
#include <Adafruit_ADS1X15.h>
Adafruit_ADS1115 ads;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const float FACTOR = 20; //20A/1V from teh CT
const float multiplier = 0.00005;
double sensorValue1 = 0;
double sensorValue2 = 0;
int crosscount = 0;
int climb_flag = 0;
int val[100];
int max_v = 0;
double VmaxD = 0;
double VeffD = 0;
double Veff = 0;
unsigned long previousMillis = 0;
unsigned long interval = 100;
float energy = 0;
float energytemp = 0;
float wh = 0;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
ads.setGain(GAIN_FOUR); // +/- 1.024V 1bit = 0.5mV
ads.begin();
Serial.begin(57600);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
}
void printMeasure(String prefix, float value, String postfix)
{
Serial.print("Current: ");
Serial.print(prefix);
Serial.print(value, 3);
Serial.println(postfix);
// lcd.print(prefix);
/*
delay(10);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Current: ");
// lcd.print(prefix);
lcd.print(value, 3);
lcd.println(postfix);
delay(500);
*/
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
}
voltage_sensor();
delay(10);
float currentRMS = getcurrent();
printMeasure("Irms: ", currentRMS, "A");
delay(100);
// wh = energy/3600000 ;
energytemp = energy / 360000 ;
// wh = energytemp/13.845 ;
wh = energytemp/14.015 ;
delay(10);
lcd.clear();
lcd.setCursor(0,0);
// lcd.print(energy);
// delay(10);
// lcd.setCursor(0,1);
// lcd.print(" Watt");
// wh = energy/1000
// delay(10);
// lcd.setCursor(0,1);
lcd.print(wh);
lcd.print(" Unit");
// lcd.print("Current: ");
// lcd.print(prefix);
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
if (!now.IsValid())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
delay(10000); // ten seconds
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}
//hhhh
float getcurrent()
{
float voltage;
float current;
float sum = 0;
long time_check = millis();
int counter = 0;
while (millis() - time_check < 1000)
{
delay(200);
voltage = ads.readADC_Differential_0_1() * multiplier;
current = voltage * FACTOR;
//current /= 1000.0;
sum += sq(current);
counter = counter + 1;
}
current = sqrt(sum / counter);
current = current*1000;
// energy = energy + Veff * current / 3600;
energy = energy + Veff * current / 1000;
Serial.print("Energy: ");
Serial.println(energy);
return (current);
}
void voltage_sensor()
{
delay(10);
for ( int i = 0; i < 100; i++ ) {
sensorValue1 = analogRead(A0);
if (analogRead(A0) > 511) {
val[i] = sensorValue1;
}
else {
val[i] = 0;
}
delay(1);
}
max_v = 0;
for ( int i = 0; i < 100; i++ )
{
if ( val[i] > max_v )
{
max_v = val[i];
}
val[i] = 0;
}
if (max_v != 0) {
VmaxD = max_v;
VeffD = VmaxD / sqrt(2);
Veff = (((VeffD - 420.76) / -90.24) * -210.2) + 210.2;
}
else {
Veff = 0;
}
Serial.print("Voltage: ");
Serial.println(Veff);
VmaxD = 0;
delay(10);
}
/*
// CONNECTIONS:
// DS1302 CLK/SCLK --> D5
// DS1302 DAT/IO --> D4
// DS1302 RST/CE --> D2
// DS1302 VCC --> 3.3v - 5v
// DS1302 GND --> GND
#include <ThreeWire.h>
#include <RtcDS1302.h>
ThreeWire myWire(D4,D5,D2); // IO, SCLK, CE
RtcDS1302 Rtc(myWire);
void setup ()
{
Serial.begin(57600);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
}
void loop ()
{
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
if (!now.IsValid())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
delay(10000); // ten seconds
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}
*/
[Code ]