I can't comprehend the idea of using switch to groung pin to get logical 1

Hey, this really ruined my day. I tought it would be easy to use boards own pullup resistor in input pin, so I could just ground it if needed...

Well, code works perfectly, but something in my brain says that I shouldn't ever use grounding pin as logical 1 :smiley:

Yes if I dont use, it needs extra pulldown resistor and if cable broke and 5V touch to ground, it might break board or expensive power supply (takes 5v/10mA from automation bus)

its just so wickked idea that I need to ground pin to make logical 1, does anyone else have such weird mindsets or I am alone there :smiley:

I think this will help you. Add something like this to the top of your sketch:

byte buttonPressedState = LOW;
byte buttonNotPressedState = HIGH;

Now you can use those variable names in your code and, as far as the rest of your code goes, the wiring of the switch is irrelevant. This also would allow you to more easily update your sketch if you decided later to change the wiring.

Of course, you are welcome to name the variables as you like, it's just an example of how the use of meaningful variable names can eliminate this sort of confusion.

you push the button down to get LOW

zaleopard:
its just so wickked idea that I need to ground pin to make logical 1, does anyone else have such weird mindsets or I am alone there :smiley:

This is computer programming - not real life. There is nothing unusual about inverting logic.

Or, we programmers do have weird mindsets :slight_smile:

...R

@zaleopard

Your topic was Moved to it's current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

If you use the internal pullup the function

Serial.println(   digitalRead(MYIOPin) );

will print "1" as long as you don't connect your IO-pin to ground. The pullup does what the name says pulling-up the logical level to HIGH

If you use the internal pullup and connect the IO-Pin to ground

Serial.println(   digitalRead(MYIOPin) );

will print "0". The connection to ground sets the IO-pin to gorund

So an if-condition that shall check for button pressed would look like

if (digitalRead(MYIOPin) == LOW) { //inverted logic when using pull-up-resistor
  //takeAction because button is pressed
}

What pert suggested makes it easier to read

byte buttonPressedState = LOW;

then the if-condition directly says what should be checked

//old version
if (digitalRead(MYIOPin) == LOW) { //inverted logic when using pull-up-resistor
  //takeAction because button is pressed
}

// perts version
byte buttonPressedState = LOW;
if (digitalRead(MYIOPin) == buttonPressedState) { //the self-explaining names do what they say
  //takeAction because button is pressed
}

To understand it electrically read this

best regards Stefan

but something in my brain says that I shouldn't ever use grounding pin as logical 1

Why do you want a logical 1 (whatever that is). Surely you just want to know whether an input is HIGH or LOW and then your program interprets the state as appropriate

This is always my version

#define ISPRESSED LOW
...
...

// check if button is pressed
if(digitalRead(yourPin) == ISPRESSED)
{
}

// check if button is not pressed
if(digitalRead(yourPin) != ISPRESSED)
{
}

Nice ideas ^^

I dont know why it was moved from bar to programming... but it wasnt really anything to do with programming, its just that I have been working 15+ years on electric and its (almost) always that you connect main voltage to make something happen.

For example in good habbit is to route live wire first to switch and then to lamp... and if someone is connected 230 directly to lamp and using wall switch to cut '0', its kind of bad way to do it, even its working perfectly well.

in a code its nothing weird, as Im also designing automation and there is really normal to have 0 as an signal.

so its 100% just wall switch that Im bothered, ok. its 5V, but so what... But after 15 years on work and 4+ on school makes your brain melt a bit :smiley:

and just to fun of it, I have bit extra options in my buttons :smiley:

//Assing binary inputs, here you can assing witch addresses are assigned to witch ports and default value for it if needed.
//I dont recommend to assign same port twice to different gAddress, but it works most of time :D
String  OutAddrBool[]       = { "1/1/4"  , "1/1/2"   , "1/1/55"  }; //Address to send telegram
int     digitalIn[]         = { 11       , 12        , 13        }; //port (IO pin) to read, There might be broblen if there is same port twice
int     digitalInStatus[]   = { -1       , -1        , -1        }; //data
int     digitalInPullup[]   = { HIGH     , HIGH      , HIGH      }; // High = pullup resistor, LOW does nothing. But notice if you define pin twice, last one affect both of them
String  digitalInType[]     = {"impulse" , "impulse" , "impulse" }; //impulse or switch
int  outAddrBoolInverted[]  = {true      , false     , true      };  //inverted mode send '1' when pin is pulled to ground, good if you use pull up resistor
unsigned long lastChange[]  = {millis()  , millis()  , millis()  }; // timer to ignore putton bounce, could also be used to refresh knx message for every 1 min etc.
int outAddrBoolSended[20]   = {0}; //you may ignore this, just make sure there is more values in array than in OutAddrBool[], indicates if value is sended (impulse switch)
int outAddrBoolLastSended[20]   = {0}; //you may ignore this, just make sure there is more values in array than in OutAddrBool[] in case of impulse switch we need to know what was last send state

So if you are not really interested in a solution just go on populating your code with even more options
melting your brain with too many options seems to be a nice hobby

zaleopard:
and just to fun of it, I have bit extra options in my buttons :smiley:

Maybe I was wrong to recommend moving this from Bar Sport to Programming :slight_smile: :slight_smile:

...R

Robin2:
Maybe I was wrong to recommend moving this from Bar Sport to Programming :slight_smile: :slight_smile:

...R

Nope you were correct as this is clearly a programming topic. 8)

StefanL38:
So if you are not really interested in a solution just go on populating your code with even more options
melting your brain with too many options seems to be a nice hobby

Erm, but i didnt even ask anything? I just didnt time to do any class for the options so I wrote the "Bar Sport" about my weird feeling about using ground as an switch connect.
and sadly all of those options are necessary. its just bit rude form atm.

Another reason is that induced noise spikes tend to be positive, so taking a pin low instead of high to indicate an event reduces false closures.

Personally, I find it hard to believe that someone who has "been working 15+ years on electric (sic)" has so much difficulty with the concept of a signal being asserted low. It happens all the time. Consider, for example, that the "Output Enable" control signals on the vast majority of digital logic ICs are Active Low.

TheMemberFormerlyKnownAsAWOL:
Another reason is that induced noise spikes tend to be positive, so taking a pin low instead of high to indicate an event reduces false closures.

Hmm, could be, Didnt tought that. might do some tests at work.

gfvalvo:
Personally, I find it hard to believe that someone who has "been working 15+ years on electric (sic)" has so much difficulty with the concept of a signal being asserted low. It happens all the time. Consider, for example, that the "Output Enable" control signals on the vast majority of digital logic ICs are Active Low.

I say that my mind doesn't like it being on switches (on a wall). im really happy that its not common way on houses, it would be painfull to look connection errors (not imbosible of course, but now you can just plug fluke to ground on fuse box and just go trought all connections.

if those would cut ground, you would have to use bit different device or plug fluke other pin to live voltage and it would be anoying if you accidentaly touch another one.

zaleopard:
Erm, but i didnt even ask anything?

Alas, it is very common to see requests for help posted in the Forum in the wrong section and without any obvious question in the text.

That's how I interpreted your Original Post.

...R

The actual reason switches controlling logic circuits are often active low is that in old TTL logic
families connecting an input pin to Vcc (+5V) could damage the chips.

In TTL logic the input stage needs a pullup resistor to 5V to read high, but will happily tolerate
0V (as in a direct short to ground).

The input stage transistor in TTL was likely to fail if the input ever saw more than 5.5V, which is
too close to comfort for the 5V Vcc supply (if there's noise on it). The power pins on a TTL chip
are actually rated to +7V so the chip should survive quite a lot of supply noise, so long as no
inputs are directly connected to Vcc.

If you routed +5V to a remote switch and back, the noise pick up on the cable might easily put

5.5V spikes on the line, which would not be safe for TTL (reliable operation is compromized),
whereas routing ground to a switch and back is without issue.

Thus the standard way to wire switches became active low (in fact many control signals in TTL
logic are active low, and this is inhereted by CMOS in the 74HCxx family as they the same logic
exactly.

If you see old TTL or LS-TTL circuits you'll see lots of 1k pullups (TTL) or 10k pullups (LS) for
inputs that are hard-wired as HIGH. These resistors prevent noise on the 5V rail damaging the
chips (or from taking more current than necessary and increasing power consumption come to that...)

Similarly, the outputs of the old-time TTL circuits could typically sink more current than they could source. Thus, the standard way of lighting an LED was to pull the output low and sink current when you wanted the LED to illuminate.

zaleopard:
Hmm, could be, Didnt tought that. might do some tests at work.
I say that my mind doesn't like it being on switches (on a wall). im really happy that its not common way on houses, it would be painfull to look connection errors (not imbosible of course, but now you can just plug fluke to ground on fuse box and just go trought all connections.

if those would cut ground, you would have to use bit different device or plug fluke other pin to live voltage and it would be anoying if you accidentaly touch another one.

The difference is that if you start switching ground in the world of 115V or 240V it is likely to makes everything live and kill someone. With 5V or 3.3V logic the worst you can do is fry your board. $10 on Ebay.

All you are really doing is completing a circuit to allow current to flow somewhere.