My code listens for a KNOCK and then moves a servo to a position, waits, and then moves it back and waits again. The problem is that it seems to be detecting KNOCKS and moving the servo while it it supposed to be "delaying" for x number of milliseconds. I read the following in the reference section for delay().
"No other reading of sensors, mathematical calculations, or pin manipulation can go on during the delay function, so in effect, it brings most other activity to a halt."
"Certain things do go on while the delay() function is controlling the Atmega chip however, because the delay function does not disable interrupts. Serial communication that appears at the RX pin is recorded, PWM (analogWrite) values and pin states are maintained, and interrupts will work as they should."
Any ideas?
Thanks!
Matt
// load servo library and create servo object
#include <Servo.h>
Servo myServo;
// constants
const int knockSensor = A2; // the piezo is connected to analog pin 1
const int threshold = 3; // threshold value to decide when the detected sound is a knock or not
const int servoPin = 6; // the servo is connected to digital pin 6
// variables
int sensorReading = 0; // variable to store the value read from the sensor pin
void setup() {
myServo.attach(servoPin); // attaches the servo on pin to the servo object
myServo.write(servoStartPos);
delay(12000);
Serial.begin(9600); // serial port
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
Serial.println(sensorReading);
Serial.println("1.5 Second delay");
delay(1500);
myServo.write(90);
delay(12000);
myServo.write(0);
delay(75000);
}
}