SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By NobleF
#198403
Hello

I'm in the early stages of what will hopefully be a fairly large project involving a python program receiving data from sensors connected to an Arduino, interpreting that data and then controlling several relays and stepper motors via the Arduino. I currently have a code that performs several analogreads and prints to serial every 10 ms.
At this point is where the problem begins. The next thing I want to include is a SparkFun AS7263 near infrared spectral sensor. I first tried simply including the takeMeasurements and printMeasurements functions included with the supplied library. This slowed my loop from 100Hz to 2 Hz. I've studied the library code and tried reading the data sheet some, and have searched some online, and have come to the conclusion that this is because of at least 2 things, First is the way the sensor works, taking measurements over a period of time and outputting an integration of the measurements over that time. The default setting for the this time is a value of 50, when I reduce that value to 1, the sensor will output a measurement approximately every 67ms, better than the 500 ms at the default but still slowing down the rest of my code too much to be acceptable. Second is that the takeMeasurements function is blocking code and stops my other sensors from updating while it is running.

My questions are these: first, do these conclusions sound correct?
second: Is it possible to modify the AS726X library to allow the Arduino to tell the sensor to collect data for as long as it wants and print it to the serial once its ready, while my other sensors are humming away at their 10ms pace? Essentially multitasking the Arduino. I've done some work with an object oriented code and have made it work in the sense that it will output the data from the other sensors but when it comes to the AS7263 it stops for the 67 ms, or 350 ms, or 500 ms depending on the integration time and reads the AS7263 then starts in on my other sensors again after this long delay has passed.
Third: I have looked through the AS763X library and don't entirely understand how it works the takeMeasurements function is this:
Code: Select all
void AS726X::takeMeasurements()
{
	clearDataAvailable(); //Clear DATA_RDY flag when using Mode 3

						  //Goto mode 3 for one shot measurement of all channels
	setMeasurementMode(3);

	//Wait for data to be ready
	while (dataAvailable() == false) delay(POLLING_DELAY);

	//Readings can now be accessed via getViolet(), getBlue(), etc
}
and I don't understand what part of this is reading the registers on the sensor. I assume this is how the Arduino receives actual measurements from the sensor? Would it be possible to simply read the registers directly in my code at a rate of 100Hz and then do any necessary calculations or combinations within my python program that is analyzing the sensor outputs anyways?

Fourth: Would I just be better off to get a second Arduino and let it run at whatever speed is necessary for the AS7263 and keep my other sensors running at 100Hz on the first Arduino?

I'm sorry if this post is too long but I'm trying to give a detailed account of what I'm trying to accomplish and the steps I've already explored. Thank you for any help or clarification that is forthcoming.
By sterretje
#198774
It's probably easy to hack that method so it's non-blocking. I did not look at the library, just based on the snippet that you gave. You need to check if takeMeasurement is used anywhere else in the library.
Code: Select all
bool AS726X::takeMeasurements(bool start)
{
  if (start == true)
  {
    clearDataAvailable(); //Clear DATA_RDY flag when using Mode 3

    //Goto mode 3 for one shot measurement of all channels
    setMeasurementMode(3);
  }

  return dataAvailable();
}
You will need to find the the function prototype in the .h file and modify it to match the first line in above.

And e.g. in loop()
Code: Select all
void loop()
{
  // remember if a measurement is started or not
  static bool started = false;
  // result of calling takeMeasurement
  bool measurementComplete;
  
  if(started == false)
  {
    // indicate started
    started = true;
    // start measurement
    measurementComplete = xx.takeMeasurement(true);
  }
  else
  {
    // check if measurement complete
    measurementComplete = xx.takeMeasurement(false);
  }

  if(measurementComplete == true)
  {
    measurementComplete = false;
    started = false;

    // process the info
    ...
    ...
  }
}
I'm not very active on this forum so you'll have to do it without my help after this. Just hoping to give you the idea.