A system that will work at night. When my distance sensor doesn't detect anything, both the light servo and the distance servo are constantly rotating. When the distance sensor detects an object, these 2 servos stay at that angle. However, while everything is fine in my code, I have a lot of problems with these rotations. Instead of constantly rotating, it rotates slowly and stutterly. I would be glad if you could help me with this. I would also like it to follow the movement, but this is all I can do.
#include <VarSpeedServo.h>
VarSpeedServo sensorServo; // Distance sensor servo motor
VarSpeedServo lightServo; // Light servo motor
const int trigPin = 12;
const int echoPin = 11;
const int ldrPin = A0; // LDR analog pin
const int lightPin = 3; // LED digital pin
long duration;
int distance;
int sensorAngle = 0; // Distance sensor angle
int lightAngle = 90; // Light servo start angle
int ldrValue = 0; // LDR değeri
bool objectDetected = false; //
bool sensorPaused = false; //
bool scanningForward = true; // (true: forward, false: back)
void setup() {
sensorServo.attach(9); // Distance sensor servo D9 pin
lightServo.attach(10); // Light servo D10 pin
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(lightPin, OUTPUT);
sensorServo.write(90); // Center the sensor while starting
lightServo.write(90); // center the light while starting
}
void loop() {
// Control ambient light with LDR
ldrValue = analogRead(ldrPin);
// Day or night control
if (ldrValue < 500) { // Night mod
digitalWrite(lightPin, HIGH); // Turn on the light
if (!sensorPaused) {
// Scanning of distance sensor and light servo
if (scanningForward) {
sensorAngle++;
if (sensorAngle >= 180) scanningForward = false;
} else {
sensorAngle--;
if (sensorAngle <= 0) scanningForward = true;
}
sensorServo.write(sensorAngle, 150); // Sensor rotate
lightServo.write(sensorAngle, 150); // Light rotate
delay(10); //
distance = measureDistance();
if (distance > 0 && distance < 50) { // Object detected
objectDetected = true;
lightAngle = sensorAngle; // Set the target angle of the light servo
sensorPaused = true; // Sensor stops
}
}
// If object detected:
if (objectDetected) {
lightServo.write(lightAngle, 150); // Direct the light to the target
delay(750); // Light servo remains constant
// If the object disappears, the sensor starts moving again.
distance = measureDistance();
if (distance >= 50 || distance == 0) { // Object disappeared
objectDetected = false;
sensorPaused = false; // Sensor moves again
}
}
} else { // Daytime mode
digitalWrite(lightPin, LOW); // Turn off the light
sensorServo.write(90, 150); // Center the sensor
lightServo.write(90, 150); // Center the light
delay(500); // Short wait for system status
}
}
// Function that measures distance
int measureDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Calculate distance (cm)
return distance;
}