Go Down

Topic: Code For Lidar Lite V3 Using Arduino (Read 125 times) previous topic - next topic

BM1159

Can anybody help me with my code to get my lidar lite v3 to flash led at different rates/ frequencies as the distance decreases.I am in need of help because the project deadline is soon aproching and I would be ever so greatful if someone would give me a hand

Code: [Select]


unsigned long pulseWidth;
#define LED 13

void setup() {
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
  pinMode(3, INPUT);
  pinMode(LED, OUTPUT);
  #define distance (pulseWidth)
    if(pulseWidth != 0)
  {
    pulseWidth = pulseWidth / 10;
    (distance <= 0);
    distance=0;}
 
}

void loop() {
{
  pulseWidth = pulseIn(3, HIGH); 
}

   if (distance=0)
    digitalWrite (LED,HIGH);
   
 
  else if (distance >= 3 && distance <= 50){
      digitalWrite (LED, HIGH);
    delay(50);
    digitalWrite (LED, LOW);
    delay(50);
  }
  else if (distance >= 51 && distance <= 100){
      digitalWrite (LED, HIGH);
    delay(100);
    digitalWrite (LED, LOW);
    delay(100);
  }
  else if (distance >= 101 && distance <= 200){
      digitalWrite (LED, HIGH);
    delay(200);
    digitalWrite (LED, LOW);
    delay(200);
  }
    else if (distance >= 201 && distance <= 300){
      digitalWrite (LED, HIGH);
    delay(300);
    digitalWrite (LED, LOW);
    delay(300);
  }
    else if (distance >= 301 && distance <= 400){
      digitalWrite (LED, HIGH);
    delay(400);
    digitalWrite (LED, LOW);
    delay(400);
  }
      else if (distance >= 400) {
      digitalWrite (LED, HIGH);
    delay(500);
    digitalWrite (LED, LOW);
    delay(500);
  }
  delay(50);
  }


PaulS

Code: [Select]
  #define distance (pulseWidth)
What is this trying to do? What is the relationship between pulseWidth, that you have not given a meaningful value to, and distance?

Code: [Select]
    if(pulseWidth != 0)
But, it is...

Code: [Select]
    (distance <= 0);
Compare distance to 0, but do nothing if it is less than 0 and do nothing if it is greater than 0. So, why bother comparing?

Code: [Select]
{
  pulseWidth = pulseIn(3, HIGH);
}

Useless curly braces.

Code: [Select]
   if (distance=0)
Some time studying the difference between the = and the == operators would be time well spent.

Since it appears that you want the LED on time and off time to be the same as the distance, why do you need so much code to make that happen?
The art of getting good answers lies in asking good questions.

BM1159

sorry Im only 14 and new to arduino software and Im just trying to  programe the sensor to flash an LED more rapidly as distance decreases but i am not having much sucsess while doing so. I would be greatful if you (PaulS) could put me on the path regards to making my code work

dannable

PaulS has pointed out where there are mistakes. He's also given you fairly big hints about what is wrong. Look at those lines and check your syntax?
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

BM1159

Just finished my first working prototype . If you have any suggestions please leave a reply
Code: [Select]
unsigned long pulseWidth;

void setup()
{
  Serial.begin(115200); // Start serial communications
 
  pinMode(2, OUTPUT); // Set pin 2 as trigger pin
  digitalWrite(2, LOW); // Set trigger LOW for continuous read

  pinMode(3, INPUT); // Set pin 3 as monitor pin

  pinMode(13, OUTPUT);
}

void loop()
{
  pulseWidth = pulseIn(3, HIGH); // Count how long the pulse is high in microseconds
  pulseWidth = pulseWidth / 10;
  // If we get a reading that isn't zero, let's print it
  if(pulseWidth >= 51 && pulseWidth <= 100)
  {
     // 10usec = 1 cm of distance
    Serial.println(pulseWidth); // Print the distance

  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(100);
  }
  else if(pulseWidth >= 10 && pulseWidth <= 50)
  {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(50);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(50);
  }
}

Go Up