user15699: I'm using the grove GSR sensor: http://www.seeedstudio.com/wiki/Grove_-_GSR_Sensor with this code:
const int GSR=A0;
int threshold=0;
int sensorValue;
const long interval = 1000;  
void setup(){
  long sum=0;
  Serial.begin(9600);
  delay(1000);

  for(int i=0;i<500;i++)
  {
  sensorValue=analogRead(GSR);
  sum += sensorValue;
  delay(5);
  }
  threshold = sum/500;
   Serial.print("threshold =");
   Serial.println(threshold);
  }

void loop(){
  int temp;
  sensorValue=analogRead(GSR);
  Serial.println(sensorValue);
  temp = threshold - sensorValue;
  delay(250);
  }

There is the delay way to skip milliseconds or the millis() function. Do you have ideas on how can I implement it? The delay has the problem that doesn't consider the time the sensor uses to read the data.
Here's what I am trying
const int GSR=A0;
int threshold=0;
int sensorValue;
const long interval = 1000; 
unsigned long startTime=millis();


void setup(){
  long sum=0;
  Serial.begin(9600);
  delay(1000);

  for(int i=0;i<500;i++)
  {
  sensorValue=analogRead(GSR);
  sum += sensorValue;
  delay(5);
  }
  threshold = sum/500;
   Serial.print("threshold =");
   Serial.println(threshold);
  }

void loop(){


  if((millis() - startTime) % 250 == 0) {
   int temp;
  sensorValue=analogRead(GSR);
  Serial.println(sensorValue);
  temp = threshold - sensorValue;

  }
  }


user13995: The difference between your first code and the second one is that the first one is blocking execution when it reaches the delay function, but the second code keeps running without being blocked.
Now your algorithm has a drawback, the loop may or may not reach the if condition exactly when the modulo is strictly equal to 0. So you may prefer to use something like :
unsigned long T = millis();

...

if( millis() - T >= 250 ) {
    ... // sensor stuff
    T = millis();
}

This idea is also presented in this article https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay.
Another approach is to use Timers and Interrupts, but this is far more complicated (http://playground.arduino.cc/Code/Timer1).

user10214: Both other answers are correct, but my usual way of dealing with this is
unsigned long startTime = 0;
#define MEASURE_PERIOD 250
...
void loop()
{
    if((millis() - startTime) >= MEASURE_PERIOD)
    {
        startTime += MEASURE_PERIOD;
        // Do your job, such as
        int temp;
        sensorValue=analogRead(GSR);
        Serial.println(sensorValue);
        temp = threshold - sensorValue;
    }
}

The difference between this way and the other ones (Nick's way, i.e. saving millis and setting the startTime variable to it, or nico's one - which is worse - , to set it again), is that this way if there is some error (e.g. 260ms between two calls) it gets recovered by the next cycle (which will be of 240ms), while in their way it is not.

15699: <p>I'm using the grove GSR sensor: <a href="http://www.seeedstudio.com/wiki/Grove_-_GSR_Sensor" rel="nofollow">http://www.seeedstudio.com/wiki/Grove_-_GSR_Sensor</a> with this code:</p>

<pre><code>const int GSR=A0;
int threshold=0;
int sensorValue;
const long interval = 1000;  
void setup(){
  long sum=0;
  Serial.begin(9600);
  delay(1000);

  for(int i=0;i&lt;500;i++)
  {
  sensorValue=analogRead(GSR);
  sum += sensorValue;
  delay(5);
  }
  threshold = sum/500;
   Serial.print("threshold =");
   Serial.println(threshold);
  }

void loop(){
  int temp;
  sensorValue=analogRead(GSR);
  Serial.println(sensorValue);
  temp = threshold - sensorValue;
  delay(250);
  }
</code></pre>

<p>There is the delay way to skip milliseconds or the <code>millis()</code> function. Do you have ideas on how can I implement it? The delay has the problem that doesn't consider the time the sensor uses to read the data.</p>

<p>Here's what I am trying</p>

<pre><code>const int GSR=A0;
int threshold=0;
int sensorValue;
const long interval = 1000; 
unsigned long startTime=millis();


void setup(){
  long sum=0;
  Serial.begin(9600);
  delay(1000);

  for(int i=0;i&lt;500;i++)
  {
  sensorValue=analogRead(GSR);
  sum += sensorValue;
  delay(5);
  }
  threshold = sum/500;
   Serial.print("threshold =");
   Serial.println(threshold);
  }

void loop(){


  if((millis() - startTime) % 250 == 0) {
   int temp;
  sensorValue=analogRead(GSR);
  Serial.println(sensorValue);
  temp = threshold - sensorValue;

  }
  }
</code></pre>
 13995: <p>The difference between your first code and the second one is that the first one is blocking execution when it reaches the <code>delay</code> function, but the second code keeps running without being blocked.</p>

<p>Now your algorithm has a drawback, the loop may or may not reach the <code>if</code> condition exactly when the modulo is strictly equal to 0. So you may prefer to use something like :</p>

<pre><code>unsigned long T = millis();

...

if( millis() - T &gt;= 250 ) {
    ... // sensor stuff
    T = millis();
}
</code></pre>

<p>This idea is also presented in this article <a href="https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay" rel="nofollow">https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay</a>.</p>

<p>Another approach is to use Timers and Interrupts, but this is far more complicated (<a href="http://playground.arduino.cc/Code/Timer1" rel="nofollow">http://playground.arduino.cc/Code/Timer1</a>).</p>
 10214: <p>Both other answers are correct, but my usual way of dealing with this is</p>

<pre><code>unsigned long startTime = 0;
#define MEASURE_PERIOD 250
...
void loop()
{
    if((millis() - startTime) &gt;= MEASURE_PERIOD)
    {
        startTime += MEASURE_PERIOD;
        // Do your job, such as
        int temp;
        sensorValue=analogRead(GSR);
        Serial.println(sensorValue);
        temp = threshold - sensorValue;
    }
}
</code></pre>

<p>The difference between this way and the other ones (Nick's way, i.e. saving <code>millis</code> and setting the <code>startTime</code> variable to it, or nico's one - which is worse - , to set it again), is that this way if there is some error (e.g. 260ms between two calls) it gets recovered by the next cycle (which will be of 240ms), while in their way it is not.</p>
