COMPILED SKETCHES NOT WORKING PROPERLY

Hello,
First of all, I am super new to arduino and programming. I just started working on it for a Science project for one of my 9th grade students.
I am working on a project to make a smart blind stick with ultrasonic, motion sensor, and water level sensor. I compiled 3 sketches into one and after correcting several errors (using this forum), I was able to upload the combined sketches into one. However, the buzzer goes off indefinitely, it just keeps buzzing, and I don't know what I did wrong. Can you check my program and see what I did wrong.

Here is compiled sketches:

#include "Arduino.h"

//ultrasonic sensor

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance = duration * 0.034 / 2;

safetyDistance = distance;
if (safetyDistance <= 50) {
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
}

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

//motionsensor;
// defines variables

const int motionpin = A0;
const int ledpin = 13;
const int buzzpin = 11; // ledpin,motionpin and buzpin are not changed throughout the process
int motionsensvalue = 0;

void loop ();

// put your main code here, to run repeatedly:

motionsensvalue = analogRead(motionpin); // reads analog data from motion sensor
if (motionsensvalue >= 200) {
digitalWrite(ledpin, HIGH);
tone(buzzpin, 10); //turns on led and buzzer
}
else {
digitalWrite(ledpin, LOW); //turns led off led and buzzer
noTone(buzzpin);
}

//WaterLevel

//www.elegoo.com
//2016.12.9

int adc_id = 0;
int HistoryValue = 0;
char printBuffer[128];

{
Serial.begin(9600);

int value = analogRead(adc_id); // get adc value

if (((HistoryValue >= value) && ((HistoryValue - value) > 10)) || ((HistoryValue < value) && ((value - HistoryValue) > 10)))

sprintf(printBuffer, "ADC%d level is %d\n", adc_id, value);
Serial.print(printBuffer);
HistoryValue = value;
}
}

What do the prints show?

Edit: does this even compile? Why have you got a loop function prototype inside loop?
Stop reinitialising the serial port!
Code tags would be useful.

(This is not a bootloader issue, and you don't need to SHOUT)

Thank you for your quick response. Like I said, I am new to this. I will find someone else that can help me.

btrevino4:
First of all, I am super new to arduino and programming.

Just looking at your title - welcome to the club., every programmer has been in the same position - you can compile a program that is logically ridiculous.

Stick with it and a solution will be found.

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor.

...R

Your code is a bit too confusing for me. Please don't redefine pins with different names like buzzer and buzzpin. Get rid of the meaningless comments and the second 'void loop()'. Also adding a few comments saying what the parts of the code are intended to do might help.

When does the buzzer go off? The second the program starts or after some particular input?

Steve

Hello,

The buzzer goes off the second the program starts and it will continue to buzz. I got the sketches from youtube and just compiled them into one; so, the names like buzz and buzzpin came with them. Thank you for your patience. I am barely learning.

I've deleted your other cross-post @btrevino4.

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Your next move is probably to put some some Serial.prints in the program so you can see where the program is getting to. There aren't many places where the buzzer is started to put a Serial.println("Buzzer starting 1" etc. immediately before each one.

Oh and try this change

    tone(buzzpin, 10); // CHANGE THIS to digitalWrite(buzzpin, HIGH);
  }
  else {
    digitalWrite(ledpin, LOW); //turns led off led and buzzer
    noTone(buzzpin);   // CHANGE THIS to digitalWrite(buzzpin, LOW);

One bit of your code thinks you have an active buzzer. This bit was treating it as a passive buzzer. It can't be both.

Steve

No, the next move is to remove any "Serial.begin"s apart from the first one in setup()...

The different sketches are using two different kinds of buzzers. The one that use " digitalWrite(buzzer, HIGH);" to turn on the buzzer expects a buzzer that will make a sound when you provide power. The one that uses " tone(buzzpin, 10);" is expecting something like a speaker which will make noise if you pulse it repeatedly at the desired frequency, in this case it's 10 Hz which is strange because that is below the range of human hearing.

Do the three separate sketches each work as desired separately? If they do, it is probably possible to combine them.

Yes, the sketches work properly separatedly. The problem is when I try to combine them.

This Simple Merge Demo may give you some ideas.

Before trying to merge the programs go through each of them carefully to make sure they are not trying to use the same resource - for example an I/O pin, or the same variable name. If you find conflicts change something in one of the separate programs and make sure it still works with the change.

...R

I wrote a post in the Introductory Tutorials section that discusses combining two or more programs... it’s as good a place as any to start, and if you follow the suggestions, you’ll learn a lot about the code you’re copying, and how to get where you want.

Search for BEGINNERS:

In addition to two of the sketches using different kinds of buzzers on the same output pin, two use different input devices on the same analog input pin: A0. That is another conflict that you will have to resolve by moving one of the sensors to a different pin.

If you add a Serial.println("Message"); in each 'if' statement you should see exactly why the sketch is turning on the buzzer.

void setup()
{
  setup1();
  setup2();
  setup3();
}

void loop()
{
  loop1();
  loop2();
  loop3();
}


//////////// SKETCH 1 /////////////////
//ultrasonic sensor

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;

void setup1() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(buzzer, OUTPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); // Starts the serial communication
}

void loop1() {
  // Trigger the output pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the round-trip travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // pulseIn() returns 0 on timeout
  if (duration != 0)
  {
    // Calculating the distance
    distance = duration * 0.034 / 2; // Convert to... cm?

    safetyDistance = distance;
    if (safetyDistance <= 50)
    {
      Serial.print("Alarm! safetyDistance: ");
      Serial.println(safetyDistance);
      digitalWrite(buzzer, HIGH);
      digitalWrite(ledPin, HIGH);
    }
    else
    {
      digitalWrite(buzzer, LOW);
      digitalWrite(ledPin, LOW);
    }
  }
}

//////////// SKETCH 2 /////////////////
//motionsensor;
// defines variables

const int motionpin = A0;
int motionsensvalue = 0;

void setup2()
{
  // Done in setup1():
  // pinMode(ledpin, OUTPUT);
}

void loop2()
{
  // put your main code here, to run repeatedly:

  motionsensvalue = analogRead(motionpin); // reads analog data from motion sensor
  if (motionsensvalue >= 200)
  {
    Serial.println("Alarm! motionsensvalue >= 200");
    digitalWrite(buzzer, HIGH);
    digitalWrite(ledPin, HIGH);
  }
  else
  {
    digitalWrite(ledPin, LOW); //turns led off led and buzzer
    digitalWrite(buzzer, LOW);
  }
}


//////////// SKETCH 3 /////////////////
//WaterLevel

//www.elegoo.com
//2016.12.9

const int adc_id = A1;  // Can't use the same pin as sketch 2!
int HistoryValue = 0;

void setup3()
{
  // Already done in setup1();
  // Serial.begin(9600);
}


void loop3()
{
  int value = analogRead(adc_id); // get adc value

  if (((HistoryValue > value) && ((HistoryValue - value) > 10))
      || ((HistoryValue < value) && ((value - HistoryValue) > 10)))
  {
    Serial.print("Water level value is:");
    Serial.println(value);
  }
  HistoryValue = value;
}