Control a Servo Using Nicla Vision's Distance Detection

Hardware:

  • Arduino IDE Version 2.2.1 for Mac
  • Nicla Vision with (ToF) sensor VL53L1X
  • Servo

My goal is to control a servo depending on the distance.
Example: At a distance of 40 mm, the servo moves to 0°; at 4000 mm, the servo moves to 180°.

I have now uploaded the complete sketch for distance detection with Arduino Nicla Vision to Nicla.
You can find an example from this tutorial here:
[https://docs.arduino.cc/tutorials/nicla-vision/proximity]
Result: OK - the LED now flashes depending on the distance.

Servo control with Nicla Vision:
Regarding the control I found the following helpful forum post:
[Controlling servos with Nicla Vision - #2 by Grumpy_Mike]

In the Arduino IDE:
I select -> Board -> Arduino Mbed OS Nicla boards -> Nicla Vision
Then I find the servo example under File -> Examples -> Servo -> Sweep

My questions:

  • How do I integrate this servo sketch into the Nicla Vision sketch mentioned above?
  • Does anyone have experience with this and can kindly help me?

Many thanks in advance.

...and here is the sketch for distance detection with Arduino Nicla Vision:

#include "VL53L1X.h"
VL53L1X proximity;

bool blinkState = false;
int reading = 0;
int timeStart = 0;
int blinkTime = 2000;

void setup() {
  Serial.begin(115200);
  Wire1.begin();
  Wire1.setClock(400000); // use 400 kHz I2C
  proximity.setBus(&Wire1);


  pinMode(LEDB, OUTPUT);
  digitalWrite(LEDB, blinkState);

  if (!proximity.init()) {
    Serial.println("Failed to detect and initialize sensor!");
    while (1);
  }

  proximity.setDistanceMode(VL53L1X::Long);
  proximity.setMeasurementTimingBudget(10000);
  proximity.startContinuous(10);
}

void loop() {
  reading = proximity.read();
  Serial.println(reading);

  if (millis() - timeStart >= reading) {
    digitalWrite(LEDB, blinkState);
    timeStart = millis();

    blinkState = !blinkState;
  }
}

...and here the sketch for the servo:

/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
}

How can I merge these two sketches?

In the meantime, I have tried the attached script with the help of GTP.
The servo only jerks and does not do what it should:
Namely, depending on the distance, it swivels back and forth between 0 and 180 degrees.
Can anyone help me?

#include "VL53L1X.h"
#include <Servo.h>

VL53L1X proximity;
Servo myservo;

bool blinkState = false;
int reading = 0;
int timeStart = 0;
int blinkTime = 2000;

void setup() {
  Serial.begin(115200);
  Wire1.begin();
  Wire1.setClock(400000); // use 400 kHz I2C
  proximity.setBus(&Wire1);

  pinMode(LEDB, OUTPUT);
  digitalWrite(LEDB, blinkState);

  myservo.attach(9); // attaches the servo on pin 9 to the servo object

  if (!proximity.init()) {
    Serial.println("Failed to detect and initialize sensor!");
    while (1);
  }

  proximity.setDistanceMode(VL53L1X::Long);
  proximity.setMeasurementTimingBudget(10000);
  proximity.startContinuous(10);
}

void loop() {
  reading = proximity.read();
  Serial.println(reading);

  if (millis() - timeStart >= reading) {
    digitalWrite(LEDB, blinkState);
    timeStart = millis();

    blinkState = !blinkState;

    // Servo-Steuerung basierend auf der Entfernung
    int servoPosition = map(reading, 40, 4000, 0, 180);
    
    // Erhöhen Sie die Schritte beim Durchlaufen der Servo-Positionen
    for (int pos = myservo.read(); pos <= servoPosition; pos += 2) {
      myservo.write(pos);
      delay(50); // Verzögerung erhöhen
    }

    for (int pos = myservo.read(); pos >= servoPosition; pos -= 2) {
      myservo.write(pos);
      delay(50); // Verzögerung erhöhen
    }
  }
}

Show how your project is wired. Power source is often the issue with motor problems.