Hello everyone!
I'm a beginner in the Arduino world and have chosen a project with the HC-SR04 and servo motor for my technical work as an experimental part.
However, after many attempts, I can't really reach my goal.
My idea was that the servomotor would rotate 90° when you hold an object in front of the sensor. If you take the object away again, the motor should turn back or close.
Could someone help me here?
I'd like to add my ideas here...
Warm greetings
#include <Servo.h>
#include <stdio.h>
Servo servo1;
long zeit=0;
long entfernung1 = 0;
long entfernung2 = 0;
int zu = 5;
int auf = 90;
int anzahl = 2;
void setup() {
// put your setup code here, to run once:
# define TRIGGER 3
# define ECHO 2
Serial.begin(115200);
pinMode(TRIGGER,OUTPUT);
pinMode(9,OUTPUT);
servo1.attach(9);
servo1.write(zu);
}
void loop() {
// put your main code here, to run repeatedly:
for (int i=0; i <= anzahl-1; i++){
zeit=0;
noInterrupts();
digitalWrite(TRIGGER, LOW);
delayMicroseconds(13);
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGGER, LOW);
zeit = pulseIn(ECHO, HIGH);
interrupts();
for (int i=0; i <= anzahl -1; i++)
{
servo1.write(zu);
entfernung2 = entfernung1 / anzahl;
Serial.println(entfernung2);
if (entfernung2 > 9550){
digitalWrite(9, HIGH);
Serial.println("open");
servo1.write(auf);
delay(3000);
digitalWrite(9, LOW);
servo1.write(zu);
delay(1000);
}
}Vorformatierter Text
Faulty logic, here. How would you detect removing the object if the HC-SR04 was already moved completely out of range? Or are you implying the object kept moving with the sensor?