How do I combine 2 codes to work as one code

I'm new to Arduino and I have a project where I control 2 motors depending on different temperatures, and I want to add a timer to the circuit by using the same Arduino Uno, but I have difficulty understanding how to combine the 2 codes into one.

//Code 1 (Temp)

// Declare all the pins 
int temp = A4;
int greenLed = 10;
int redLed = 11;
int fan1 = 12;
int fan2 = 13;

int thresholdValue = 0;
int celsius = 0;
int fahrenheit = 0;
int T1 = 50;
int T2 = 80;

// Functions for various work
void greenLightOn(){
  digitalWrite(greenLed, HIGH);
}
void greenLightOff(){
  digitalWrite(greenLed, LOW);
}
void redLightOn(){
  digitalWrite(redLed, HIGH);
}
void redLightOff(){
  digitalWrite(redLed, LOW);
}
void fan1On(){
  digitalWrite(fan1, HIGH);
}
void fan1Off(){
  digitalWrite(fan1, LOW);
}
void fan2On(){
  digitalWrite(fan2, HIGH);
}
void fan2Off(){
  digitalWrite(fan2, LOW);
}


void setup()
{
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(fan1, OUTPUT);
  pinMode(fan2, OUTPUT);
  pinMode(temp, INPUT);
  Serial.begin(9600);
}

void loop(){
  
  // Temperature calculation
  celsius = map(((analogRead(A4) - 20) * 3.04), 0, 1023, -40, 125);
  fahrenheit = ((celsius * 9) / 5 + 32);
  
  Serial.print(celsius);
  Serial.print(" C : ");
  Serial.print(fahrenheit);
  Serial.println(" F");
  
    
  if(celsius<T1){
    fan2On();
    fan1Off();
    redLightOn();
    greenLightOff();
  }
  else if(celsius>=T1 && celsius<=T2){
    fan2Off();
    fan1Off();
    greenLightOn();
    redLightOff();
  }
  else if(celsius>T2){
    fan1On();
    fan2Off();
    redLightOn();
    greenLightOff();
  }
  else{
    Serial.println("Temperature is Normal");
  }
  delay(1000);
}

//Code 2 (Timer+LCD)

#include <LiquidCrystal.h>
#include "Countimer.h"
Countimer tdown;
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
#include <EEPROM.h>

#define bt_set    A0
#define bt_up     A1
#define bt_down   A2
#define bt_start  A3

int time_s = 0;
int time_m = 0;
int time_h = 0;

int set = 0;
int flag1=0, flag2=0;

int relay = 8;
int buzzer = A5;

void setup() {
Serial.begin (9600);

pinMode(bt_set,   INPUT_PULLUP);
pinMode(bt_up,    INPUT_PULLUP);
pinMode(bt_down,  INPUT_PULLUP);
pinMode(bt_start, INPUT_PULLUP);

pinMode(relay, OUTPUT);
pinMode(buzzer, OUTPUT);

lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("   Welcome To   ");
lcd.setCursor(0,1);
lcd.print("Countdown  Timer");
tdown.setInterval(print_time, 999);
eeprom_read();
delay(1000);
lcd.clear();
}

void print_time(){
time_s = time_s-1;
if(time_s<0){time_s=59; time_m = time_m-1;}
if(time_m<0){time_m=59; time_h = time_h-1;}
}

void tdownComplete(){Serial.print("ok");}

//tdown.stop(); 

void loop(){
tdown.run();

if(digitalRead (bt_set) == 0){
if(flag1==0 && flag2==0){flag1=1;
set = set+1;
if(set>3){set=0;}
delay(100); 
}
}else{flag1=0;}

if(digitalRead (bt_up) == 0){
if(set==0){tdown.start(); flag2=1;}
if(set==1){time_s++;}
if(set==2){time_m++;}
if(set==3){time_h++;}
if(time_s>59){time_s=0;}
if(time_m>59){time_m=0;}
if(time_h>99){time_h=0;}
if(set>0){eeprom_write();}
delay(200); 
}

if(digitalRead (bt_down) == 0){
if(set==0){tdown.stop(); flag2=0;}
if(set==1){time_s--;}
if(set==2){time_m--;}
if(set==3){time_h--;}
if(time_s<0){time_s=59;}
if(time_m<0){time_m=59;}
if(time_h<0){time_h=99;}
if(set>0){eeprom_write();}
delay(200); 
}

if(digitalRead (bt_start) == 0){ flag2=1; 
  eeprom_read(); 
  digitalWrite(relay, HIGH); 
  tdown.restart(); 
  tdown.start();
}

lcd.setCursor(0,0);
if(set==0){lcd.print("      Timer     ");}
if(set==1){lcd.print("  Set Timer SS  ");}
if(set==2){lcd.print("  Set Timer MM  ");}
if(set==3){lcd.print("  Set Timer HH  ");}

lcd.setCursor(4,1);
if(time_h<=9){lcd.print("0");}
lcd.print(time_h);
lcd.print(":");
if(time_m<=9){lcd.print("0");}
lcd.print(time_m);
lcd.print(":");
if(time_s<=9){lcd.print("0");}
lcd.print(time_s);
lcd.print("   ");

if(time_s==0 && time_m==0 && time_h==0 && flag2==1){flag2=0;
tdown.stop(); 
digitalWrite(relay, LOW);
digitalWrite(buzzer, HIGH);
delay(300);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(300);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(300);
digitalWrite(buzzer, LOW);
}

if(flag2==1){digitalWrite(relay, HIGH);}
else{digitalWrite(relay, LOW);}

delay(1);
}

void eeprom_write(){
EEPROM.write(1, time_s);  
EEPROM.write(2, time_m);  
EEPROM.write(3, time_h);  
}

void eeprom_read(){
time_s =  EEPROM.read(1);
time_m =  EEPROM.read(2);
time_h =  EEPROM.read(3);
}

Hello vaaltuin
Post your current sketch, well formated, with comments and in so called code tags "</>" and a schematic, not a Fritzy diagram, to see how we can help.
Have a nice day and enjoy coding in C++.
Дайте миру шанс

1 Like

What is the timer supposed to do among the temperature measurements?

It's a super common problem.

Google

Combine Arduino programs

Or

Combine Arduino sketches

you get the idea. The basic process will be found with a bit of finger work.

It will be best if the sketches are written well to begin with. Coding can be done in anticipation of it needing to serve elsewhere or in combination with other programs. Sadly this is not often the case.

It will go better the more you understand each on its own. Sadly this too is not often the case, dunno how much you know about the two parts you seek to marry, but you will know much more about them if you succeed!

If you just take two random sketches and have no clue about how to write an Arduino program, it will be an educational experience.

But it is done alla time, check your internets.

a7

1 Like

Hello vaaltuin
Try this example as frame work for your project.
Multiple_Tabs_Task1_Task2_Task3-220518a.zip (1.8 KB)
Have a nice day and enjoy coding in C++.
Дайте миру шанс!

What kind of cruel hoax is this?

It may work for some two or more well-designed sketches, but then they wouldn't be that hard to combine without your wonderful method.

If any task misbehaves (blocks or whatever) this is worse than useless.

In any case, it is still necessary to verify that no resources are used in conflict.

a7

Hello alto777
Thank you for your critical comments on my proposal.
Can you offer a better solution?
Have a nice day and enjoy coding in C++.
Дайте миру шанс!

I have a demo about just that, the comments are being finished.
See post 46. The comments show code with delay, the code itself shows replacing the delay with a timer.

Run the example and watch serial monitor. The lines that are just a big number, that's how many times void loop() ran all the sketches in one second. I get over 45000.
That's over 45 runs through loop per millisecond for average response speed.

My take on this is here
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

The best route is to understand what the coding does , then combining or using as for your own sketch will be easier .
If you combine two sketches without understanding them , then if ( when) there is a problem you won’t be able to fix it or learn much from the exercise

Having taken a low level flight about @vaaltuin's two sketches, it seems that if s/he were the author, in any sense of the word, there would be no need to ask for any help to combine the functions of the two into one sketch.

Therefor I assume that not to be the case, and further conclude that we are not dealing with an experienced coder. No shame in any of that; it's partly why we are here to help.

I don't know how many ppl downloaded your Multiple_Tabs_Task1_Task2_Task3-220518a.ino and got it running. I did so thinking that you may have brought your usual flair for brilliant C++ coding to bear on the matter, and were presenting us finally with an easy way to accomplish the goal of getting multiple sketches to function together in a simple way.

So I was disappointed to find a fairly simple means for doling out timed slices to multiple loop()s, made a bit obscure to an older fashioned plain C programmer, but nevertheless a nice show of how it might be done with some modern enhancements. Not gonna go look at it again to see if you used any actual C++. I can only read C++, and I always try to learn something when I do.

This will be way over the head of @vaaltuin, who will benefit more from doing it the hard way, even as your way would not really have made it simple for her. All the other advices have pointed out the hard facts, and several tutorial pages have been linked. One hopes that all that will be enough to encourage the OP to dig in and just do it.

And as I may have said, your solution only works with sketches that are designed well in the first place. Meaning the same designed might already have her own ideas about how to do, conceptually at least, exactly what your demonstration reveals.

As for my better solution, yes, yes, I have a very exciting and involved solution, but it requires a much taller ladder to have been climbed by one who might wish to employ it, and even my method still requires a careful and thorough knowledge of the constituent sketches.

And my method uses ideas and techniques that would raise a fire-storm of objections from "real" programmers, valild all I will cheerfully admit.

Sry I know my tone my have been offensive, I was only truly disappointed as I have said, and the various frustrations one experiences hanging out around here surfaced and escaped onto the internets before I could count to ten, after which I might have just shaken my head and remained silent on this matter on this thread.

Have a nice day and thanks you for showing off C++ as you do from time to time. :wink:

a7

Do I see a pair of crutches there? A time slicer with all of the overhead required plus a little?

Yes, this is a framework to run several tasks in parallel.

With non-blocking code, you don't need that.

Still adding comments. Just run the demo sketch.
It makes a log with times in millis. How tight does it run?

add
finished 2 sketches. 1st is with delays. 2nd is with timers and verbose comments.

TBH I was learning these things in the early 80's out of need at work in a fab shop. I'm a but used to them. I ran Micros not Frames.

With Arduino in 2014 I replaced delays with timers in a greenhouse automation in Budapest. It had serial, 2-wire, and GSM (13 delays start to send). With delays, it barely ran at all.

I found a way to take a thread of code containing delays and turning it into a state machine with a single timer in front as a function that behaves as an object.
The timer uses static variables (members), only runs when the state machine sets an interval to wait and clears the interval when the wait is over, the state machine runs again until it sets a wait.

It's a technique to un-delay, not an art. It's cut and dry.
The idea of writing functions that run over and over "doing the Next Thing" is the art.
Any long series of calculations especially if floats are involved can be broken into steps in a state machine so as to not hog cpu cycles.

I posted CombineSketchesDemo in Programming Questions. Maybe it can be moved.

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