How to program this.

Hi guys,

New to microcontrollers, not to programming; though it’s been a few years. Below you’ll see some sample code I got for a Lidar Lite I’m using to get a read. It is constantly getting the distance, what I want to do is put in an if then statement such as if “get data” is typed it goes to get the data and displays it. I than want to program the file to save data upon exit as a text file.

/*

http://pulsedlight3d.com

This sketch demonstrates getting distance with the LIDAR-Lite Sensor

It utilizes the ‘Arduino I2C Master Library’ from DSS Circuits:

You can find more information about installing libraries here:

*/

#include <I2C.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.

void setup(){
Serial.begin(9600); //Opens serial connection at 9600bps.
I2c.begin(); // Opens & joins the irc bus as master
delay(100); // Waits to make sure everything is powered up before sending or receiving data
I2c.timeOut(50); // Sets a timeout to ensure no locking up of sketch if I2C communication fails
}

void loop(){
// Write 0x04 to register 0x00
uint8_t nackack = 100; // Setup variable to hold ACK/NACK resopnses
while (nackack != 0){ // While NACK keep going (i.e. continue polling until sucess message (ACK) is received )
nackack = I2c.write(LIDARLite_ADDRESS,RegisterMeasure, MeasureValue); // Write 0x04 to 0x00
delay(1); // Wait 1 ms to prevent overpolling
}

byte distanceArray[2]; // array to store distance bytes from read function

// Read 2byte distance from register 0x8f
nackack = 100; // Setup variable to hold ACK/NACK resopnses
while (nackack != 0){ // While NACK keep going (i.e. continue polling until sucess message (ACK) is received )
nackack = I2c.read(LIDARLite_ADDRESS,RegisterHighLowB, 2, distanceArray); // Read 2 Bytes from LIDAR-Lite Address and store in array
delay(1); // Wait 1 ms to prevent overpolling
}
int distance = (distanceArray[0] << 8) + distanceArray[1]; // Shift high byte [0] 8 to the left and add low byte [1] to create 16-bit int

// Print Distance
Serial.println(distance);
}

Please read the two posts at the top of this Forum for guidelines on posting here, especially when posting code. Also, use Ctrl-T in the IDE to reformat the code into a standard format before copying and posting here. It will help us help you.

Alright,

I went ahead and read those two posts as you asked, I’ll try to my best in going with them.

What I want this program to do: I want to a user (me) to enter a number into a window and have that many measurements taken and than have each measurement displayed on a screen. Below is the current code I am using, supplied by Pulsedlight for their Lidar Lite module.

I put in bold the part I want to add / change.
/*

http://pulsedlight3d.com

This sketch demonstrates getting distance with the LIDAR-Lite Sensor

It utilizes the ‘Arduino I2C Master Library’ from DSS Circuits:

You can find more information about installing libraries here:

*/

#include <I2C.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.

void setup() {
Serial.begin(9600); //Opens serial connection at 9600bps.
I2c.begin(); // Opens & joins the irc bus as master
delay(100); // Waits to make sure everything is powered up before sending or receiving data
I2c.timeOut(50); // Sets a timeout to ensure no locking up of sketch if I2C communication fails
}

void loop() {
// Write 0x04 to register 0x00

uint8_t nackack = 100; // Setup variable to hold ACK/NACK resopnses
while (nackack != 0) { // While NACK keep going (i.e. continue polling until sucess message (ACK) is received )
nackack = I2c.write(LIDARLite_ADDRESS, RegisterMeasure, MeasureValue); // Write 0x04 to 0x00
delay(1); // Wait 1 ms to prevent overpolling
}

byte distanceArray[2]; // array to store distance bytes from read function

// Read 2byte distance from register 0x8f
nackack = 100; // Setup variable to hold ACK/NACK resopnses
while (nackack != 0) { // While NACK keep going (i.e. continue polling until sucess message (ACK) is received )
nackack = I2c.read(LIDARLite_ADDRESS, RegisterHighLowB, 2, distanceArray); // Read 2 Bytes from LIDAR-Lite Address and store in array
delay(1); // Wait 1 ms to prevent overpolling
}
int distance = (distanceArray[0] << 8) + distanceArray[1]; // Shift high byte [0] 8 to the left and add low byte [1] to create 16-bit int

// Print Distance

int numberOfDataPoints //get the number of data points to display
Get user input for the number of distances to measure
int conversion = distance / (2.54) /(12) // converting 2.54 CM for 1 inch and 12 inch for one foot.
int count = 0
for (count = 0; i <= numberOfDataPoints; count++)
** {**
** Serial.println(conversion);**
** }**

}

sketch_may05a.ino (2.07 KB)

Alright,

I went ahead and read those two posts as you asked, I'll try to my best in going with them.

I'm sorry, dude, but it's very obvious that you didn't.

Oh! My mistake! The important bit is actually the third one. There are three sticky posts. Read the thirs one, especially point 6.

Which one's there's no link?

Read the two by Nick Gammon at the top of this Forum.

Thanks.