How to create a variable on start up and change in loop()?

Hi,

I've got the following code, adapted from one the examples:

/*
  DigitalReadSerial
 Reads a digital input on pin 2, prints the result to the serial monitor

 This example code is in the public domain.
 */

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
int oldVal = digitalRead(2);

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(2, INPUT);
  pinMode(13,OUTPUT);
  digitalWrite(2,HIGH);
  
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int sensorValue = digitalRead(2);
  if(sensorValue != oldVal){
  // print out the state of the button:
  Serial.println(sensorValue);
  int oldVal = int sensorValue;
  }
  if(sensorValue > 0){
    digitalWrite(13,HIGH);
  }
  if(sensorValue < 1){
    digitalWrite(13,LOW);
  }
  delay(1);        // delay in between reads for stability
}

What I'm hoping this does is reads the state of a switch on Pin 2, and then if the state of the switch is changed, it will print the state of the switch that's changed. I'm unsure if we can use digitalRead outside of a function; and at the same time, I'm unsure if this is going to keep updating oldVal constantly (or only assign it once upon start up). What is the way/better way of doing this?

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

Multi:

  int oldVal = int sensorValue;

You have already declared oldVal as a global. By declaring it again here you are creating a local variable named oldVal. It will be destroyed as soon as you exit the if statement block. That's going to cause your code not to work because you need to preserve the value of oldVal, that's why you made it a global. There is another problem, with that code. You are also redeclaring sensorValue, you don't seem to understand that you only need to specify the type on the declaration, after that just use the variable name, no type! Change that line to:

  oldVal = sensorValue;

Multi:

  if(sensorValue > 0){

digitalWrite(13,HIGH);
  }
  if(sensorValue < 1){
    digitalWrite(13,LOW);
  }

Please read this:
https://www.arduino.cc/en/Reference/DigitalRead

Returns

HIGH or LOW

The code you wrote does work because HIGH happens to be defined as 1 and LOW happens to be defined as 0 but it's needlessly complex. Doesn't this make much more sense?:

  if(sensorValue == HIGH){
    digitalWrite(13,HIGH);
  }
  if(sensorValue == LOW){
    digitalWrite(13,LOW);
  }

Thank you for the second part; that does make alot more intuitive sense. I only started with Arduino in the past hour or so, thanks for the tips!

With oldVal, I only made it a global as I wasn't sure how to keep it the same between iterations of the loop; oldVal is storing the last state of the switch, so if I just get rid of the local oldVal variable, my code will work as expected?

Multi:
With oldVal, I only made it a global as I wasn't sure how to keep it the same between iterations of the loop; oldVal is storing the last state of the switch, so if I just get rid of the local oldVal variable, my code will work as expected?

Globals are probably the most beginner friendly way to do this and your approach will work fine once you fix the bugs.

There is another option, making the oldVal static, but I think it makes the code less easy to understand unless you are familiar with how static variables work so you're probably better with the global. Globals just make sense to me intuitively, static variables don't. Here's information on static in case you're curious:
https://www.arduino.cc/en/Reference/Static