How to merge/combine/add three sketches in one sketch? [ARDUINO]

How can I add up/ merge / combine three sketches. kindly help with the code below.[ARDUINO]
.
.
//Sketch1

#include “EmonLib.h” // Include Emon Library

#define VOLT_CAL 148.7

int offset = 84;
EnergyMonitor emon1; // Create an instance

void setup()
{

Serial.begin(9600);

emon1.voltage(1, VOLT_CAL, 1.7); // Voltage: input pin, calibration, phase_shift

}

void loop()
{
emon1.calcVI(20,100); // Calculate all. No.of half wavelengths (crossings), time-out

float supplyVoltage = emon1.Vrms + offset; //extract Vrms into Variable

Serial.print("Voltage: ");
Serial.println(supplyVoltage);

if (supplyVoltage > 203)
{

Serial.println(“Over Voltage detected”);
pinMode (9,OUTPUT);
digitalWrite(9 , LOW);
}
delay (600);
if (supplyVoltage < 200.3)
{

Serial.println(“Low Voltage detected”);
pinMode (9,OUTPUT);
digitalWrite(9 , LOW);
}
delay (600);
}

//Sketch 2

#define VIN A0
const float VCC = 5.0;

float cutOffLimit = 1.01;// set the current which below that value, doesn’t matter. Or set 0.5

float sensitivity = 0.066 ;

const float QOV = 0.5 * VCC; // quiescent Output voltage of 0.5V
float voltage;

void setup() {

pinMode (12,OUTPUT);
Serial.begin(9600);

Serial.println(" Over Current fault protection ");

}

void loop() {

float voltage_raw = (5.0 / 1023.0)* analogRead(VIN);// Read the voltage
voltage = voltage_raw - QOV + 0.012 ;//
float current = voltage / sensitivity +12.93;

abs (current);

Serial.print("I: ");
Serial.print(current,3); // 2 decimal places
Serial.println(“A”);

if (current>0.3)
{
Serial.println (“over Current fault detected”);
pinMode (12, OUTPUT);
digitalWrite(12 , LOW);

}
delay(500);
if (current<0.3)
{
Serial.println (“Normal current”);
pinMode (12, OUTPUT);
digitalWrite(12 , HIGH);

}

delay(500);
}

//Sketch 3

#define VIN A2
const float VCC = 5.0;

float cutOffLimit = 1.01;// set the current which below that value, doesn’t matter. Or set 0.5

float sensitivity = 0.066 ;

const float QOV = 0.5 * VCC; // quiescent Output voltage of 0.5V
float voltage;

void setup() {

pinMode (11,OUTPUT);
Serial.begin(9600);

Serial.println(" Leakage current fault protection ");

}

void loop() {

float voltage_raw = (5.0 / 1023.0)* analogRead(VIN);// Read the voltage
voltage = voltage_raw - QOV + 0.012 ;//
float current = voltage / sensitivity +12.93;

abs (current);

Serial.print("I: ");
Serial.print(current,3); // 2 decimal places
Serial.println(“A”);

if (current>0.3)
{
Serial.println (“Leakage Current detected”);
pinMode (11, OUTPUT);
digitalWrite(11 , LOW);

}
delay(500);
if (current<0.3)
{
Serial.println (“Normal”);
pinMode (11, OUTPUT);
digitalWrite(11 , HIGH);

}

delay(500);
}

Welcome to the Forum. Please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question …
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The “Code: [Select]” feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing or review.

If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower right corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Combine the usual way:
The stuff before setup() goes together,
the stuff in setup() goes, together,
the stuff in loop() goes together.

Any other function()s go outside of loop(), usually after.

Resolve and/or delete any duplications, such as common IO pin assignments, Serial.begin() being called 2 or 3 times, etc.

When done, you should have:
pre-setup() code, like #includes, byte pinName = 3; variable declarations
then
setup(){
// set up pinModes, etc
}
then
void loop(){
// whatever the programs are doing
}
then any other functions

CrossRoads:
Resolve and/or delete any duplications, such as common IO pin assignments, Serial.begin() being called 2 or 3 times, etc.

...and then resolve the chaos that usually results from delay() calls being added to code that can't tolerate any delays. :slight_smile:

This Simple Merge Demo may help.

Before trying to merge the programs go through each of them carefully to make sure they are not trying to use the same resource - for example an I/O pin. If you find conflicts change something in one of the separate programs and make sure it still works as a separate program with the change.

...R

CrossRoads:
Any other function()s go outside of loop(), usually after.

Having functions after the loop is usually working because the IDE creates function prototypes for you and thus the compiler does not bark when compiling, but it’s not a best practice.

In C++ things needs to be declared before being used, so in my opinion functions need to go before the setup() and loop() to take “good programing habits” or being properly declared with a .h and extra files.

Start by learning to combine just 2 sketches.... :confused:

The tracking in all three only looks at the value of interest once per second.

All three would benefit from having the long delays (500, eternity) moved into the if statement: delay only if you print, so a massive stream is suppressed, which I must guess was the purpose.

All three could print once and not again until a different condiction needs to be printed. Then you mightn't need the delay(500)s at all.

If you don't want to examine your values of interest at the full loop rate, probably 10s of thousands of times per second, then a single delay of, say, 1 millisecond at the top of the loop would be a simple if kruse way of accomplishing that. Or a smaller delay in microseconds.

These three look fairly easy to munge into one, the advices given so far applied to your sketches should proceed without too much fuss.

Even if you leave all the delays in place.

--> I just think you are compromising the utility if the system by (inadvertently?) leaving it blind 99.999999 percent of the time.

HTH

a7