Issue with Arduino, LIDAR-Lite and buzzer

Sorry I am new in Arduino and too old man (55). About the lite laser ringe finder I include a buzzer but not use a servo and serial monitor count till 180 and alarm began
I want to detect movement and sign…I´m going crazy. Can you help me?. Thanks in advance.
The code that I use is:

#include <Wire.h>

#define    LIDARLite_ADDRESS   0x62          // Default I2C Address of LIDAR-Lite.
#define    RegisterMeasure     0x00          // Register to write to initiate ranging.
#define    MeasureValue        0x04          // Value to initiate ranging.
#define    RegisterHighLowB    0x8f          // Register to get both High and Low bytes in 1 call.
#define    Buzzer 2

#include <Servo.h>

Servo myservo;

int pos = 0;         // Position of the servo (degress, [0, 180])
int distance = 0;    // Distance measured
int alertDistance = 200;

void setup()
{
  // Serial output
  Serial.begin(9600);
  Serial.println("< START >");
  
  // Servo control
  myservo.attach(5);
  
  // LIDAR control
  Wire.begin(); // join i2c bus
  
  pinMode(Buzzer, OUTPUT);
}

// Get a measurement from the LIDAR Lite
int lidarGetRange(void)
{
  int val = -1;
  
  Wire.beginTransmission((int)LIDARLite_ADDRESS); // transmit to LIDAR-Lite
  Wire.write((int)RegisterMeasure); // sets register pointer to  (0x00)  
  Wire.write((int)MeasureValue); // sets register pointer to  (0x00)  
  Wire.endTransmission(); // stop transmitting

  delay(20); // Wait 20ms for transmit

  Wire.beginTransmission((int)LIDARLite_ADDRESS); // transmit to LIDAR-Lite
  Wire.write((int)RegisterHighLowB); // sets register pointer to (0x8f)
  Wire.endTransmission(); // stop transmitting

  delay(20); // Wait 20ms for transmit
  
  Wire.requestFrom((int)LIDARLite_ADDRESS, 2); // request 2 bytes from LIDAR-Lite

  if(2 <= Wire.available()) // if two bytes were received
  {
    val = Wire.read(); // receive high byte (overwrites previous reading)
    val = val << 8; // shift high byte to be high 8 bits
    val |= Wire.read(); // receive low byte as lower 8 bits
  }
  
  return val;
}

void serialPrintRange(int pos, int distance)
{
    Serial.print("Position (deg): ");
    Serial.print(pos);
    Serial.print("\t\tDistance (cm): ");
    Serial.println(distance);
}

void loop()
{
  for(pos = 0; pos <= 180; pos += 1)
  {
    
    distance = lidarGetRange();
    serialPrintRange(pos, distance);
    delay(20);
  }
  for(pos = 180; pos>=0; pos-=1)
  {
    myservo.write(pos);
    distance = lidarGetRange();
    serialPrintRange(pos, distance);
    delay(20);
    if (distance <= alertDistance){
      serialPrintRange(pos, distance);   
      Serial.print("ALERT");
         digitalWrite(Buzzer, HIGH);
         delay (20);
         digitalWrite(Buzzer, LOW);
     }
     
}


    
  }

So sorry.
I´m not use servo but included it to future projects.

I´m fairly and clumsy. But I began to study Arduino one year ago :frowning:

Hey!

That’s great! Everyone can learn Arduino at any age! :slight_smile: As long as you are curious and dedicated you should have a fun ride into the world of embedded software & systems! :smiley:

If I get this properly you wish for the buzzer to sound whenever you detect an object at a certain distance?

First, I recommend that you remove any code that you are not using. If you do not have a servo in this iteration of the project you should remove all servo related code such as:

#include <Servo.h>
Servo myservo;
int pos = 0;
myservo.attach(5);
myservo.write(pos);

You should probably also remove:

Serial.print("Position (deg): ");
Serial.print(pos);
for(pos = 0; pos <= 180; pos += 1)
for(pos = 180; pos >= 0; pos -= 1)

And just add a single delay(???); at the end of the “void loop()” function instead of the two for loops that are currently there.

Once you’ve removed all not needed code it should be much easier to get a clear picture of what needs to be modified for your code to perform the intended purpose of your project!

Since Arduino is basically a pre-processor with libraries for C/C++, I’d recommend that you also look at some C/C++ tutorials to help you out understand the fundamentals better. Maybe have a look at this? It could certainly help with any gaps in your knowledge. Most difficulties in programming come from those gaps (unknown unknown knowledge).

Sincerely,

P.-S.: I moved your posts to their own topic for simplicity. Also, I added [ code ] & [ /code ] tags to your post to make it more readable.