How to Change a INT (not associated) with a Pin

Hopefully a basic question.

I've been looking through the "Language Reference" page for my solution, but can't seem to find it.

I'm looking to change in programming language the state of an "int" variable

example:

int SWITCH = 0;

void loop() {

if (Switch == 0) {... } if (Switch == 1) {... }

I want to program in other if statements depending on the situation to change the "Switch int". The int Switch is not associated with a pin. It's strictly for programming. Using it as a variable that I can program around.

Sorry if that doesn't make sense.

I don't understand your issue. That is exactly what variables are for. Just set it to whatever value you want and use it in your if tests. Have you tried to do this yet?

By the way, variables are never ever "associated with a pin". They're just convenient places to hold numbers. If that number happens to be used later as a pin number then so be it, but that doesn't mean the variable is anything other than a convenient way to name a number.

I suppose the question is what language/text do I use to change the variable not associated with a pin?

digitalWrite(Switch == 0); analogwrite(Switch == 0);

etc... ?

Not sure what language to use to manually change an variable.

Sorry.. New...

Every variable change I've seen has been associated with a pin in the examples

How about just:

myVariable = 3;

or

myVariable = 5;

You just assign a new value.

You should note that in your code posted above yu have the variable named SWITCH but later try to refer to it as Switch. C++ is case sensitive so those two are not the same.

What do you mean associated with a pin? You keep using that phrase, maybe I'm not understanding what you think that means.

SWITCH = 2;

is exactly what I was looking for. THANK YOU.

I know that it was a stupid question, but I thought i needed a "write" or something with it.

Yes I also know that script is case sensitive. I'm doing to many things at once and missed it.

What I meant by associated with a pin is... I thought that variables were only changing based off of a pin status. High or Low via digitalRead etc... I was unclear how to change it just by saying "SWITCH = 2;".

I know it seemed like a silly question, but you were a help.

Thank you again.

Delta_G is right...just think of a variable as a chunk of memory with a name. Try this program:

int LEDPIN = 13;   // Onboard LED on Arduino board

void setup() {
  // initialize serial:
  pinMode(LEDPIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Enter 0 or 1");  // Tell user what to do
}

void loop() {
  int state = 0;    // Treat the state as Off to start
  int Switch;       // Not a good name, because C has a keyword "switch"
  
  while (Serial.available() > 0) {
    Switch = Serial.parseInt() ;
    if (Switch == 0) {
      Serial.println("You entered 0, LED Off");
      state = LOW;                 // Change the state
    } else {
      if (Switch == 1) {
        Serial.println("You entered 1, LED On");
        state = HIGH;
      } else {
        Serial.println("You can't follow directions");
      }
    }
    digitalWrite(LEDPIN, state);
  }
}

Note its size (about 3414 bytes) and how "responsive" it is. After you've played with it for a while, change the line that reads:

    Switch = Serial.parseInt() ;

which uses the parseInt() function to change the ASCII key code for the character you typed to an int data type. Change that line to:

    Switch = Serial.read() - '0' ;

It performs as before, but the code size has shrunk to 2968 bytes and is a little faster to respond. Moral: There's more than one way to skin a cat when programming!

Ok. I finally had time to try something and needless to say it's not working the way I want it to. So I'm feeling silly.

What I'm looking for:

When ---> ContactState = HIGH // I want the LED Strip to light for 5 Seconds then shut off... And not repeat until the ContactState = LOW, then HIGH again. (Door Closes then Opens again).

I'm cut my sketch to highlight the portion I'm talking about.

const int LEDStrip = 2;
const int Contact = 5;

int ContactState = LOW;
int LEDState = LOW;

void setup() {
pinMode(LEDStrip, OUTPUT);
pinMode(Contact, INPUT);
}

void loop() {
ContactState = digitalRead(Contact);

if (ContactState == HIGH && LEDState == LOW){  // WHEN THE DOOR OPENS, TURN ON THE LED STRIP AND CHANGE THE LEDSTATE VARIABLE TO HIGH (SO THE LED ONLY TIMES FOR 5 SECONDS AND DOESN'T REPEAT).
  LEDState == HIGH;
  digitalWrite(LEDStrip, HIGH);
  delay(5000);
  digitalWrite(LEDStrip, LOW);
  }
if (ContactState == LOW){  // WHEN THE DOOR CLOSES, CHANGE THE LEDSTATE VARIABLE TO LOW
  LEDState == LOW;
  digitalWrite(LEDStrip, LOW);  // Redundant turning the LED Strip Off.
  }
}

I was under the impression (apparently wrong impressions :) ) that once the LED Strip Turns on that it changes the LEDState Variable to HIGH. Then when the script reads again the Contact State and the LED State of LOW would no longer be true, so it should skip over it until both states become true again.

It's as if and I suppose most likely that the Variable LEDState is just being ignored.

Any help is appreciated.

Thanks.

if (ContactState == HIGH && LEDState == LOW)//correct for conditional test
LEDState == HIGH;//incorrect for assignment
LEDState == LOW;//incorrect for assignment

You are making assignments here and not a comparisons. One equal sign.

You correctly understand that == is required in the if conditional test, but after the test, when you actually want to change the state you are assigning a value, not comparing it.

cattledog: You correctly understand that == is required in the if conditional test, but after the test, when you actually want to change the state you are assigning a value, not comparing it.

Thank you for the help. I did try both one = and two == in place just in case before posting too. I noticed earlier in the post that Delta just showed 1 =. But unfortunately it still isn't working. The light stays on until I close the contact (LOW). And doesn't shut off after the 5 seconds.

I have modified your software to have the = for assignment, and changed the led to the internal pin13 led and the contact pin 5 to INPUT_PULLUP so it doesn't float in my setup. The led turns off after 5 seconds.

You'll need to check how your led strip and contact switch are wired.

const int LEDStrip = 13;
const int Contact = 5;

int ContactState = HIGH;
int LEDState = LOW;

void setup() {
pinMode(LEDStrip, OUTPUT);
pinMode(Contact, INPUT_PULLUP);
}

void loop() {
ContactState = digitalRead(Contact);

if (ContactState == LOW && LEDState == LOW){  // WHEN THE DOOR OPENS, TURN ON THE LED STRIP AND CHANGE THE LEDSTATE VARIABLE TO HIGH (SO THE LED ONLY TIMES FOR 5 SECONDS AND DOESN'T REPEAT).
  LEDState = HIGH;
  digitalWrite(LEDStrip, HIGH);
  delay(5000);
  digitalWrite(LEDStrip, LOW);
  }
if (ContactState == HIGH){  // WHEN THE DOOR CLOSES, CHANGE THE LEDSTATE VARIABLE TO LOW
  LEDState = LOW;
  digitalWrite(LEDStrip, LOW);  // Redundant turning the LED Strip Off.
  }
}

cattledog: the contact pin 5 to INPUT_PULLUP so it doesn't float in my setup. The led turns off after 5 seconds.

Winner Winner! Thank You!

Just changing the Contact to INPUT_PULLUP instead of just an INPUT worked!