always play in the ground function

what does it represent in Arduino coding
if i want to do a program
-->that every time it have some new value for particular sensor
it stops whatever it`s doing and display a new value ?
what functions can i use ?

i checked the attachInterrupt but the function must take a pin
and i kinda can`t provide this
because the reads are analog and it had to be processed before displaying

Use a variable to hold the last reading; initialize to e.g. -1.

Do a reading and store in a variable. If the reading differs from the last reading
1)
copy current reading to last reading so it is remembered
2)
take the needed action

  1. Don't let the Arduino waste time. Aka, no delay() or Serial parse crap :slight_smile:

sterretje:
Use a variable to hold the last reading; initialize to e.g. -1.

Do a reading and store in a variable. If the reading differs from the last reading
1)
copy current reading to last reading so it is remembered
2)
take the needed action

yes i have applied this part
but there is another actions going in the loop
what i need is if the Arduino happens to be in on of these action and a new read come
stop this action and print the value

but there is another actions going in the loop

You have not posted your code so we cannot see what it is doing.

At a guess you are using the delay() function and/or a for loop and/or a while loop that stops the loop() function running freely so that the sensor can be read frequently and the display updated.

Post your code.

Generally, you don’t use attachInterrupt for reading sensors unless you are dealing with things that happen within milliseconds, or that need to be timed accurately to the microsecond.

If you are doing job A that takes a while, and you need to occasionally handle job B too, then you need to write your code so that it can do a little bit of job A at a time.

void loop() {
  do_a_slice_of_job_A();
  maybe_do_job_B();
}

To accomplish this, you need to keep track of where you are inside job A using some global variables.

Let’s say that job A is “count from 1 to 1000 and print out the numbers”. The simple way to do it is:

void loop() {
  for(int counter = 1; counter<=1000; counter++) {
    Serial.println(counter);
  }
}

But if you want to be able to start and stop the counter, then that loop needs to be unrolled and the state needs to go in some global variables:

int counter = 1;
void loop() {
  do_some_counting();
}

void do_some_counting() {
  if(counter<=1000) {
    Serial.println(counter);
    counter ++;
  }
}

With the job of counting split up like that, then you can write code to start and stop the counting, and conditionally do the actual work:

boolean counting = false;
void main() {
  if(!counting && digitalRead(startCountingPin)==LOW) {
    counting = true;
    counter = 1;
  }
  else
  if(counting && digitalRead(stopCountingPin)==LOW) {
    counting = false;
    Serial.println("STOPPED COUNTING.");
  }

  if(counting) {
    do_some_counting();
  }
}