How to Merge Arduino Codes for Temperature and Smoke Sensors?

Click For Summary

Discussion Overview

The discussion focuses on merging two Arduino codes for a project involving temperature and smoke sensors. The project aims to activate a fan when a temperature rise is detected and to pump water when smoke is indicated. Participants explore how to combine the functionalities of both codes into a single program.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant seeks guidance on merging two separate Arduino codes for a school project involving temperature and smoke sensors.
  • Another participant suggests ensuring that the Arduino pins for both tasks do not conflict and recommends creating separate functions for each task to manage variables effectively.
  • A participant expresses confusion about the programming language and indicates a preference for using C language in the Arduino IDE.
  • One participant requests the individual code pieces from the provided links to better assist in merging the codes.
  • A participant provides the first code for the temperature sensor and describes its functionality, including how it controls the fan based on temperature readings.
  • The same participant shares the second code for the smoke sensor, detailing its operation and how it activates a relay and buzzer when smoke is detected.

Areas of Agreement / Disagreement

Participants have not reached a consensus on how to merge the codes, and multiple approaches are being discussed. There is uncertainty regarding the best method to integrate the functionalities of both sensors.

Contextual Notes

The discussion includes references to specific hardware (Arduino Uno and Nano) and code snippets, but there may be limitations in understanding how to effectively combine the two codes without potential conflicts or issues in functionality.

Mariam-baher
Messages
3
Reaction score
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
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.
 
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
 
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.
 
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:

Similar threads

Replies
14
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K