user4168: I'm new to Arduino and I have two devices that I'm trying to control:

A RGB LED light strip that can change colors
A light sensor that can detect the ambient light level

What I'd like to do is have the LED strip continuously cycle through a rainbow of colors, and the light sensor will adjust the brightness of the LED strip based on the ambient light level. Here are the two pieces of code by themselves, which I've tested in isolation and confirmed that they work properly:
LED strip:
j = (j + 1) % 256; // current iteration of the light cycle

// set the strip color
for(int i=0; i< strip.numPixels(); i++) {
  strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}

// display the new colors and wait 20ms before the next cycle
strip.show();
delay(20);

Light sensor:
sensors_event_t event;
tsl.getEvent(&event); // get a light measurement
strip.setBrightness(event.light); // set the brightness of the LED strip

The problem is that the light sensor takes anywhere from 100ms to 600ms to get a reading, and tsl.getEvent(&event) is a blocking call, so the end result is that the LED strip updates way too slowly. I would need to run the two side by side so that the light sensor read does not block the LED strip update. Any idea how I can accomplish this?

user4169: Setup a 'state server' in your loop(). Put simply this means keeping track of the next command for each task, then doing one thing in each task per loop().
If one task needs to run at a faster speed than another task you can use a timer in your loop to decide what to do next.
If a task needs a pause then save the "Last Run" millis() to a global variable and ignore the task until the correct amount of time has passed. Do not use a delay(), always let the loop() run.
The idea is that the loop() runs as often as possible but you do one thing each time it runs. 
It takes a while to get the hang of this way of working but it leaves the Arduino processor free to process all jobs evenly.
int task1Status=0;
int task2Status=0;

void loop()
{
  doOneThingOfTask1();
  doOneThingOfTask2();
}

//10 things to light the leds, but only do one
void doOneThingOfTask1()
{
  if (task1Status==10)
    task1Status=0;

  switch (task1Status)
  {
    case 0:
       //start doing a new process

    case 1:
       //do something

    case 9:
       //do the last thing

  }
}


user313: Your first, biggest problem is

the light sensor takes anywhere from 100ms to 600ms to get a reading, and tsl.getEvent(&event) is a blocking call,

You need non-blocking access to the light sensor. Look at the library code to see what it is doing when it blocks. Chances are it waits for the sensor to set a 'done' bit. If so, you'll need to modify the library, either by

adding a boolean .isDone() function that tests the done bit, and don't let your main program call for a reading until isDone() == TRUE; or
making .getEvent() a non-blocking such that it immediately returns an obviously invalid reading (such as -1) instead of blocking if the sensor isn't ready yet.

That leaves your main program free to service whatever needs doing and is ready to do without holding up everything for the slow sensor.

user2967: The arduino is not a multithreaded or multi-tasking device. You have to write your different functions to "play nice" with each other. Blocking code is death for this sort of multitasking. You might need to rewrite your code that reads the light sensor to be non-blocking. 
If you can get that code to be non blocking then Visual Micro's post is the way to go.

user2881: You can make the sensor return data faster by lowering it's accuracy.
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */

You can also modify the library to not power-down the sensor after each reading. Just open TSL2561.cpp, find the getFullLuminosity function, and comment out the line reading disable(); and the 12 lines reading switch (_integration){ ... }
Another solution is, to have the pixels update every 20ms using a timer. You can have the timer, interrupt the main loop every 20ms and update the pixel values. 
(I can't really help you with timers, as I myself set the registers for my timers, and ISRs directly, which isn't for beginners. But there are probably libraries for it, to make it more user friendly.)

4168: <p>I'm new to Arduino and I have two devices that I'm trying to control:</p>

<ol>
<li>A RGB LED light strip that can change colors</li>
<li>A light sensor that can detect the ambient light level</li>
</ol>

<p>What I'd like to do is have the LED strip continuously cycle through a rainbow of colors, and the light sensor will adjust the brightness of the LED strip based on the ambient light level. Here are the two pieces of code by themselves, which I've tested in isolation and confirmed that they work properly:</p>

<p>LED strip:</p>

<pre><code>j = (j + 1) % 256; // current iteration of the light cycle

// set the strip color
for(int i=0; i&lt; strip.numPixels(); i++) {
  strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) &amp; 255));
}

// display the new colors and wait 20ms before the next cycle
strip.show();
delay(20);
</code></pre>

<p>Light sensor:</p>

<pre><code>sensors_event_t event;
tsl.getEvent(&amp;event); // get a light measurement
strip.setBrightness(event.light); // set the brightness of the LED strip
</code></pre>

<p>The problem is that the light sensor takes anywhere from 100ms to 600ms to get a reading, and <code>tsl.getEvent(&amp;event)</code> is a blocking call, so the end result is that the LED strip updates way too slowly. I would need to run the two side by side so that the light sensor read does not block the LED strip update. Any idea how I can accomplish this?</p>
 4169: <p>Setup a 'state server' in your loop(). Put simply this means keeping track of the next command for each task, then doing one thing in each task per loop().</p>

<p>If one task needs to run at a faster speed than another task you can use a timer in your loop to decide what to do next.</p>

<p>If a task needs a pause then save the "Last Run" millis() to a global variable and ignore the task until the correct amount of time has passed. Do not use a delay(), always let the loop() run.</p>

<p>The idea is that the loop() runs as often as possible but you do one thing each time it runs. </p>

<p>It takes a while to get the hang of this way of working but it leaves the Arduino processor free to process all jobs evenly.</p>

<pre><code>int task1Status=0;
int task2Status=0;

void loop()
{
  doOneThingOfTask1();
  doOneThingOfTask2();
}

//10 things to light the leds, but only do one
void doOneThingOfTask1()
{
  if (task1Status==10)
    task1Status=0;

  switch (task1Status)
  {
    case 0:
       //start doing a new process

    case 1:
       //do something

    case 9:
       //do the last thing

  }
}
</code></pre>
 313: <p>Your first, biggest problem is</p>

<blockquote>
  <p>the light sensor takes anywhere from 100ms to 600ms to get a reading, and tsl.getEvent(&amp;event) is a blocking call,</p>
</blockquote>

<p>You need non-blocking access to the light sensor. Look at the library code to see what it is doing when it blocks. Chances are it waits for the sensor to set a 'done' bit. If so, you'll need to modify the library, either by</p>

<ol>
<li>adding a boolean <code>.isDone()</code> function that tests the done bit, and don't let your main program call for a reading until isDone() == TRUE; or</li>
<li>making .getEvent() a non-blocking such that it immediately returns an obviously invalid reading (such as -1) instead of blocking if the sensor isn't ready yet.</li>
</ol>

<p>That leaves your main program free to service whatever needs doing and is ready to do without holding up everything for the slow sensor.</p>
 2967: <p>The arduino is not a multithreaded or multi-tasking device. You have to write your different functions to "play nice" with each other. Blocking code is death for this sort of multitasking. You might need to rewrite your code that reads the light sensor to be non-blocking. </p>

<p>If you can get that code to be non blocking then Visual Micro's post is the way to go.</p>
 2881: <p>You can make the sensor return data faster by lowering it's accuracy.</p>

<pre><code>tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
</code></pre>

<p>You can also modify the library to not power-down the sensor after each reading. Just open <code>TSL2561.cpp</code>, find the <code>getFullLuminosity</code> function, and comment out the line reading <code>disable();</code> and the 12 lines reading <code>switch (_integration){ ... }</code></p>

<p>Another solution is, to have the pixels update every 20ms using a <a href="http://playground.arduino.cc/code/timer1" rel="noreferrer">timer</a>. You can have the timer, interrupt the main loop every 20ms and update the pixel values. </p>

<p>(I can't really help you with timers, as I myself set the registers for my timers, and ISRs directly, which isn't for beginners. But there are probably libraries for it, to make it more user friendly.)</p>
