If (change in analog value)

I would like to write a sketch where the condition of an if statement is a comparision with a current analog value and analog value of that pin on previous loop.

What stops you?

If you look at the state change detection example, you can see how the previous value is remembered. You can compare that remembered value with the current value. The state change detection example is for digital inputs but the same principle applies to analogue inputs.

Note that analogue readings will always have some fluctuation so you have to add a little bit of a window.

Create a variable called previousValue, store the value in the variable.

Is this what you suggesting?

type or paste code here//if(change in analog value)
int photocellPin1 = 1;
int sel_0 = 12;
int m_photocellPin1;
int prevPhotocellPin1;

void setup() {
    Serial.begin(9600);
    pinMode(photocellPin1, INPUT);
    pinMode(sel_0, OUTPUT);

  }
void loop() {
  int m_photocellPin1 = analogRead(photocellPin1);
    Serial.println("========================================================");
    Serial.println("LDR 1\tLDR 2\tLDR 3\tLDR 4\tLDR 5\tLDR 6\tLDR 7 ");
    Serial.println("========================================================");
    Serial.print ( m_photocellPin1  );
    Serial.print ( "\t");

    if ( m_photocellPin1 - 50 < prevPhotocellPin1) {
      digitalWrite (sel_0, HIGH);
    }
    else if (photocellPin1 >= prevPhotocellPin1) {
      digitalWrite (sel_0, LOW);
    }
    delay (1000);
    prevPhotocellPin1 = m_photocellPin1;
  }

And when you tested it what happened ?

for some reason its always high

execpt when I change the code to both conditional write LOW

I changed the direction of the comparator and that works.
My next question is how can I get it to stay for more than one loop

//if(change in analog value)
int photocellPin1 = 1;
int sel_0 = 12;
int m_photocellPin1;
int prevPhotocellPin1;

void setup() {
  Serial.begin(9600);
  pinMode(photocellPin1, INPUT);
  pinMode(sel_0, OUTPUT);

}
void loop() {
  int m_photocellPin1 = analogRead(photocellPin1);
  Serial.println("========================================================");
  Serial.println("LDR 1\tLDR 2\tLDR 3\tLDR 4\tLDR 5\tLDR 6\tLDR 7 ");
  Serial.println("========================================================");
  Serial.print ( m_photocellPin1  );
  Serial.print ( "\t");
  Serial.print (prevPhotocellPin1);
  Serial.print ( "\t");


  if ( m_photocellPin1 - 50 > prevPhotocellPin1) {
    digitalWrite (sel_0, HIGH);
  }
  else if (photocellPin1 + 50 < prevPhotocellPin1) {
    digitalWrite (sel_0, LOW);
  }
  delay (1000);
  prevPhotocellPin1 = m_photocellPin1;
}

Try

    if ( m_photocellPin1 < prevPhotocellPin1) {
      digitalWrite (sel_0, HIGH);
    }
    else if (photocellPin1 >= prevPhotocellPin1) {
      digitalWrite (sel_0, LOW);
    }
    delay (1000);
    prevPhotocellPin1 = m_photocellPin1 + 10;

No, that’s not it but a version of which should.

This did the trick, and it holds its state for until a 15 analog change, not only one loop
Thanks for the help with the details and the support arduino Forum!

//if(change in analog value)
int photocellPin1 = 1;
int sel_0 = 12;
int m_photocellPin1;
int prevPhotocellPin1;

void setup() {
  Serial.begin(9600);
  pinMode(photocellPin1, INPUT);
  pinMode(sel_0, OUTPUT);

}
void loop() {
  int m_photocellPin1 = analogRead(photocellPin1);
  Serial.println("========================================================");
  Serial.println("LDR 1\tLDR 2\tLDR 3\tLDR 4\tLDR 5\tLDR 6\tLDR 7 ");
  Serial.println("========================================================");
  Serial.print ( m_photocellPin1  );
  Serial.print ( "\t");
  Serial.print (prevPhotocellPin1);
  Serial.print ( "\t");

    if ( m_photocellPin1+25 > prevPhotocellPin1) {
      digitalWrite (sel_0, HIGH);
      prevPhotocellPin1 = m_photocellPin1  ;
    }
    else if (photocellPin1 <= prevPhotocellPin1) {
      digitalWrite (sel_0, LOW);
    }
    delay (100);
    
    
}

I think you marked the topic as solved too early.
Look, in the second condition you are compared pin number rather than value:

it is obviously wrong, because pin number is not changed :slight_smile:

In general, it's good to choose more descriptive names for variables. The variable for the value should more obviously be named something like PhotocellValue and prevPhotocellValue rather than prevPhotocellPin1

good catch, but the names are fine. Also worked on actual hardware. Why not try doing some project with the hardware with your knowledge if your not already.

Also any relation to Alto777, because that user was really patronizingl and commenting all my post. Kinda over it

Are you sure? :slight_smile:
You don't see that this line is wrong?

What is the point of comparing the pin number with the read value?

OMG forgive me for trying to help you. I will review the very long thread(s) you had with your onion garlic project to see just how patronizing I must have been.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.