Hi I'm still a novice to arduino and am using a pir sensor to trigger a servo to move and push down a sewing machine pedal. I've made it so there is a 5second delay between the sensor being triggered and when it is triggered it randomly selects how long it will triggered for (between 5 and 15 seconds). The problem I'm having is that the servo wants to continually move when triggered instead of reaching a certain angle and stopping and returning when not triggered, I don't want the servo to burn out.
Here is the code i've been using so far:
#include <Servo.h>
/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
Servo myServo;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
long randomNumber;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 3; //the digital pin connected to the PIR sensor's output
int ledPin = 13;
int v = 0;
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
myServo.attach(14); //analog pin 0
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
randomSeed(analogRead(0));
}
////////////////////////////
//LOOP
void loop(){
if (digitalRead(pirPin)) {
// Pin reads HIGH
Serial.println("high");
myServo.write (180);
} else {
// Pin reads low
Serial.println("low");
myServo.write (0);
} delay(1000);
randomNumber = random(10,20);
}
For some reason I when the sensor is triggered it's showing as low and high when not triggered.
Any Help would be much appreciated