Simple if then statment not working!

I'm new to Arduino and trying to make a switch that will change a value in a variable. I have two Serial print commands at the end of the loop to see what is changing, one being the analog switch value and the other being the stored variable. When I close the switch the analog switch value changes but the variable stays the same so I know it isn't a problem with the wiring. I have no idea what is wrong with the code so if anyone could tell me that would be great.

Code:

void setup() {
  Serial.begin(9600);
}
int door = 0;
void loop() {
  int sensor = analogRead(A0);
  
  if ( sensor >= 1219 ) {
    door = 1;
  }
  else if ( sensor < 1219 ) {
    door = 0;
  }
    Serial.print(sensor);
    Serial.print(' ');
    Serial.print(door);
    Serial.print('\n');
    delay(30);
}

The door variable stays at 0.

analogRead returns a value 0 - 1023 ... so it can never be >= 1219.

If you are just trying to see if a switch is on or off, you can use digitalRead, rather the analogRead.

Hello
This task looks like a school assigment. Is´nt it?
Have a nice day and enjoy coding in C++.

Also, your second if is not needed.

  if ( sensor >= 1219 ) {
    door = 1;
  }
  else if ( sensor < 1219 ) {
    door = 0;
  }

If the value is not >= X, it must be < X, so there is no need to test that.

Haha I agree. Looks like one to me too

Seems he joined an hour before he posted, took three minutes to view five posts in three threads and posted this thread. How early in school is programming taught these days?

@caidenjk
1.

if ( sensor >= 1219 ) 
{
    door = 1;
}
else 
{
    if ( sensor < 1219 ) 
    {
        door = 0;
    }
}

So, embedded if() statement with else() is not needed.

2. Your sensor value cannot go above 1023. How?
adc-12

When your sensor produces 0V as output, then ADC delivers 0s on its all output bits (b0 - b9). That is ADC value is: 0000000000 in binary = 0 in decimal.

When your sensor produces 5V as output, then ADC delivers 1s on its all output bits (b0 - b9). That is ADC value is: 1111111111 in binary = 1023 in decimal.

Prcatically, your sensor will produce an outpur between 0V and 5V; so, the ADC value will always remain within 0 to 1023.

Hello caidenjk
Try this example sketch. You can rearrange the limit to you needs.

void setup() {
  Serial.begin(9600);
}
constexpr int Limit {512};
void loop() {
  int sensor = analogRead(A0);
    Serial.print(sensor);
    Serial.print(' ');
    Serial.print(sensor<Limit?"0":"1");
    Serial.print('\n');
    delay(30);
}

Have a nice day and enjoy coding in C++.