seperate delay from main loop

Hey guys - my first post here so it is a noob one, apologies!

How can I seperate a delay in my script from the main loop? Almost like a seperate thread? I’m sure the code sample below should clarify my problem:

int motorPin = 9;
int lightPin = 0;
int pinB = 1;

void setup()
{
  Serial.begin(9600);
  pinMode(motorPin, OUTPUT);
  pinMode(lightPin, INPUT);
}

void motor()
{
    for(int i = 0; i < 255; i++)
  {
     analogWrite(motorPin, i);
      delay(5); 
  }
  delay(50);
  for(int i = 255; i > 0; i--)
  {
     analogWrite(motorPin, i);
    delay(5); 
  }
  delay(5000);
}

void loop()
{
  motor();
  Serial.write(analogRead(lightPin));
  delay(50);
}

As you can see, the app waits 5 seconds before it sends the lightPin info via the serial bus! I’m not sure how to overcome this and any help will be appreciated, thanks.

Take a look at the Blink Without Delay example.

thanks arrch

that should do the trick