Estoy de vuelta
Seguí los datos que gentilmente surbyte me sugirió y he trabajado bastante pero estoy en un callejón sin salida. No funciona.
Cuando comienza el sistema, el sensor calibra sus 30 segundos y está activo.
Pero aunque ningún botó esté presionado el motor gira al detectar movimiento como si el botón del PIN 9 hubiera sido presionado. Si presiono el botón del PIN 8 cambia de dirección pero solo si lo mantengo presionado, si lo suelto cambia nuevamente.
EL sensor responde pero no respeta la transisión (PAUSE TIME) para asumir LOW. Lo cual funciona sin problemas en el código del sensor separado.
Aqui adjunto el código como está ahora.
//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 = 8; // the number of the pushbutton1 pin
const int button2Pin = 9; // 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, LOW);
digitalWrite(button2Pin, LOW);
// initialize the H-Bridge pin as an output:
pinMode (ENB, OUTPUT);
pinMode (IN4, OUTPUT);
pinMode (IN3, OUTPUT);
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);
digitalRead(pirPin);
if (digitalRead(pirPin) == HIGH && millis() > 1000) {
if (button1State == HIGH){
// Motor one direction
analogWrite(ENB,220);
digitalWrite (IN4, HIGH);
digitalWrite (IN3, LOW);
}
else if (digitalRead(pirPin) == HIGH && millis() > 1000) {
if (button2State == HIGH);
// Motor other direction
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;
}
else if (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,0);
}
delay(50);
analogWrite(ENB,0);
}
}
Muchas gracias y un saludo