Thank you Steve,
I have one sketch I was working yesterday. I feel embarrassed but I will post it.
Thank you again.
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 10000;
boolean lockLow = true;
boolean takeLowTime;
const int pirPin = 2; //the digital pin connected to the PIR sensor's output
const int button1Pin = 2; // the number of the pushbutton1 pin
const int button2Pin = 7; // the number of the pushbutton2 pin
const int IN3 = 5;
const int IN4 = 4;
const int ENB = 3;
int button1State = 0; // variable for reading the pushbutton status
int button2State = 0; // variable for reading the pushbutton
void setup() {
//start serial connection
Serial.begin(9600);
// initialize the pushbutton pin as an input:
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
digitalWrite (button1Pin, HIGH);
digitalWrite (button2Pin, HIGH);
// initialize the relay pin as an output:
pinMode (ENB, OUTPUT);
pinMode (IN4, OUTPUT); // Input4 conectada al pin 4
pinMode (IN3, OUTPUT);
//preset the relays to LOW
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
pinMode(pirPin, INPUT);
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);
}
void loop() {
// read the state of the pushbutton values:
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if ((button1State == HIGH) && (digitalRead(pirPin) == HIGH && millis() > 1000)) {
// Motor gira en un sentido
analogWrite(ENB,220);
digitalWrite (IN4, HIGH);
digitalWrite (IN3, LOW);
if (lockLow) {
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis() / 1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}{
analogWrite(ENB,220);
digitalWrite (IN4, HIGH);
digitalWrite (IN3, LOW);
}
if ((button1State == HIGH) && (digitalRead(pirPin) == LOW)) {
if (takeLowTime) {
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if (!lockLow && millis() - lowIn > pause) {
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause) / 1000);
Serial.println(" sec");
delay(50);
analogWrite(ENB,220);
digitalWrite (IN4, LOW);
}
if ((button2State == HIGH) && (digitalRead(pirPin) == HIGH && millis() > 1000)) {
// Motor gira en un sentido
analogWrite(ENB,220);
digitalWrite (IN4, LOW);
digitalWrite (IN3, HIGH);
if (lockLow) {
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis() / 1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if ((button2State == HIGH) && (digitalRead(pirPin) == LOW)) {
if (takeLowTime) {
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if (!lockLow && millis() - lowIn > pause) {
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause) / 1000);
Serial.println(" sec");
delay(50);
analogWrite(ENB,220);
digitalWrite (IN3, LOW);
}
}
}
}