pin going high not allowing analog read to change I have the following problem: 

My arduino takes analogRead(A0) from a potentiometer whose position I can't adjust directly.

When analogRead(AO) goes to a value X,  I want pin 8 to go HIGH, this causes the circuit to open. and stop A0 getting higher . so I do an if statement: 

if(analogRead(AO)&gt; X) {

digitalWrite(pin, HIGH)  \\opens the circuit} 

else {
digitalWrite(pin, LOW)  \close the circuit
 }


The issue I have here, is when then circuit is open,  I would like to change the value of the potentiometer (A0) which is controlled by a motor which is also in the circuit: but doesnt depend on the arduino board.  Given I can't control A(0) then the circuit will remain on A(0) >X forever.  

The system going to LOW pin or close circuit, after it reaches HIGH pin for 2 seconds would be great, but then the program goes to the beginning and realizes that still A(0)> X , and goes back to a open circuit state, therefore not allowing me to have the close circuit anymore in order to make A(0) &lt; X  and leave the circuit closed . Maybe something that cyclically turns pin on an off for t mseconds? but this is not ideal as it would cause high wear on the system

Edited the post to be more precise. I hope now its clearer
thanks



thank you for your answers, but after playing for a while with your codes, and the ideas behind it, still cannot get it to work. 

for instance, Chrisl with your first:

when potentiometer is at > X, It cuts the circuit which is good, but still doesn't go off until both potentiometer goes to &lt; X and 3 seconds lapse , but this is not going to be possible to achieve anyways    because the value of the potentiometer is fixed on X 

THis is what Id need- 

if (PinState == HIGH)

after 3 seconds go low, 

and not turn HIGH until at least these two conditions are met; analogRead has gone to &lt; X and then analogRead > X ,and in that moment we are back at
PinState HIGH
is  even possible to achieve something like this?

the circuit is something like this:






simulate this circuit &ndash; Schematic created using CircuitLab

UPDATE: 
I have using this code, which does almost what I want: 
         if(analogRead(A0) > X) {
      unsigned long currentMillis = millis();

if ((unsigned long)(currentMillis - previousMillis) &gt;= interval) {

   pinState = !pinState; 
   digitalWrite(pin, pinState)
   // save the "current" time
   previousMillis = millis();
}}


When it gets to analogRead(A0)> X the problem of this code is that it switches the pin On and OFF continuosly,

but instead of switching it off/on after the interval time , I want it to be LOW always. Of course as long as time  is more than interval,and not get to a HIGH, until It has gone back to  analogRead(A0)> X and time is less than interval. And repeat the process... When time is more than interval it will go from where it was digitalWrite(pin, HIGH) to the new state digitalWrite(pin, LOW)

I made this table to be more clear

Table: analogRead(A0) > X? 

1-yes -->now check if time in this state is less than interval = 3000:

1.1-YES, ---> digitalWrite(pin, HIGH)
   1.2- if no ( time is more than interval ) --->  digitalWrite(pin, LOW)

2-  No, analogRead(A0) &lt; X . So -->  keep  digitalWrite(pin, LOW)

NEW UPDATE:

The state diagram for the new ranges should be something like this. I hope it is clear. I also removed the delays and replaced  (V &lt; X ) for (V &lt; Y )  , it works great. Thank you Edgar.

Z in the diagram being the variable for the new range, Z &lt;&lt; X


LAST UPDATE: 

Question Solved by Edgar Bonet
The description you give for the desired behavior is very unclear and
hard to follow. I am going to rephrase what I think I understood in
terms of a finite state machine, which is the correct way of
describing this kind of systems. Please, review the description below,
as chances are I did not completely understand what you really want.

Let's say the system can be in any of these three states:


In the V_LOW state, the input voltage is below the threshold X and
the output is LOW. If the input raises above X, we set the output
to HIGH and transition to the V_HIGH state.
In the V_HIGH state, the input is above the threshold X and the
output is HIGH. If the input falls below X we go back to the
V_LOW state. If we remain in V_HIGH for three seconds, we switch
the output to LOW and go to the TIMEOUT state.
In the TIMEOUT state, the input is above the threshold, yet the
output is LOW. When the input falls below X we go to the V_LOW
state.


Below is a state graph for this machine. The states with a gray
background have the output LOW, whereas the state with a white
background has the output HIGH. Each possible transition is drawn as
an arrow, labeled by the condition that triggers the transition:



Is this the behavior you have in mind? If no, try to describe the
behavior you want in terms of a finite state machine, as I have done
here. If yes, then I suggest you follow chrisl's advice and implement
some hysteresis, by having different thresholds for going into and out
of V_LOW.

Anyhow, below is a translation of the FSM above in C++. It may not be
the most efficient way to write it, but I believe it is straightforward
enough to not require any additional explanation:

void update_output()
{
    static enum { V_LOW, V_HIGH, TIMEOUT } state;
    static uint32_t time_entered_v_high;
    int V = analogRead(A0);

    switch (state) {
    case V_LOW:
        if (V &gt;= X) {
            digitalWrite(pin, HIGH);
            state = V_HIGH;
            time_entered_v_high = millis();
        }
        break;
    case V_HIGH:
        if (V &lt; X) {
            digitalWrite(pin, LOW);
            state = V_LOW;
        } else if (millis() - time_entered_v_high &gt;= 3000) {
            digitalWrite(pin, LOW);
            state = TIMEOUT;
        }
        break;
    case TIMEOUT:
        if (V &lt; X) {
            state = V_LOW;
        }
        break;
    }
}




Edit 1: Here is how to implement the hysteresis: you need to choose a
second, lower threshold, for going back to the state V_LOW. Let's call
it Y, which is smaller than X. Then, both on the diagram and the
code, replace every instance of “V &lt; X” by “V &lt; Y”. That's it. There is
absolutely nothing more to change.



Edit 2: Addressing the updated question. Now the output should go
HIGH when the input is out of a defined range. I'll add hysteresis
right away by using four thresholds
V0&nbsp;&lt;&nbsp;V1&nbsp;&lt;&nbsp;V2&nbsp;&lt;&nbsp;V3:


the input is in range if it is within
[V1,&nbsp;V2)
the input is out of range if it is not within
[V0,&nbsp;V3)
the ranges [V0,&nbsp;V1)
and [V2,&nbsp;V3) provide hysteresis.


Note that for consistency I like to have all the intervals closed on the
same side. I will also rename the states V_LOW and V_HIGH to
IN_RANGE and OUT_OF_RANGE respectively. Here is the updated code:

void update_output()
{
    static enum { IN_RANGE, OUT_OF_RANGE, TIMEOUT } state;
    static uint32_t time_exited_range;
    int V = analogRead(A0);

    switch (state) {
    case IN_RANGE:
        if (V &lt; V0 || V &gt;= V3) {
            digitalWrite(pin, HIGH);
            state = OUT_OF_RANGE;
            time_exited_range = millis();
        }
        break;
    case OUT_OF_RANGE:
        if (V &gt;= V1 &amp;&amp; V &lt; V2) {
            digitalWrite(pin, LOW);
            state = IN_RANGE;
        } else if (millis() - time_exited_range &gt;= 3000) {
            digitalWrite(pin, LOW);
            state = TIMEOUT;
        }
        break;
    case TIMEOUT:
        if (V &gt;= V1 &amp;&amp; V &lt; V2) {
            state = IN_RANGE;
        }
        break;
    }
}