trigering pin on and off with two threshold values

Thread Starter

Dabu WhiteHacker

Joined Sep 5, 2017
68
hey i was trying to turn on and off the led when a variable reach a certain threshold value and stay in that condition until it reaches another threshold value
for example i have a variable x whose value vary between 1 and 10.when the value of x become 1 i want to turn the led on and stay on until it reaches 10 and when it reaches 10 it should stay off until it again reaches 1.
may be something like that :/

if(x==10){
digitalWrite(led,HIGH);
}
else if(x==1){
digitalWrite(led,LOW);
}
else {
// some code that keeps the led state in which it was unitil it reaches the other thershold value
}
 

WBahn

Joined Mar 31, 2012
32,871
hey i was trying to turn on and off the led when a variable reach a certain threshold value and stay in that condition until it reaches another threshold value
for example i have a variable x whose value vary between 1 and 10.when the value of x become 1 i want to turn the led on and stay on until it reaches 10 and when it reaches 10 it should stay off until it again reaches 1.
may be something like that :/

if(x==10){
digitalWrite(led,HIGH);
}
else if(x==1){
digitalWrite(led,LOW);
}
else {
// some code that keeps the led state in which it was unitil it reaches the other thershold value
}
You shouldn't need your second else block. The value of the pin should hold it's value until you explicitly change it.

The one thing you probably want to do, however, is not check for exact equality unless you are SURE that is what you need. So you might want

Code:
if (x <= 1)
   digitalWrite(led, LOW);
if (x >= 10)
   digitalWrite(led, HIGH);
Note that you don't need to put the second if() within the else clause of the first one.
 

Ramussons

Joined May 3, 2013
1,568
You shouldn't need your second else block. The value of the pin should hold it's value until you explicitly change it.

The one thing you probably want to do, however, is not check for exact equality unless you are SURE that is what you need. So you might want

Code:
if (x <= 1)
   digitalWrite(led, LOW);
if (x >= 10)
   digitalWrite(led, HIGH);
Note that you don't need to put the second if() within the else clause of the first one.
Are'nt statements 3 and 5 to be the same?

if (x <= 1) .and. if (x => 10)
digitalWrite(led, LOW);
 

WBahn

Joined Mar 31, 2012
32,871
Are'nt statements 3 and 5 to be the same?

if (x <= 1) .and. if (x => 10)
digitalWrite(led, LOW);

Uh... no.

What you have will set the output LO as soon as either end of the range is met and then there is nothing to ever set it HI again.

Statements 2,3 test if x is at or below 1 and, if it is, it sets the output LO.

Statements 4,5 test if x is at or above 10 and, if it is, it sets the output HI.

If x is between 1 and 10 (non-inclusive), it leaves it alone.
 

WBahn

Joined Mar 31, 2012
32,871
hey i was trying to turn on and off the led when a variable reach a certain threshold value and stay in that condition until it reaches another threshold value
for example i have a variable x whose value vary between 1 and 10.when the value of x become 1 i want to turn the led on and stay on until it reaches 10 and when it reaches 10 it should stay off until it again reaches 1.
may be something like that :/

if(x==10){
digitalWrite(led,HIGH);
}
else if(x==1){
digitalWrite(led,LOW);
}
else {
// some code that keeps the led state in which it was unitil it reaches the other thershold value
}
I just went back and read your verbal description more carefully and it doesn't agree with your code snippet (which is what I was primarily basing things on).

From your verbal description, it sounds like you really want.

Code:
if (x <= 1)
   digitalWrite(led, HI);
if (x >= 10)
   digitalWrite(led, LO);
This would be typical of many level control schemes. Say that x is the level in a water tank and a HI output turns on a pump that fills the tank.
 

Ramussons

Joined May 3, 2013
1,568
Uh... no.

What you have will set the output LO as soon as either end of the range is met and then there is nothing to ever set it HI again.

Statements 2,3 test if x is at or below 1 and, if it is, it sets the output LO.

Statements 4,5 test if x is at or above 10 and, if it is, it sets the output HI.

If x is between 1 and 10 (non-inclusive), it leaves it alone.
I missed the last part of the requirement
when it reaches 10 it should stay off until it again reaches 1.
 

John P

Joined Oct 14, 2008
2,061
This is fine, but if x is an unsigned quantity, you need to make sure that it never gets loaded with a negative value, or decremented below zero. Because a value of less than zero is taken as a large positive value, and your program will mysteriously do exactly the opposite of what it should be doing! I expect most of us have been there and done that.
 
Top