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)> 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) < 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 < 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 < 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 – 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) >= 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) < 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 < X ) for (V < Y ) , it works great. Thank you Edgar.
Z in the diagram being the variable for the new range, Z << X
LAST UPDATE:
Question Solved by Edgar Bonet
millis()
like in the BlinkWithoutDelay example, that comes with the Arduino IDE