What am I missing?

Hey guys, clearly I’m not understanding something here. This script has no real purpose except to understand how it works.

What I dont get is that I want inputVariable1 to latch, ie, stay on, or stay off. The problem I have is that even though the condition isn’t met to make it go HIGH, it goes HIGH as soon as I take away the magnet from the hall effect sensor, Even though the value is never <500

The hall effect sensor sits at 515 normally, and I have coded in a bit of a dead spot between 500 - 530.

Surely inputVariable 1 should just stay at it’s state, what’s changing it back to low without an input?

void setup() {
  // put your setup code here, to run once:
int inputVariable1 = LOW;
Serial.begin(9600);
}

void loop() {
  int inputVariable1;
if (analogRead(A1) <500 && inputVariable1 == LOW) {
 inputVariable1 = HIGH;
}
if (analogRead(A1) >530 && inputVariable1 == HIGH){
    inputVariable1 = LOW;
}
if (inputVariable1 == LOW){
  digitalWrite(13, LOW);
}
if (inputVariable1 == HIGH){
  digitalWrite(13, HIGH);
}
Serial.println (inputVariable1) & (analogRead(A1));
Serial.println (analogRead(A1))
;
}
int inputVariable1;

This statement will create a new variable called inputVariable1 each time through loop() and its value may be anything as you have not initialised it explicitly. Either make it a global variable and remove the int in the statement or use

static int inputVariable1 == LOW;

to set its value when first declared and to retain its value, whatever it is, next time through loop()

You are calling analogRead() a few times and each may give a different value. It would probably make more sense to read it once and store the value so you use the same value in all the tests and in what you print.

However I suspect your main problem is with this code

void loop() {
 int inputVariable1;

which creates a new variable every time the function is called and the previous value is lost. Also, the variable created within a function is only visible inside the function.

If you want to create a global variable that is visible throughout your program you should declare it outside of any functions - the usual place is before setup()

The other option is to define the variable within the function as

void loop() {
 static int inputVariable1;

which means that it rememebers its value between calls to loop(). But it will still only be visible within loop()

...R

Thanks guys, the static int worked a treat.

So should I only be reading the analog input once per loop? How would I go about doing that?

Something like

sensor = analogRead(A1)

And then just using sensor, instead of analogRead(a1) everytime? Although this seems like I just gave it an alias, not actually doing it any different.

jakeoo: Something like

sensor = analogRead(A1)

And then just using sensor, instead of analogRead(a1) everytime? Although this seems like I just gave it an alias, not actually doing it any different.

The difference is that it will have the same value until you perform the assignment again.

static int inputVariable1 == LOW;

Hmmm.

Bum - did I really type that ?