-------------------------------------------------------ENGLISH--------------------------------------------------
Hi, I'm new into arduino, I only created a few proyects. I'm having probles with using the HC-SR04 as an ON/OFF module and also with the position of the servo. My objective is to be able to, with one servo and one HC-SR04 open and close a door and let the servo holding the position.
Right now I made this code but the servo returns to the original poition and i want it to stay until i pass the hand again.
-------------------------------------------------------SPANISH--------------------------------------------------
Muy buenas, soy relativamente nuevo con el arduino, estoy teniendo problemas a la hora de usar el HC-SR04 como un interruptor.Es deci, mi objetivo es pasar la mano por encima del sensor y que abra una puerta, y si volvemos a pasar la mano, que esta se cierre. Por ahora solo he conseguido que al pasar la mano el servo se mueva hasta los 90 grados y una vez quito la mano, el servo vuelve a su posicion. No consigo dar con la forma para que este mantenga su posicion.
Gracias de antemano.
-----------------------------------------------------CODE/CODIGO----------------------------------------------
#include <Servo.h>
const int trigPin = 6;//Setup the variables for the HC-SR04
const int echoPin = 5;
Servo myservo;
// create servo object to control a servo
// variable to store the servo position
int pos = 0;
int pos3 = 90;
void setup() {
// initialize serial communication:
Serial.begin(9600);
// attaches the servo on pin 9 to the servo object
myservo.attach(9);
}
void loop()
{
long duration, inches, cm;
// The sensor is triggered by a HIGH pulse of 10 or
// more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
//Tell the Arduino to print the measurement in the serial console
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
//----------------comenzamos con las posiciones del motor---------------
if (cm >= 30 ) {myservo.write(pos); //posiciones segun distancia
}
else if (cm >= 0 && cm < 30) {myservo.write(pos3);//distintas posiciones del servo
}
delay(50);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
//Converts the Microseconds Reading to Centimeters
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
-----------------------------------------------------END OF CODE-----------------------------------------
Thanks for your help.