I am working on a security system that involves using a servo to pull the trigger on a Nerf gun. This servo is triggered by a laser getting cut which changes the reading on the photo-resistor. It can also be temporarily de-activated by placing by fingerprint on the reader.
The problem is that while the code is not in a delay, the servo twitches back and forth a couple degrees about once a second. This would not be a big deal except for the fact that it makes noise. Is there any way to fix this problem?
Here is a copy of my code:
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
#include <Servo.h>
LiquidCrystal lcd(10);
const int RED = 7;
const int GREEN = 6;
const int BLUE = 5;
const int photo = A0;
int light;
int pos = 0;
Servo trigger;
int trig;
SoftwareSerial mySerial(2, 3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
int fingerprintID = 0;
int incorrect = 0;
//=========================================================================
void setup() {
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" - DH's NERF ");
lcd.setCursor(0, 1);
lcd.print("Security System");
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
trigger.attach(9);
trigger.write(0);
trig = 0;
pinMode(photo, INPUT);
//greenLED();
Serial.begin(9600);
Serial.println(" - DH's NERF Security System");
finger.begin(57600);
//delay(1000);
}
//=========================================================================
void loop() {
fingerprintID = getFingerprintID();
if (fingerprintID == 1 || fingerprintID == 2) {
if (trig != 0) {
trigger.write(0);
trig = 0;
}
Serial.println(" - Access Granted");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("-Access Granted");
lcd.setCursor(0, 1);
lcd.print("Wellcome David!");
greenLED();
incorrect = 0;
delay(4000);
}
else if (fingerprintID == -1) {}
else {
incorrectFinger();
}
if (incorrect >= 3) {
if (trig != 75) {
trigger.write(75);
trig = 75;
}
Serial.println(" - Intruder Recognized");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" < Intruder > ");
lcd.setCursor(0, 1);
lcd.print(" < Recognized > ");
incorrect = 0;
redLED();
delay(4000);
}
else {
runLaserSensor();
}
}
//=========================================================================
void runLaserSensor() {
light = analogRead(photo);
Serial.print(light);
lcd.setCursor(0, 0);
lcd.print(light);
lcd.print(" ");
if (light <= 333 && light >= 100) { //lots of light
if (trig != 0) {
trigger.write(0);
trig = 0;
}
Serial.println(" - Beam is not broken");
lcd.setCursor(0, 1);
lcd.print(" - Beam Intact ");
blueLED();
}
else { //not much light
if (trig != 75) {
trigger.write(75);
trig = 75;
}
Serial.println(" - Laser Cut!");
lcd.setCursor(0, 1);
lcd.print(" - Laser Cut! ");
redLED();
}
delay(50);
}
//=========================================================================
int getFingerprintID() {
uint8_t p = finger.getImage(); //take pic of finger
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz(); //look in the figerprint database
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch(); //check if the finger is correct
if (p != FINGERPRINT_OK) {
incorrectFinger();
return -1;
}
return finger.fingerID;
}
//=========================================================================
void incorrectFinger() {
incorrect++;
Serial.print("Incorrect: ");
Serial.println(incorrect);
}
//=========================================================================
void greenLED() {
digitalWrite(RED, LOW);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, LOW);
}
//==============================================
void blueLED() {
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, HIGH);
}
//==============================================
void redLED() {
digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
}
Thanks in advance,
--DH