Merging two codes

  • #1
Mariam-baher
3
0
Thread moved from the technical forums to the schoolwork forums
TL;DR Summary: how can i merging two cods

hey, iam new at coding and i must to make a project to my school.
the project is
fire occurs
when the tempreture sensor feels the rise in tempreture opeans the fan
when the smoke senor indicate the smoke pump the water
i use these two videos
1-https://how2electronics.com/temperature-based-fan-speed-controller-using-arduino/
2-https://youtu.be/yYMQPZXn4Ic?si=6bVnfE3SmwGUZQ3N
how can i merging the two codes?
 
Physics news on Phys.org
  • #2
First, you need to decide what computer language you want to use. Python is very popular now for those types of programs. You need to make sure that the Arduino pins for the two tasks do not conflict. In your code you can make a function for each task. That will allow the variables in each task to be separate from the other task even if the variable name is the same. In your code, a top level program can loop at the rate that you want and call the two functions, one at a time, in each loop.
 
  • #3
FactChecker said:
First, you need to decide what computer language you want to use. Python is very popular now for those types of programs. You need to make sure that the Arduino pins for the two tasks do not conflict. In your code you can make a function for each task. That will allow the variables in each task to be separate from the other task even if the variable name is the same. In your code, a top level program can loop at the rate that you want and call the two functions, one at a time, in each loop.
i cannot understand you well, i will upload the in the Arduino IDE so i guess i will use the c language
 
  • #4
Welcome to PF. :smile:

Mariam-baher said:
TL;DR Summary: how can i merging two cods

1-https://how2electronics.com/temperature-based-fan-speed-controller-using-arduino/
2-https://youtu.be/yYMQPZXn4Ic?si=6bVnfE3SmwGUZQ3N
To help make your question more clear, can you post the individual code pieces from those two videos/links? That will help others who want to help you with your question.

When posting code at PF, please enclose the code in code tags. You can do that by doing this:

[ code ]
<<put the code in here>>
[ /code ]

(but leave out the spaces in the code tags to enable them.
 
  • #5
the first code uses Arduino uno and the second code uses Arduino Nano ( i have arduino uno).
the first code when the temperature sensor lm35 feels the rise in temperature from the fire the fan will open
the second code when the smoke sensor MQ2 indicted the smoke the water pumping action must occur


here is the first code:
C:
#include <LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7);
int tempPin = A0; // the output pin of LM35
int fan = 11; // the pin where fan is
int led = 8; // led pin
int temp;
int tempMin = 30; // the temperature to start the fan 0%
int tempMax = 60; // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;
 
void setup() {
pinMode(fan, OUTPUT);
pinMode(led, OUTPUT);
pinMode(tempPin, INPUT);
lcd.begin(16,2);
Serial.begin(9600);
}
 
void loop()
{
temp = readTemp(); // get the temperature
Serial.print( temp );
if(temp < tempMin) // if temp is lower than minimum temp
{
fanSpeed = 0; // fan is not spinning
analogWrite(fan, fanSpeed);
fanLCD=0;
digitalWrite(fan, LOW);
}
if((temp >= tempMin) && (temp <= tempMax)) // if temperature is higher than minimum temp
{
fanSpeed = temp;//map(temp, tempMin, tempMax, 0, 100); // the actual speed of fan//map(temp, tempMin, tempMax, 32, 255);
fanSpeed=1.5*fanSpeed;
fanLCD = map(temp, tempMin, tempMax, 0, 100); // speed of fan to display on LCD100
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
}
 
if(temp > tempMax) // if temp is higher than tempMax
{
digitalWrite(led, HIGH); // turn on led
}
else // else turn of led
{
digitalWrite(led, LOW);
}
 
lcd.print("TEMP: ");
lcd.print(temp); // display the temperature
lcd.print("C ");
lcd.setCursor(0,1); // move cursor to next line
lcd.print("FANS: ");
lcd.print(fanLCD); // display the fan speed
lcd.print("%");
delay(200);
lcd.clear();
}
 
int readTemp() { // get the temperature and convert it to celsius
temp = analogRead(tempPin);
return temp * 0.48828125;
}

and this link explain everything in details, I have the same materials that he has also i have made he same connections: https://how2electronics.com/temperature-based-fan-speed-controller-using-arduino/


the second code :
with Arduino nano (i have Arduino uno) and i have MQ2 gas sensor:

C:
/*Fire protection system with Arduino */

#define flame 2
#define relay 3
#define buzzer 4

void setup() {
  Serial.begin(9600);
  pinMode(flame, INPUT);
  pinMode(relay, OUTPUT);
  pinMode(buzzer, OUTPUT);

  digitalWrite(relay, HIGH);
}
void loop() {
  bool Svalue = digitalRead(flame);

  if (Svalue == 0) {
    digitalWrite(relay, LOW);
    digitalWrite(buzzer, HIGH);
    delay(300);
    digitalWrite(buzzer, LOW);
    delay(300);
    digitalWrite(buzzer, HIGH);
    delay(300);
    digitalWrite(buzzer, LOW);
    delay(300);
    digitalWrite(buzzer, HIGH);
    delay(300);
    digitalWrite(buzzer, LOW);
  } else {
    digitalWrite(relay, HIGH);
  }

}


here in this link in details what he did: https://srituhobby.com/how-to-make-a-fire-protection-system-with-arduino/
 
Last edited by a moderator:
Back
Top