How to merge three scetches?

Hello guys! I have several working scetches which i want to merge into one. The first one turns on LED when sound is detected, second one turns on LED when light is detected and the third one turns on LED when gas is detected. Can anyone help me how to merge them?

// set pin numbers:
const int soundPin = 2; // the number of the Sound sensor pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int soundState = 0; // variable for reading the sound sensor status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the sound sensor pin as an input:
pinMode(soundPin, INPUT);
}

void loop(){
// read the state of the sound sensor value:
soundState = digitalRead(soundPin);

// check if any sound is detected.
// if it is, the sound sensor is :PW:
if (soundState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
delay(60000);
//wait 60 seconds and turn it off
digitalWrite(ledPin, LOW);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

const int lightPin = 2;
const int ledPin = 13;
int value = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(lightPin, INPUT);
Serial.begin(9600);
}
void loop() {
value = digitalRead(lightPin);
if (value == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
delay(60000);
//wait 60 seconds and turn it off
digitalWrite(ledPin, LOW);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int ledPin = 13; // LED connected to digital pin 13

int sensorValue = 0; // value read from the sensor

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// determine alarm status
if (sensorValue >= 200)
{
digitalWrite(ledPin, HIGH); // sets the LED on

}
else
{
digitalWrite(ledPin, LOW); // sets the LED off
}

// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.println(sensorValue);

// wait 100 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(100);
}

you start with one of the sketches and

  1. merge global variables,
  2. merge functions
  3. merge includes
  4. merge the setup() code
  5. merge the loop code()

in every merge step you should resolve conflicting names, #parameters etc.

do that for every additional sketch.
So step one would lead to - note there are some conflicts to resolve!

// set pin numbers:
const int soundPin = 2;     // the number of the Sound sensor pin
const int ledPin =  13;      // the number of the LED pin

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int ledPin = 13;                 // LED connected to digital pin 13


const int lightPin = 2;
const int ledPin =  13;
int value = 0;

int sensorValue = 0;        // value read from the sensor
// variables will change:
int soundState = 0;         // variable for reading the sound sensor status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the sound sensor pin as an input:
  pinMode(soundPin, INPUT);     
}

void loop(){
  // read the state of the sound sensor value:
soundState = digitalRead(soundPin);
  
  // check if any sound is detected.
  // if it is, the sound sensor is :PW:
  if (soundState == LOW) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
    delay(60000);
    //wait 60 seconds and turn it off
    digitalWrite(ledPin, LOW);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}

@robtillaart has described the process very neatly.

I would make one additional suggestion.

I would put the code from each sketch's loop() into a separate function in the new sketch so it looks like this

void loop() {  // this is the new sketch's loop()
   functionA();
   functionB();
   functionC();
}

void functionA() {
   // code from loop() of sketchA
}

void functionB() {
   // code from loop() of sketchB
}

void functionC() {
   // code from loop() of sketchC
}

This should make it much easier not to get hopelessly confused with the different pieces of code. It will be even easier to follow if you give the functions names that reflect what they do.

Having said all that, it may actually be easier to write your new sketch from scratch just using the existing sketches for guidance.

...R

const int analogInPin = A0;  
const int ledPin = 17;       

const int lightPin = 2;
const int ledPin2 =  14;

const int soundPin = 3;     
const int ledPin3 =  15; 

int lightValue = 0;
int sensorValue = 0;       
int soundState = 0;   



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

  pinMode(ledPin, OUTPUT);     
  pinMode(ledPin2, OUTPUT); 
  pinMode(lightPin, INPUT); 
  pinMode(ledPin3, OUTPUT);      
  pinMode(soundPin, INPUT); 
}

void loop() {
     light();
     sound();
     gas();    
 
  


               
}

void gas(){
  sensorValue = analogRead(analogInPin);
if (sensorValue >= 200)
  {
    digitalWrite(ledPin, HIGH);  
   delay(10000); 
   digitalWrite(ledPin, LOW);  
  }
}
  
  void light(){
     lightValue = digitalRead(lightPin);  
  
  if (lightValue == HIGH) {     
    digitalWrite(ledPin2, HIGH);  
    delay(10000);
    digitalWrite(ledPin2, LOW);  
  } 
  }
  
  void sound(){
    soundState = digitalRead(soundPin);
  if (soundState == LOW) {       
    digitalWrite(ledPin3, HIGH);  
    delay(10000);
    digitalWrite(ledPin3, LOW);  
  }
  } 
  
  }

Here is what I did and it is working but when light is HIGH and ledPin2 is HIGH the other functions are not working or when gas is HIGH the other not work. They work one after another. How can i make them work together? Should i remove all the delays?

Should i remove all the delays?

Yes

If you need portions of your program to execute for periods of time but don't want pauses to prevent other functions, reading of sensors etc affected then look at using the principle in the BlinkWithoutDelay example in the IDE.

Note the start time of an activity and periodically check whether the required time has elapsed. If not, do something else and come back later to check again, otherwise react to the time elapsing.

In the first post of this Thread I wrote an extended example of the Blink Without Delay concept. It may be similar to what you are trying to do.

...R

const int analogInPin = A0;  
const int lightPin = 2;
const int soundPin = 3;

Sound, light, and gas, huh? Well, that's perfectly obvious.

PaulS:

const int analogInPin = A0;  

const int lightPin = 2;
const int soundPin = 3;



Sound, light, and gas, huh? Well, that's perfectly obvious.

Yes, sound, light and gas. What is perfectly obvious?

Thank you for your help guys!

What is perfectly obvious?

Nothing. Two pin names make sense. The other does not.