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
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;
}
}