Merging 3 sketches NOOB ALERT!

Hi guys,

I've tried to merge 3 sketches together and I'm getting conflicting declarations. I know what I am doing wrong but I don't have the experience or knowledge to rectify it. I'm very very new to Arduino so please excuse my ignorance.

This is my sketch here:

#include <dht.h>
#define dht_apin A0 // Analog Pin sensor is connected to

int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // PIR input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status

int sensorPin=7; // microphone input pin
boolean val =0;

int tempPin=A0; // Temperature input pin
boolean val =0;

dht DHT;

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare PIR sensor as input

pinMode(sensorPin, INPUT); // declare microphone input

pinMode(tempPin, INPUT);
Serial.begin(9600);
delay(500); //Delay to let system boot
Serial.println("DHT11 Humidity & temperature Sensor\n\n");
delay(1000); //Wait before accessing Sensor

Serial.begin(9600);
}

void loop(){

// Start of PIR sensor
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}

// Start of Mic program
val =digitalRead(sensorPin);
Serial.println (val);
// when the sensor detects a signal above the threshold value, LED flashes
if (val==HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);

//Start of Temp Program
val =digitalRead(tempPin);
Serial.println (val);
// when the sensor detects a signal above the threshold value, LED flashes
if (val==HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
DHT.read11(dht_apin);

Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");

delay(5000);//Wait 5 seconds before accessing sensor again.

//Fastest should be once every two seconds.

}
}

I'm really struggling with this so any help, even if it is just a minor little tweak to set me on my way would be great.

I'm wanting the project to read the digital signal from the PIR and Microphone and if detected, output for 5 seconds and then turn off. The temperature is just there to monitor and will not control any outputs.

It conflicts immediately at the 'boolean val'... I can see my error but I don't know what to change it to. Really struggling so any help would be great. Thanks

(deleted)

I find it interesting that you know that you can, and need to, change pin numbers, but that you don't know that you can, and should, change the name of the variable that the value returned by digitalRead() is stored in.

Of course, inputPin and sensorPin are useless names. Of course, it's an input pin, or you wouldn't be reading it. Of course there is a sensor connected. What kind of sensor? That is what the variable name should reflect (along with the fact that it IS a pin number or a pin state).

I'm sorry. I'm literally none the wiser. I've zero experience with these whatsoever. I was aware of the mistakes because it tells me about the conflicting declaration at the bottom.. Not because I understand what is happening lol.

it's the int val and boolean val I'm confused with.

#include <dht.h>
#define dht_apin A0 // Analog Pin sensor is connected to

int ledPin = 13; // choose the pin for the LED
int pirPin = 2; // PIR input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int pir = 0; // variable for reading the pin status

int micPin=7; // microphone input pin
boolean mic =0;

int tempPin=A0; // Temperature input pin
boolean temp =0;

Assuming you mean something like that? I get what you are saying.

Loz2212:
I'm sorry. I'm literally none the wiser. I've zero experience with these whatsoever. I was aware of the mistakes because it tells me about the conflicting declaration at the bottom.. Not because I understand what is happening lol.

Think about it for a little while.

The Arduino has a number of I/O pins. You can't use one of them for two different things. It would be like trying to put your two feet into the same shoe.

And as an extension of that idea you cannot use the same name for two different variables.

The first thing you should do is go through the separate programs and make a list of all the areas of overlap.

Then make changes in two of the programs so that you have three separate program without any conflicting variable names or use of I/O pins. Make sure that the three separate programs work with those changes. If there is a problem fix it before trying to merge anything.

When you have all three programs working without any overlapping usages then have a look at this Simple Merge Demo. And don't try to merge all three programs at the same. Merge 2 of them and make the merged version works before trying to merge the 3rd.

...R

Thank you Robin. I believe I have made a very small amount of progress. However, now the MOSFET (switching a motor) starts up and just continues running.. :frowning:

I've attached my code that I have tried my best with.

It makes perfect sense now what you are all saying.

#include <dht.h>
#define dht_apin A0 // Analog Pin sensor is connected to
 
int ledPin = 13;

int MOSFETPin = 8;                // choose the pin for the MOSFET

int pirPin = 2;               // PIR input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int pir = 0;                    // variable for reading the pin status

int micPin=7;                // microphone input pin
boolean value =0;

int tempPin=A0;                // Temperature input pin
boolean val =0; 

dht DHT;
 
void setup() {
  pinMode (ledPin, OUTPUT);
  pinMode(MOSFETPin, OUTPUT);      // declare MOTOR as output
  pinMode(pirPin, INPUT);     // declare PIR sensor as input
  
  pinMode(micPin, INPUT);   // declare microphone input

  pinMode(tempPin, INPUT);
  Serial.begin(9600);
  delay(500);                  //Delay to let system boot
  Serial.println("DHT11 Humidity & temperature Sensor\n\n");
  delay(1000);                //Wait before accessing Sensor
 
  Serial.begin(9600);
}
 
void loop(){

      // Start of PIR sensor
  pir = digitalRead(pirPin);  // read input value
  if (pir == HIGH) {            // check if the input is HIGH
    digitalWrite(MOSFETPin, HIGH);  // turn motor on
    delay (5000);                  // leave running for 5 seconds
    digitalWrite(MOSFETPin, LOW);   // turn motor off
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    digitalWrite(MOSFETPin, LOW); // turn LED OFF
    if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }


  
   // Start of Mic program
   value =digitalRead(micPin);
  Serial.println (value);
  // when the sensor detects a signal above the threshold value, LED flashes
  if (value==HIGH) {
    digitalWrite(MOSFETPin, HIGH);
    delay (5000);
    digitalWrite(MOSFETPin, LOW);
  }
  else {
    digitalWrite(MOSFETPin, LOW);



    //Start of Temp Program 
   val =digitalRead(tempPin);
  Serial.println (val);
  // when the sensor detects a signal above the threshold value, LED flashes
  if (val==HIGH) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }
    DHT.read11(dht_apin);
    
    Serial.print("Current humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature); 
    Serial.println("C  ");
    
    delay(5000);//Wait 5 seconds before accessing sensor again.
 
  //Fastest should be once every two seconds.


    
  }
}

However, now the MOSFET (switching a motor) starts up and just continues running..

Does your MOSFET really have legs? That move well enough for it to run?

Motors run. MOSFETs do not.

  pir = digitalRead(pirPin);  // read input value

Why is pir global? What other function needs access to the value in pir?

What IS pri, if not the state of the PIR? Why do you have another variable called pirState?

I KNOW the answer to that is that you didn't write the code, but I want you to THINK about why that useless variable exists. If you can't figure out why, GET RID OF IT.

@PaulS.. Please keep your bad attitude off this thread. Whether you are an established member or not, your advice is not constructive nor is it helping me. I stated clearly it was switching a motor, which it is. Don't speak to me like I should know what I am doing or patronize me. I'm here because I don't.

I usually don't nibble to trolls but I've had the same condescending attitude from you on other threads I've posted. You aren't giving this site a welcoming feel right now. Not cool.

Please don't comment on this anymore.

Here's a tutorial on merging sketches.

Loz2212:
I've attached my code that I have tried my best with.

How many programs have been merged into that program? (the correct answer is 2 :slight_smile: )

Did you do as I said and get all three programs working separately with compatible pin usage and variable names?

I don't think you have told us what each of the separate programs does and what the merged program is intended to do?

@PaulS may not have a great bedside manner, but his diagnoses are always worth considering carefully. However I am more comfortable using global variables whereas he prefers local variables where possible.

...R

Thank you Robin. I have used 3 sketches I found online, these are not mine at all lol.

A PIR sensor, temperature sensor and microphone. Each one runs individually.. however, I have attempted to now add an output so there is a good chance I've knackered it up.

I'm basically wanting the PIR and Microphone to output to a motor, run it for 5 seconds and stop. Then the programs just resumed until another input is detected. The temperature is there to be monitored on the serial monitor with no intended output.

Apologies for any incorrect terminology.

I'm unsure what you mean by global.

Thank you.

Loz2212:
Thank you Robin. I have used 3 sketches I found online, these are not mine at all lol.

A PIR sensor, temperature sensor and microphone. Each one runs individually..

But you have not told us exactly what each of them does.

I'm unsure what you mean by global.

A global variable is one which is defined outside any function and is then available everywhere throughout the program. A local variable is one that is defined inside a function and consequently cannot be used outside the function.

For this sort of question Google is your friend.

...R

Replace val with

byte micVal =0;
byte pirVal =0;
byte tempVal =0;

and adjust the placed where val gets used to use the appropriate variable.

PaulMurrayCbr:
Replace val with

byte micVal =0;

byte pirVal =0;
byte tempVal =0;




and adjust the placed where val gets used to use the appropriate variable.

I thought I had already advised the OP to do that sort of thing.

...R

Robin2:
I thought I had already advised the OP to do that sort of thing.

...R

Me, too. But OP blew me off. I don't expect him/her to be any more polite with either of you, OR to do as you suggest.

Robin2:
I thought I had already advised the OP to do that sort of thing.

I haven't read the whole thread, obviously. It's going to say all the usual things that threads named "I want to merge N sketches" says. You don't "merge" sketches - you write a sketch that does what you want.

I don't really know what I'm supposed to say. I have a setup where I'm wanting the PIR sensor and Microphone sensor to detect sound or movement and simply output to a motor for 5 seconds. I'm working with a fabricator and welder as he is wanting to patent an idea, this is why I can't say exactly what it does (if that's what you mean).

I'm sorry if I'm coming across as ignorant.. I'm just thick and struggling to understand this.

The PIR sensor I just want to detect human movement and switch an output high for 5 seconds. The same for the microphone. Nothing else needed.

The temperature sensor is just to monitor the ambient air temp and output on the serial monitor.

I'm changing all the things you are asking as I go. I'm trying to do the best with what I know. It's all alien to me. If you need anything else from me or me to be more specific please ask. Apologies again for anyone I appear to have p****d off.

If you need anything else from me or me to be more specific please ask.

If you ARE making code changes, and the code is still not doing what you want, you need to post it, along with any error messages, if the problem is that it fails to compile, or an explanation of what it actually does and how that differs from what you want, if it does compile but doesn't do what you want.

Loz2212:
I'm sorry if I'm coming across as ignorant..

It's not that you are coming across as ignorant. It is just that you are providing no feedback about the things you are doing to follow the advice you have been given. Or, if you don't understand the advice, you have not told us and given us a chance to explain.

For example I suggest a sequence of steps in Reply #4 - I have no idea if you have worked through them.

...R