I need help with a lighthouse for works in the dark

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;
}

don't you just need one condition instead of sensorPaused and objectDetected/

the code always monitor the distance and if an object is detected, set objectDetected and i think turns on a light

when an object is no longer detected, turn off the light a clear object detected

only sweep the servo when an object is not detected.

if the room light is > 500> set the servos to 90, turn off the light and return

delay() is your enemy! Figure out how to make sure your employees are not taking year long smoke breaks on the clock.

-jim lee

sounds like if you use delay() you're doing it wrong!

The most common cause of servo "stuttering" is inadequate power supply current. How are you powering the servos?
Another possibility is lack of deadband in position comparisions, for instance:

if (ldrValue < 500) 

What happens if ldrValue jitters between 499 and 500?

Post a wiring diagram.

2 Likes

The only stationary position for these servos is in the 90 degree (STOP) position. You would need different servos to "follow movement."

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.