How come in the arduino examples, when a button state is high, it means that the button is being pressed. But when I do my own programming, it's the other way around, and I always set it to low?
it's on a breadboard and it's a 2 pin push button. one pin is in a 5 volt power rail, and then a resistor is connected to the ground rail, and then in the same row as the button, which is then in the same row as a pin of the chip... sorry if it's confusing.
Delta_G:
Buttons can be connected either to be active low or active high where being pressed makes the button give low or high. Either way works, but you have to take care to note which way you have it in the code. Active low buttons are much easier to use because the Arduino has built in pull-up resistors. Active high buttons are easier for beginners to understand for some unknown reason. So the tutorials get written using active high buttons even though you will usually use active low if you know what's up.
Active high signals are probably easier for beginners to think about because one has to think of active low signals have to be thought of kinda backwards. This is because HIGH == TRUE and if statements are looking to act on a TRUE. It is a little more complex to think about having one's if statements act on a !LOW. (Other programming syntaxes would have this as ~LOW, or /LOW, or just simply Not LOW.)
Sembazuru:
This is because HIGH == TRUE and if statements are looking to act on a TRUE. It is a little more complex to think about having one's if statements act on a !LOW. (Other programming syntaxes would have this as ~LOW, or /LOW, or just simply Not LOW.)
Just adding one more statement: ~LOW would equal -1, !LOW would equal 1. So don't use ~LOW in this case. ~ is the bit operator NOT, and ! is the logical NOT.
Sembazuru:
This is because HIGH == TRUE and if statements are looking to act on a TRUE. It is a little more complex to think about having one's if statements act on a !LOW. (Other programming syntaxes would have this as ~LOW, or /LOW, or just simply Not LOW.)
Just adding one more statement: ~LOW would equal -1, !LOW would equal 1. So don't use ~LOW in this case. ~ is the bit operator NOT, and ! is the logical NOT.
True, in the context of C and it's derived languages (of which the Arduino is a part), ~LOW would be inappropriate. But as I said "Other programming syntaxes". I've seen other programming languages that use the tilde for boolean NOT. (Probably who ever thought up that syntax was probably thinking that a letter with the tilde accent looks close enough to a bar above the letter, especially with the limited character set of original ASCII...)
Edit: Actually let me restate... Other languages don't differentiate between boolean NOT and bit NOT. If the boolean datatype was actually 1 bit, there probably wouldn't be the need for ! as not, but that is neither here nor there. For this thread, one should only consider !HIGH == LOW.