Hola,
estoy desarrollando un proyecto y me encuentro en problemas.
Basicamente es una bandeja motorizada que gira cuando algún movimiento es detectado por un sensor de Movimiento en la sala , cuando no hay más movimiento detectado, se detiene. Sigue asi hasta llegar al final de su recorrido (una vuelta completa) toca un botón que cambia la dirección y el proceso comienza en la dirección contraria y asi sigue infinitamente.
O sea que el PIR maneja el start/stop del motor y los botones el cambio de dirección.
Estoy trabajando con un ARDUINO UNO, H-Bridge, PIr sensor de Parallax y dos botones.
He realizado el sketch para el Sensor de movimiento basado en un sketch de Kristian Gohlke y funciona perfectamente. He realizado el sketch para utilizar los dos botones para cambiar de direccion hacia adelante y reversa y también funciona.
Lo que no puedo hacer y pido su ayuda es combinar ambos que es lo que necesito para el proyecto.
Aqui publico ambos códigos.
Código para el PIR
/*
* Based on the sketch by Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at
* @date: 3. September 2006
* kr1 (cleft) 2006
* released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
* http://creativecommons.org/licenses/by-nc-sa/2.0/de/
*
*
*
* The sensor's output pin goes to HIGH if motion is present.
* However, even if motion is present it goes to LOW from time to time,
* which might give the impression no motion is present.
* This program deals with this issue by ignoring LOW-phases shorter than a given time,
* assuming continuous motion is present during these phases.
*
*/
/////////////////////////////
//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;
int pirPin = 2; //the digital pin connected to the PIR sensor's output
int IN3 = 5;
int IN4 = 4;
int ENB = 3; // ENB conectada al pin 3 de Arduino
/////////////////////////////
//SETUP
void setup() {
Serial.begin(9600);
pinMode (ENB, OUTPUT);
pinMode (IN4, OUTPUT); // Input4 conectada al pin 4
pinMode (IN3, OUTPUT); // Input3 conectada al pin 5
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);
}
////////////////////////////
//LOOP
void loop() {
if (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;
}
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,220);
digitalWrite (IN4, LOW);
}
}
}
Código para los botones
const int buttonForwardPin = 8; // the number of the pushbutton pin
const int buttonRewersePin = 9; // the number of the pushbutton pin
const int IN3 = 5;
const int IN4 = 4;
const int ENB = 3;
int buttonStateF = 0;
int buttonStateR = 0;
int IN3State = LOW;
int IN4State = LOW;
void setup() {
// put your setup code here, to run once:
pinMode(ENB, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(buttonRewersePin, INPUT);
pinMode(buttonForwardPin, INPUT);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
digitalWrite(buttonRewersePin, 0);
digitalWrite(buttonForwardPin, 0);
}
void loop() {
buttonStateF = digitalRead(buttonForwardPin);
buttonStateR = digitalRead(buttonRewersePin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonStateF == 1) {
digitalWrite(IN4, HIGH);
digitalWrite(IN3, LOW);
analogWrite(ENB, 255);
buttonStateR = 0;
}
if (buttonStateR == 1) {
digitalWrite(IN4, LOW);
digitalWrite(IN3, HIGH);
analogWrite(ENB, 250);
buttonStateF = 0;
}
buttonStateR = 0;
buttonStateF = 0;
// FORWARD
delay(1000);
}
Cualquier sugerencia es muy bienvenida.
Desde ya gracias
rudni