Making a provision that the speed of readings of 2 different sensors is the same

I was just trying to find a code or a resource on Arduino to make a provision that the speed of the readings of 2 different types of sensors is the same,could some help me out with it ?

could some help me out with it ?

Not until you define which two sensors you are talking about, and what you mean by "the speed of the readings".

If you are trying to force a slow reading sensor, like a temperature sensor, to cough up a reading as fast as a phototransistor, you can forget it.

If you want to read two sensors at the same frequency, that's pretty simple. Every time you read one, read the other one.

I am using lidar lite v3 and IMU BNO055

And about the speed of the readings I mean that the number of readings in a particular period of time is the same

And the desired (hoped for) speed is?

You only read the sensors less often than the slower one, leaving cycles to process and output your data is how.

For timing, go learn the built-in to your IDE Example named BlinkWithoutDelay to start. It teaches a valuable lesson.

This is the code I am working on

#include <LIDARLite.h>
#include <LIDARLite_v3HP.h>

LIDARLite lidarLite;
int cal_cnt = 0;
#include <EEPROM.h>
int addr = 0;
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>

Adafruit_BNO055 bno = Adafruit_BNO055(55);

void setup()
{
Serial.begin(9600); // Initialize serial connection to display distance readings

lidarLite.begin(0, true); // Set configuration to default and I2C to 400 kHz
lidarLite.configure(0); // Change this number to try out alternate configurations
Serial.begin(9600);
Serial.println(“Orientation Sensor Test”); Serial.println("");

/* Initialise the sensor */
if(!bno.begin())

{
/* There was a problem detecting the BNO055 … check your connections */
Serial.print(“Ooops, no BNO055 detected … Check your wiring or I2C ADDR!”);
while(1);
}

delay(1000);

bno.setExtCrystalUse(true);
}

void loop()
{
int dist;

// At the beginning of every 100 readings,
// take a measurement with receiver bias correction
if ( cal_cnt == 0 ) {
dist = lidarLite.distance(); // With bias correction
} else {
dist = lidarLite.distance(false); // Without bias correction
}

// Increment reading counter
cal_cnt++;
cal_cnt = cal_cnt % 100;

// Display distance
Serial.print(dist);
Serial.println(" cm");

delay(10);

/***
Need to divide by 4 because analog inputs range from
0 to 1023 and each byte of the EEPROM can only hold a
value from 0 to 255.
***/

int val = analogRead(0) / 4;

/***
Write the value to the appropriate byte of the EEPROM.
these values will remain there when the board is
turned off.
***/

EEPROM.write(addr, val);

/***
Advance to the next address, when at the end restart at the beginning.

Larger AVR processors have larger EEPROM sizes, E.g:

  • Arduno Duemilanove: 512b EEPROM storage.
  • Arduino Uno: 1kb EEPROM storage.
  • Arduino Mega: 4kb EEPROM storage.

Rather than hard-coding the length, you should use the pre-provided length function.
This will make your code portable to all AVR processors.
***/
addr = addr + 1;
if (addr == EEPROM.length()) {
addr = 0;
}

/***
As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
EEPROM address is also doable by a bitwise and of the length - 1.

++addr &= EEPROM.length() - 1;
***/

delay(100);
/* Get a new sensor event */
sensors_event_t event;
bno.getEvent(&event);

/* Display the floating point data */
Serial.print(“X: “);
Serial.print(event.orientation.x, 4);
Serial.print(”\tY: “);
Serial.print(event.orientation.y, 4);
Serial.print(”\tZ: “);
Serial.print(event.orientation.z, 4);
Serial.println(””);

delay(100);

}

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor. The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.

...R

joyagrawal:
This is the code I am working on

It does need to be put inside of code tags so the forum html won't interpret code symbols asnd look like crap.

To get a regular number of reads per second for any 1 sensor you put it inside of an if( timer runs out ) { -code- }

unsigned long startTimer[ 4 ];  //  I want to time reading 4 sensors, each has a start and interval
unsigned long waitTimer[ 4 ] = { 100, 250, 500, 1000 };

byte sensePin[ 4 ] = { A0, A1, A2, A3 };
int    senseVal[ 4 ];
byte whichSense; // default is 0

..............

void loop()
{


  if ( millis() - startTimer[ whichSense ] >= waitTime[ whichSense ] )  // this is a task on a timer, 1 code runs many sensors
  {
    senseVal[ whichSense ] = analogRead( sensePin[ whichSense ] );
    // print or do something here

    startTimer[ whichSense ] += waitTime[ whichSense ];  // reset start 
    if ( ++whichSense >= 4 )   whichSense = 0; // the ++ before whichSense adds 1 before the >= 4 happens.
  }


  // you can put task after task inside of loop() to run on time or on pin state change or on a variable set in some other task.
}

Quote this post to see what code tags look like as editor text.