i know ive made mistakes but i dont know what they are. we are making a gate that opens when you snap and doesnt open when there is something in the way the gate also closes itself with a 360 servo which has a spool that with a string t0 bring it back this is our code. our sound sensor isnt getting correct readings and our 360 motor isnt pulling the string back and isnt working it just keeps going PLEASE HELP!!!!!!!!
CODE:
int threshold = 17; //Set minimum threshold for LED lit
int sensorvalue = 0;
int maximumRange = 100;
int minimumRange = 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);
gate.write(180);
lock.write(180);
}
void loop()
{
//gate.write(180); // alosed and locked angles
//lock.write(180);
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");
//Serial.println(sensorvalue);
//Serial.println(inches);
if ((sensorvalue > 100) && (inches > 20))
{
lock.write(180); //unlocked angle
delay(1500);
gate.write(180); //open angle
delay(1000);
gate.write(0); //closed angle
delay(1000);
lock.write(0);
delay(1500); //locked angle
}
}