Simple Pin Values

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:

Serial.println(digitalRead(buttonPin));

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)).

Thanks :wink:

Hello HermanJFourie

HermanJFourie:

// 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);
   
}

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);" ?

Regards,
bidouilleelec

bidouilleelec:
1/ Good programming is to use exact comment :
"const byte KButton = 3; // Connect push button to Pin 2 and Earth"

With the use of meaningful variable names there is no need for a comment and no risk that the comment gets out of sync
const byte startButtonPin = 3;

...R

If I have a variable named buttonPin then obviously it's going to be a constant.

I don't see that. If you have a variable named buttonPin then obviously it SHOULD BE a constant. THAT I can see.

All changes made as suggested...Thanks

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);      
    
}

Can somebody explain why the LED connected to the relay pin for debugging purposes is flassing if I keep the button depressed.

I don't even know what "flassing" means, so I can't.

You almost certainly need to look at the state change detection example.

Flashing

              Pushed = 1- Pushed;

}
            if (Pushed ==HIGH)

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 "!".

  Pushed = !Pushed;

Can somebody explain why the LED connected to the relay pin for debugging purposes is flashing if I keep the button depressed.

Work through the code and see what happens when the button is held pressed.

  if (ButtonStatus == LOW)    //Pressed 0
  {
    Serial.println("In loop 1");
    Pushed = 1 - Pushed;
  }

Each time through loop(), if the button is pressed then the state of Pushed will be changed

You need to determine when the button becomes pressed, not when it is pressed. Look at the StateChangeDetection example in the IDE