I have a simple LED light that has an AC signal going across it. This signal is setup in a way so that when the signal goes low, its at 0.00V and high at numbers >= 0.20V. The simple AnalogRead() I have implemented is reading as so: "0.00 0.00 0.25 0.28 0.00 0.00 0.32 0.33...." This is causing this light to flicker a bit. The LED is connected to a simple wire that when its connected, it lights up, and obviously when it is not connected it stays at 0.00V. I need a certain loop(I have tried just about every implementation I can with various loops) to be able to read this voltage, and if it is high at all in the sequence, that means the wire is connected, but then low if the wire is not connected. My problem is that when saying while high, turn on, and if low, turn off, it constantly flickers because is it reading the 0.00 sequence with high numbers as low and trying to turn off. I have tried to take an array and store the numbers if it is high, and then if the array gets to 50 values, turn on LED. If the array holds 50 low vales in a row, turn off. I was very unsuccessful with this approach and was hoping you guys could help me out. I am sorry if I worded this problem the wrong way. I am still new to this forum thing...Any help is appreciated.
and if it is high at all in the sequence
So, how do you define a "sequence"? 10 readings 1 second apart? 3 readings a week apart? 1000 readings a millisecond apart?
Im not sure how I can get that exact number for you but I would say around 10-20 readings a second
Im not sure how I can get that exact number for you
They are your requirements. Make something up if you don't know.
but I would say around 10-20 readings a second
So, use millis() to get a time. Use a while loop to read the analog pin for one second. If any value is non-zero, do something and break out of the loop. If not, when the loop ends, you'll know that no positive value was received in the one second window.
How can I set it so that I read the analog pin for one second?
deeg92:
How can I set it so that I read the analog pin for one second?
Use a while loop
this is what I have so far:
time = millis();
int detState = analogRead(detA);
float vdet = detState * (5.0/1023.0);
while(time <= 1000){
if(vdet >= 0.14){
digitalWrite(11, HIGH);
break;
}
}
digitalWrite(11, LOW);
this is what I have so far:
Besides being incomplete and improperly posted, does it do what you want?
As soon as the while loop ends, for whichever reason, the pin will be turned off. Is that what you want?
I changed the post so that you could read the code. Also, I have closed the void loop() I just do not have it posted. When the wire is connected, it should read the voltages higher than zero and turn on. That part I can get working, my problem is I cannot find a way to implement the "if zero, turn off" part because then it reads the alternating values when the wire is connected and tries to turn it on and off at the same time. I am not writing the loop correctly that is why I am asking for help
You need to keep track of whether or not you got a positive value during the time that the while loop is running.
bool gotPositiveValue = false;
unsigned long startTime = millis();
while(millis() - startTime < 1000)
{
if(analogRead(detA) > threshold)
{
gotPositiveValue = true;
// do whatever else needs doing
break;
}
}
if(gotPositiveValue)
{
// Do whatever
}
else
{
// Did not get a positive value...
}
There is no reason to convert the analog pin reading to a float to determine if it exceeds some threshold.
Ok thanks a lot! That all makes perfect sense except the if(gotPositiveValue). Isn't that saying we did not get a positive value? The exact same thing as the else statement?
Isn't that saying we did not get a positive value?
No, it is saying that we DID get a positive value.
if(!gotPositiveValue)
would be how to test for not having gotten a positive value.
Ok, I have all that implemented and the LED will blink very inconsistent and does not follow the connection of the wire. It turns on and off whether the wire is connected or not.
It turns on and off whether the wire is connected or not.
Reading an analog pin with nothing connected to it is a waste of time.
Your current code?
int detState = analogRead(detA);
float vdet = detState * (5.0/1023.0);
Serial.println(vdet);
bool gotPositiveValue = false;
unsigned long startTime = millis();
while(millis() - startTime < 500){
if(vdet >= 0.20){
gotPositiveValue = true;
break;
}
}
if(gotPositiveValue){
digitalWrite(11, HIGH);
}
else{
digitalWrite(11, LOW);
}
I switched it to 500 but I tried both that and 1000
Your code won't even compile - no setup(), no loop().
I have all of that just fine I was just sending you the code I was working on. If you want everything I can send it but this code is part of a much larger program that has many inputs and outputs declared.
the code that you sent and I implemented will turn the LED on when the wire is initially connected but it will not turn off when I disconnect the wire.
but it will not turn off when I disconnect the wire.
That is the nature of an analog-to-digital converter. The ADC can not tell that there is nothing connected.