ребят, проверьте пожалуиста код. У меня что-то не получается. мотор крутится без остановки.
Код:
/*
Analog Pin 0: microphone
Digital Pin 3: DC gear reduction motor attached to the lock. (Or a motor controller or a solenoid or other unlocking mechanisim.)
Digital Pin 5: Green LED.
*/
// Pin definitions
const int soundSensor = 0; // microphone on pin 0.
const int lockMotor = 3; // Gear motor used to turn the lock.
const int greenLED = 5; // Status LED
// Tuning constants. Could be made vars and hoooked to potentiometers for soft configuration, etc.
const int threshold = 550; // Minimum signal from the microphone to register as a sound.
const int soundFadeTime = 100; // milliseconds we allow a sound to fade before we listen for another one. (Debounce timer.)
const int lockTurnTime = 300; // milliseconds that we run the motor to get it to go a half turn.
// Variables.
int soundSensorValue = 0;
void setup() {
pinMode(lockMotor, OUTPUT);
pinMode(greenLED, OUTPUT);
Serial.begin(9600); // Uncomment the Serial.bla lines for debugging.
Serial.println("Program start."); // but feel free to comment them out after it's working right.
//digitalWrite(greenLED, HIGH); // Green LED on, everything is go.
}
// Records the sound.
void listenToSound(){
Serial.println("someone is blowing in");
}
// Runs the motor (or whatever) to turn the disc.
void triggerSpinDisc(){
Serial.println("Disc is spinning!");
// turn the motor on for a bit.
digitalWrite(lockMotor, HIGH);
digitalWrite(greenLED, HIGH); // And the green LED too.
delay (lockTurnTime); // Wait a bit.
digitalWrite(lockMotor, LOW); // Turn the motor off.
}
void stopDisc(){
Serial.println("disc is stopping");
delay(soundFadeTime);
digitalWrite(lockMotor, LOW);
digitalWrite(greenLED, LOW);
}
void loop() {
// Listen for any sound at all.
soundSensorValue = analogRead(soundSensor);
Serial.println(soundSensorValue);
if (soundSensorValue >=threshold){
listenToSound();
triggerSpinDisc();
} else {
stopDisc();
}
}