Adafruit TOF sensor, and a MG90S servo problem related to code

Good evening everyone!

i am wondering if any of the bright minds on this forum could please explain why, and possible a solution to my problem.

Keywords: Arduino Servo and TOF sensor - reading angle and Y-distance

The problem: It seems when running my code, when the object is further away the servo trottles down.
i assume it is becouse it's having to wait for the TOF to reach it's destination before going trough next step (moving the servo)

->Is there a way i can have two seperate loops running simultainously?

End product: I am trying to get a sensor (actualy four Vl53L0x) to cover 360 degrees, all on one servo posting angle for each sensor in each quadrant, and Y-reading from the TOF sensor.

the code: this is my first code i am writing for arduino, and using reverse engineering on bits and piceses of information here and there as i am not a programmer. :frowning:

Best regards
/Andreas

The range readings are in units of mm. */

#include <Wire.h>
#include <VL6180X.h>
#include <Servo.h>

VL6180X sensor;
Servo myservo;
int pos = 0 ;

void setup() 
{
  Serial.begin(9600);
  Wire.begin();
  
  sensor.init();
  sensor.configureDefault();
  sensor.setTimeout(500);
    myservo.attach(9); 
    myservo.write(0);
}

 void loop() {
  for (pos = 0; pos <= 100; pos += 2) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    //Serial.print(sensor.readRangeSingleMillimeters());
     Serial.print(sensor.readRangeSingleMillimeters());Serial.print(" mm");Serial.print("   ");Serial.print(pos);Serial.print(" Vinkel/angle");
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(1);                       // waits 15ms for the servo to reach the position
    Serial.println();

  }
  for (pos = 100; pos >= 0; pos -= 2) { // goes from 180 degrees to 0 degrees
    Serial.print(sensor.readRangeSingleMillimeters());Serial.print(" mm");Serial.print("   ");Serial.print(pos);Serial.print(" Vinkel/angle");
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(1);   
     Serial.println();// waits 15ms for the servo to reach the position
  }
}

You have a timeout configured. I suspect that if the sensor doesn't have anything in range it will wait that 500mS before moving on. When it does timeout, you should be seeing 255mm in the serial monitor for range.

Aha that might be the problem, i've missed that line of code! xD

I will try it out in an 30 min time and mark it as solved if it helps! cheers!

wildbill:
You have a timeout configured. I suspect that if the sensor doesn't have anything in range it will wait that 500mS before moving on. When it does timeout, you should be seeing 255mm in the serial monitor for range.

that worked flawlessly :stuck_out_tongue: thanks for the simple but effective information xD