Problem while combining/compiling code for 2 programs related to same project

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 ]

It would be extremely useful if you posted the full error message and your sketch. In case you haven't done so yet, please read How to get the best out of this forum to find out how to post code (and error messages).

Might be able to help but pretty hard when you haven't shared the code???

I apologise for the same since iam new to this forum and was figuring out how & where to post the code. I have edited my post, please check again for the same. Thankyou for quick reply.

I apologise for the same since iam new to this forum and was figuring out how & where to post the code. I have edited my post, please check again for the same. Thankyou for your quick reply

I apologise for the same since iam new to this forum and was figuring out how & where to post the code. I have edited my post, please check again for the same. Thankyou for a quick reply

Better but the formatting makes it hard to read. You need to use the </> in the editor to paste in your code.

Anyway... after a quick look, it seems you have just added the 2nd program to the end of the first... you can't do that. All your variables should be declared at the top, and you can only have one setup() and one loop() routine... you need to combine the 2 into 1.

Thankyou. Now i understand my mistake that you have mentioned. I will make changes accordingly and get back soon. Appreciate your quick reply.

Thankyou. Now i understand my mistake that you have mentioned. I will make changes accordingly and get back soon. Appreciate your quick reply.

If you have any variables that are declared in both programs with the same names, then one set will need to be changed so they are unique.

Same goes for any functions with the same name.

Also - check which GPIO pins each program is using to make sure there is no overlap of pins between the 2 programs.

Surely will check for these mistakes as well

The "ThreeWire" library is not available through Library Manager so where did you find it?

Same for "RtcDS1302".

I see you are using the names D4, D2, and D5 which are not defined for Arduino UNO. What board are you compiling for?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.