Are these lines of code unuual?

The program below is working reasonably well (with a button that's not too noisy). But there are a couple of lines that puzzle me. They seem different to my recollection of other similar programs. (Tried bolding my comments but presumably fails in code?).

So I'd appreciate comments from more experienced users please.

// Input (button or RC4) on pin 4 goes HIGH when pressed, unlike original,
// so that I can use some existing wiring
// Toggles output when input goes LOW, i.e. when button released
// Based on original from ArduinoGetStarted.com
// Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-toggle-led
// No debouncing; not critical in this case with visual feedback
// Constants

const byte RC4Pin = 4; // Arduino pin connected to button's pin
const byte outPin    = 12; // Arduino pin connected to LED's pin

// Variables
int outState = LOW;
int lastRC4State;
int currentRC4State;

void setup()
{
  Serial.begin(115200);
  Serial.println("
");
  pinMode(RC4Pin, INPUT); // External pull-down
  pinMode(outPin, OUTPUT);

  currentRC4State = digitalRead(RC4Pin); // **Not usually in setup()?**
}

void loop()
{
  lastRC4State = currentRC4State; // Save last state for next loop. **Isn't this usually at the end of the loop? Does it matter?**
  currentRC4State = digitalRead(RC4Pin); // Read new state

  if (lastRC4State == HIGH && currentRC4State == LOW)
  {
    Serial.println("The button was pressed");

    // toggle state of LED
    outState = !outState;

    // control output according to the toggled state
    digitalWrite(outPin, outState);
  }
}

  pinMode(RC4Pin, INPUT); // External pull-down

Looks like a comment to say that the pin has an external pulldown resistor

  currentRC4State = digitalRead(RC4Pin); // Not usually in setup()?

Looks like the code reads the state of a pin at start up to set the initial value of a variable. There is nothing odd about that as the code goes on to detect a change of state of that pin

It can matter. Since loop() is called repeatedly, you would see the difference only in the first call.

There is no reason to assume that code you find somewhere on the internet is correct. It often is not.

Thanks both, understood, appreciate the fast replies.

As my italicising or bolding didn't work, for clarity here are the two lines in question:

currentRC4State = digitalRead(RC4Pin); // Not usually in setup()?

lastRC4State = currentRC4State; // Save last state for next loop. Isn't this usually at the end of the loop? Does it matter?

I have answered the first one

As to the second one, you can do it anywhere before you read the state of the pin again as long as it is not in a conditional statement so that you can be sure that the copy will happen

You can even do

lastRC4State = currentRC4State;
currentRC4State = digitalRead(RC4Pin);

if it suits you

As you observe, it's often done differently. This way though guarantees that lastState has a valid value before it is used. That may be helpful if you're swapping between pullup and pulldown because you don't have to remember to initialize it differently for the two options.

If you find yourself doing much of this, I recommend having only one place where the switch is actually digitalRead() interrogated.

Then if you change the wiring, you have only one thing to do - invert the state of the reading directly when you read the switch.

Everyone else just uses the result.

It could be a variable that can be seen by all who care.

It could be a function that contains the actual reading, everyone uses it to do the reading.

 int theSwitch = digitalRead(thePin);  // replace with !digitalRead if you change the sense

Becuase it's in one place, the rest of the code just thinks HIGH means pressed, or whatever.

Becuase it's in one place, you only need change one line of code.

Another thing seen that I have done is to define your own PRESST and NOT_PRESST, and link them (in one place!) to the true values of digitalRead, HIGH and LOW, viz:

// swap HIGH and LOW for pulled down wiring of switches
# define PRESST        LOW
# define NOT_PRESST    HIGH

HTH

a7

Great suggestions. I’ll try them.

Well I got it exactly backwards, I’ll edit the post to say what I meant.

Which I hope wasn’t un-fixing. I get so confused.

a7