Just wondering if the question's also about how to tell if a voltage is actually above 2? Just in case that is the question, well that's just an analogRead and you will be wanting to check for that value to be over 2/5 of 1023 (or is that 1024?) so over 409.
You should have a look at the StateChangeDetection example, which is also in the IDE at File > Examples > 2.Digital
That example includes a whole load of stuff to do with counting presses which is unnecessary in your case, and in another thread recently member groundfungus pruned it back to do only the detection. I include that below; it's for a digital read so you'll need to change it to be for your analog case, but the idea is essentially the same.
//StateChangeDetection example pruned of all the counting stuff
//thanks to groundfungus arduino forum thread 450871 reply #10
// this constant won't change:
const int inputPin = 9; // the pin that the input voltage is attached to
// Variables will change:
int inputState = 0; // current state of the input
int lastInputState = 0; // previous state of the input
void setup()
{
// initialize the input pin as a input: (not really necessary as the default is INPUT
pinMode(inputPin, INPUT);
Serial.begin(9600);
}
void loop()
{
// read the input pin:
inputState = digitalRead(inputPin);
// compare the inputState to its previous state (lastInputState)
if (inputState != lastInputState)
{
if (inputState == HIGH)
{
// if the current state is HIGH then the input
// went from not high to high:
Serial.println("Went high");
// *****************************************
// do the stuff that a HIGH input triggers
// *****************************************
}
else
{
// if the current state is LOW then the input
// went from high to not high: do nothing except print something if you want.
Serial.println("Went low");
}
}
// save the current state as the last state,
//for next time through the loop
lastInputState = inputState;
}
If the precision is not too important and you don't have to execute anything else in the mean while, you can try the below:
void loop()
{
//read analog value to variable 'volt'
if(volt>409) //(2/5)*1024
if(vlotChange(volt))
digitalWrite(led,1);
//same way for the less than 2V
}
boolean voltChange(double volt)
{
int i=1;
boolean flag=true;
while(i<=10) //for 1 sec delay. Change according to the change in delay
{
//read voltage to variable nVolt
if(nvolt!=volt)
{
flag=false;
break;
}
delay(1000); //checking the change in every sec. Delay can be changed as required.
}
return flag;
}
If the precision is important working with timer will be better.
logans:
If ... you don't have to execute anything else in the mean while
I think in spite of your warning, it's not a great idea to do anything like that with a delay(). It's not difficult to implement non-blocking millis()-based timing, and makes the code more adaptable. Oftentimes one starts out with only having to say read one switch or blink one led, but as soon as you want to do two things at once you're screwed.
Much easier to get your mind round millis()-based non-blocking code early on, then use it as second nature.
.
.
.
.
boolean voltChange(double volt)
{
int i=1;
boolean flag=true;
while(i<=10) //for 1 sec delay. Change according to the change in delay
{
//read voltage to variable nVolt
if(nvolt!=volt)
{
flag=false;
break;
}
delay(1000); //checking the change in every sec. Delay can be changed as required.
i++
}
return flag;
}