I have read that it is good programming practice to set PINS as Constants and also start their names with capital "K" to help you understand your code later.
What I struggle with grasping, is if I print the constant's value I get the PIN #. How do I see if the PIN is HIGH(1) or LOW(0) if it is connected to a push button? I got around the problem by assigning its value to a second variable, and printing that variables value.
// Declare pins - Constants
const byte KButton = 3; // Connect push button to Pin 2 and Earth
// Declare Variables
byte Status = 0;
void setup()
{ //This only happens once
pinMode(KButton, INPUT_PULLUP); // Remember Pin 3 is HIGH when NOT Pressed
Serial.begin(9600);
}
void loop()
{ // This is the LOOP
Status = digitalRead(KButton); // Read if Button is pushed
Serial.println(KButton);
Serial.println(Status);
Status = digitalRead(KButton);
delay(1000);
}
HermanJFourie:
start their names with capital "K" to help you understand your code later.
I've never heard that, thought I have seen people use that ugly "Hungarian notation" thing. I don't go for that. If you write quality code with descriptive variable names then it should largely be obvious to you what the characteristics of each variable is without needing to turn their names into alphabet soup. If I have a variable named buttonPin then obviously it's going to be a constant.
HermanJFourie:
How do I see if the PIN is HIGH(1) or LOW(0) if it is connected to a push button? I got around the problem by assigning its value to a second variable, and printing that variables value.
That works. If you're not going to do anything with the value of the variable other than print it then you might as well just print the return value of digitalRead() directly:
That's pretty much what you do. digitalRead(pin_number) will give you the current state of a pin. You can either save the value to a variable, as you have done, or print it directly - Serial.println(digitalRead(pin_number)).
const byte KButton = 3; // Connect push button to Pin 2 and Earth
// Declare Variables
byte Status = 0;
void setup()
{ //This only happens once
pinMode(KButton, INPUT_PULLUP); // Remember Pin 3 is HIGH when NOT Pressed
Serial.begin(9600);
}
void loop()
{ // This is the LOOP
Status = digitalRead(KButton); // Read if Button is pushed
Serial.println(KButton);
Serial.println(Status);
Status = digitalRead(KButton);
delay(1000);
}
1/ Good programming is to use exact comment :
"const byte KButton = 3; // Connect push button to Pin 2 and Earth"
2/ Why do you use twice :
"Status = digitalRead(KButton);" ?
Can somebody explain why the LED connected to the relay pin for debugging purposes is flashing if I keep the button depressed. I need to use the same syntax to see if a limit switch is made and this will stay depressed.
// Declare pins - Constants
const byte Button = 3; // Connect push button to Pin 3 and Earth
const byte CRelay = 8; // Connected to relay, LED for debugging
// Declare Variables
byte ButtonStatus = 0;
byte Pushed = 0;
void setup()
{ //This only happens once
pinMode(Button, INPUT_PULLUP); // Remember Pin 3 is HIGH when NOT Pressed
pinMode (CRelay, OUTPUT);
Serial.begin(9600);
}
void loop()
{ // This is the LOOP
ButtonStatus = digitalRead(Button); // Read if Button is pushed, HIGH when not PUSHED
delay(100);
if (ButtonStatus == LOW) //Pressed 0
{
Serial.println("In loop 1");
Pushed = 1- Pushed;
}
if (Pushed ==HIGH)
{
Serial.println("In Loop 2");
digitalWrite(CRelay, HIGH);
}
else
{
Serial.println("In loop 3");
digitalWrite(CRelay, LOW);
}
delay(100);
}
It's a bad idea to do mathematics with logical variables. While the byte Pushed can obviously hold a lot more than just HIGH or LOW, questions like "Is HIGH greater than LOW?" simply have no meaning.
Instead of "1-" maybe you meant "not"? This may also be written as "!".