Using micro switch on analog pin

no need to test your code, I take your word for it Robin :slight_smile:

perhaps I'll give testing my code a try this evening - when I will be able to set up everything. I may have a false memory here if you are so sure about it. I cannot exclude the possibility that I "tried" an analogRead() on a digital pin (by accident) - in case there is a difference there or possible at all.

Thank you guys anyways for explaining things to me. You live and learn :slight_smile:

step1

okay, I have now experimented a little. Yes, digitalRead() is the way to go. AnalogRead() does not work at all with a simple switch ... for testing I didn't even use a switch, simply held two cables together (one from pin A1, the other to GND. Then I thought.. hmm, maybe I don't even need GND if I use the metal casing of my apparatus as GND. My body (hand) works jsut as well! So all I do is connect a cable to A1, use INPUT_PULLUP in pinMode and when I touch the cable digitalRead(A1) detects it.

I just hope it will not be too sensitive. Humidity etc. might be a factor. But as for now I am very satisfied. I haven't even gone as far as trying to find out if analogRead would be faster with analog inputs. I am a complete digital guy now :smiley:

AnalogRead() does not work at all with a simple switch

It does if you wire it correctly and test the value returned properly. One mistake is to test for values of exactly 0 or 1023 when analogRead() may be returning a slightly higher or lower value because of a resistance in the circuit. Testing for say < 200 or > 800 might be better, but as you have discovered digitalRead() works as expected and has the advantage that you can use INPUT_PULLUP

yes, of course I forgot about that. Having to wrap my head around all those values and which ones I should choose using digitalRead() instead is much more convinient. The simple IF analogRead(A0) 'do something' works for the TP224 but it won't for the simple switch where you have to check for certain values/ ranges as you say.

Cheers
step1

step1:
The simple IF analogRead(A0) 'do something'

That is a program style that I would never use with analogRead(). analogRead() does not return a Yes/No value.

...R

whatever it returns it works with the tp224. I don't know why but I don't really care (at least for now since I'm not using it in my current project anymore anyway). To find out why one would have to figure out what the Arduino does with the command when it processes it. Probably adds some stuff to the best of its "knowledge" to make it work :slight_smile: or the developpers of the tp224 deemed code as dumb as that possible, who knows - someone will :smiley:

Cheers
step1

UKHeliBob:
... but as you have discovered digitalRead() works as expected and has the advantage that you can use INPUT_PULLUP

But you would use INPUT_PULLUP for an analogRead() as well. :roll_eyes:

Paul__B:
But you would use INPUT_PULLUP for an analogRead() as well. :roll_eyes:

But INPUT_PULLUP has no meaning for an analogue input. Admittedly, when wired correctly an analogue input will not be floating anyway, so the point is moot

UKHeliBob:
But INPUT_PULLUP has no meaning for an analogue input.

Nonsense!

Analog inputs A0 to A5 include the digital structure, including the internal pull-up, activated by setting to INPUT and writing the port HIGH.

If you connect a device - such as an LDR or switch/ button - from the pin to ground, it will be biased by the approximately 47k pull-up and its on/ off state or voltage drop read by analogRead.

Nonsense!

What !
How dare you suggest that my answer was wrong ?

Even worse, how dare you be correct ? :slight_smile:

My apologies for misleading anyone who read my previous post