Combining multiple aurdino codes

Hey.
I'm using the aurdino uno board for my home automation project. Basically it's an amplifier and for that i have installed two sensors.

  1. Sound sensor
  2. UV sensor
    The sound sensor senses the frequency of our clap and switches on. The UV sensor calculates our distance from the amplifier and and then controls the volume.
    I'm facing an issue with combining the codes.
    Please help me with the same.

I'm facing an issue with combining the codes.

What have you tried and what are the issues ?

Please post the code and explain the problems

Code 1:

int currentValue;
int maxValue;
int minValue;
unsigned long timer;
int sampleSpan = 200; // Amount in milliseconds to sample data
int volume; // this roughly goes from 0 to 700
int ledpin = 7;

void setup()
{
Serial.begin(9600);
pinMode (ledpin, OUTPUT);
resetValues();
}

void loop()
{
currentValue = analogRead(A0);

if (currentValue < minValue) {
minValue = currentValue;
}
if (currentValue > maxValue) {
maxValue = currentValue;
}

if (millis() - timer >= sampleSpan) {
volume = maxValue - minValue;
//Serial.println(volume);
resetValues();
}

// I arbitrarily select 100 as the value above which the
//microphone is picking a loud noise.
if (volume > 400)
{
Serial.println(“Loud”);
digitalWrite(ledpin,HIGH);
} else
{
Serial.println(“Quiet”);
digitalWrite(7,LOW);
}

}

void resetValues()
{
maxValue = 0;
minValue = 1024;
timer = millis();
}

Code 2:

#define trigPin 13
#define echoPin 12

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

if (distance >= 200 || distance <= 0){
Serial.println(“Out of range”);
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}

And now we need to see your attempt to combine them and details of what worked and what didn't.

Steve

...and code tags

WELCOME to the Forum

it is our ritual that we request every new member to read HOW TO USE THIS FORUM

in your case, you nailed the subject line, bravo

We have a thing called 'code tags' more on that in #7 in that link

also, to edit a post, you go to the bottom right of the post and modify it.

Please use code tags and post your attempt to merge the to sketches.