im building a baby gate in class. The idea is that a clap or snap that is higher than 20 decibels will turn it on. But if there is something in front of the ultrasonic sensor within 20 inches, the gate will not open. There is one servo to open/lock our lock, and one to open/close the gate. The sensors wont work, so can someone tell me how to fix my code.?
int threshold = 13; //Set minimum threshold for LED lit
int sensorvalue = 0;
long duration, inches;
#include <Servo.h>
Servo lock; // create servo object to control a servo
Servo gate; // a maximum of eight servo objects can be created
void setup()
{
Serial.begin(9600);
pinMode(2, OUTPUT);
lock.attach(6); // attaches the servo on pin 9 to the servo object
gate.attach(8);
pinMode(12, OUTPUT);
pinMode(11, INPUT);
}
void loop()
{
gate.write(90); // alosed and locked angles
lock.write(90);
sensorvalue = analogRead(A0); //Read the analog value
Serial.print("Analog: ");
Serial.println(sensorvalue); //Print the analog value
//Serial.print(" ");
//Serial.print("Digital: ");
//Serial.println(digitalRead(DO)); //Print the digital value
digitalWrite(12, LOW);
delayMicroseconds(2);
digitalWrite(12, HIGH);
delayMicroseconds(5);
digitalWrite(12, LOW);
duration = pulseIn(11, HIGH); // measure the time of flight of sound wave
inches = duration / 74 / 2; // 1130 ft/s * 12in/ft * 1s/1,000,000us = 74 // factor of 2 since sound travels out and back
Serial.print(inches);
Serial.println(" inches");
if ((sensorvalue >= threshold) && (inches < 20))
{
lock.write(0); //unlocked angle
delay(1500);
gate.write(90); //open angle
delay(10000);
gate.write(0); //closed angle
delay(2000);
lock.write(90);
delay(1500); //locked angle
}
}