Delta_G:
With digitalRead you get a LOW for anything below 1.5V. So you could really use a value as high as 340 there and still match what digitalRead does. I don't think you need to or want to insist that the value come all the way to 1 or 0.
Delta_G:
I am surprised that you needed to do any calculation to understand how that was going to work.
1. For a typical TTL gate, the VOL (maximun value that will be recognized as LOW) is 0.5V and VOH (minimum value that will be recognized as HIGH) is 2.7V. I can't find the source of 1.5V for VOL. Any value between 0.5 and 2.7 is the forbidden zone.
2. For ATmega328P VOL is 0.9V and VOH is 4.2V.
3. The 340 of your post cannot be a magic number; it must have some kind of theoretical support. Taking your value 1.5V as VOL, I have come very close to 340 ((1024/5000)*1.5 ~= 308?) under 10-bit accuracy of the ADC Module of ATmega328P MCU.
4. If any body wants to connect a push button with an analog channel of the ATmega328P MCU, he must use the following if(){} structure with comparison value of: (1024/5000)*0.9 ~= 184; there is no theoretical support for 340?). No doubt, the credit for the concept of this non-zero parameter goes to @Delta_G; the credit for the accurate value of this parameter goes to whom?).
if(ananlogRead(A6) <=184)
{
Serial.print("Button is closed");
}
5. And still, there is something to say --
(1) The push button is simply an ON/OFF switch; it will be either at closed condition (with zero contact resistance) or at open condition (with infinite resistance). Any value in between for the contact resistance will qualify the button as a bad one, and it should be replaced.
(2) According to data sheet, the error of the ADC Module of the ATmega328P MCU is 2-bit. Therefore, it should be good enough to use the following if() structure with comparison value of 3 (11b) to decide the close condition of a push button:
if(ananlogRead(A6) <=3)
{
Serial.print("Button is closed.");
}
(3) Is there really any need to use the comparison value of: 340 or 184 or 500 (Post#11)?