Can 1 and 0 be used in place of HIGH and LOW when evaluating a pin's level?
int pin_lvl;
pin_lvl = digitalRead(3);
if (pin_lvl == HIGH) break;
or can I use...
int pin_lvl;
pin_lvl = digitalRead(3);
if (pin_lvl == 1) break;
Doesn't seem totally clear to me...
From Arduino Reference Constants - Defining Pin Levels: HIGH and LOW...
...the Arduino (Atmega) will report HIGH if:
- a voltage greater than 3 volts is present at the pin (5V boards)
Yet from "Wiring.h"...
#define HIGH 0x1
#define LOW 0x0
THinking that if "a voltage greater than 3 volts is present at the pin" then the value from digitalRead is 1 and since HIGH is defined as 1, either HIGH or 1 can be used for the evaluation of the read value. Is that correct?
I use 0 & 1 all the time.
You can define your own names such as SWITCHOFF and SWITCHON or use false and true, in fact anything that evaluates to 0 and 1 will work, but generally HIGH and LOW do for me.
void setup()
{
Serial.print("HIGH "); Serial.println(HIGH);
Serial.print("true "); Serial.println(true);
Serial.println();
Serial.print("LOW "); Serial.println(LOW);
Serial.print("false "); Serial.println(false);
}
Not tested, but should give you the idea how to test what happens.
It's often clearer to use a descriptive name (as suggested by UKHeliBob). If all your switches or buttons are connected between pin and ground, you can define e.g. PRESSED to be the same as LOW.
#define PRESSED LOW
void loop()
{
if(digitalRead(yourPin) == PRESSED)
{
// do something if button is pressed
}
if(digitalRead(yourOtherpin) != PRESSED
{
// do something if other button is not pressed
}
}
If you have a mix of buttons connected to ground and buttons connected to Vcc, you will have to define multiple descriptive names. E.g.
#define BTN1_PRESSED LOW // button 1 is LOW when pressed
#define BTN2_PRESSED HIGH // button 2 is HIGH when pressed
You can #undef the macros in wiring.h and create something a little more fun.
I like the bored programmers way of defining true/false, 0/1
#define HIGH '/'/'/'
#define LOW '-'-'-'