Analog value - High, low and in between

Hi folks

I have some trouble with this code

#include <LiquidCrystal.h>

int val;
int lastVal = 0;               //set initial value to 0
int calcVal;                   //calulated value (val - lastVal) for "goes down"
int calc2Val;                  //calulated value (lastVal - val + 300) for "goes up"

int sensorValue = 0;        // value read from the pot

LiquidCrystal lcd(8, 7, 5, 4, 3, 2);  // initialize the library with the numbers of the interface pins

void setup()
{
 
  lcd.begin(20, 4);            // set up the LCD's number of columns and rows: 
  Serial.begin( 9600 );          // initialize the serial communications:
}

void loop(){
  sensorValue = analogRead(A2);  
  // print the results to the serial monitor:
  Serial.print("sensor = ");                       
  Serial.println(sensorValue);  
  Serial.println(calcVal);  
  Serial.println(calc2Val);    
 

  delay(2000);  

  val = analogRead(A2);
  if (val > 850)                  //if the read value is more than 850
  {
    lcd.setCursor(0, 0);
    lcd.print("high");
  }

   else if (calc2Val > 0)          //if the calulated value raises more than 300
  {
    lcd.setCursor(0, 2);
    lcd.print("goes up");
  }

  else if (calcVal > 300)          //if the calulated value falls more than 300
  {
    lcd.setCursor(0, 1);
    lcd.print("goes down");
  }
  
   else if (val < 300)              //if the read value is less than 300
  {
    lcd.setCursor(0, 3);
    lcd.print("down");
  }

  calcVal = val - lastVal;
  calc2Val = lastVal - val + 300;
  val = lastVal;                    // 'save' the value of val to next iteration
  delay(100);
}

I have a LDR connected, and i want the program to print out when the value is high, when it is low, when it on the way down (falls by 300) and when it is on the up(raises by 300).

The problem is doesnt work as intended... at all...

It prints out when it is high (above 850), so that is OK
It prints out when it "goes down", so that also OK
It prints out "goes up" a second after it prints "goes down". aint OK!
And the "down" doesnt come up at all!! in Serial monitor i can see that the value does come under 300...?

Im kind of confused

Could the "swicth case" be the way to do it, or am i totally off? :roll_eyes:

Why are you acting on the values in calcVal and calc2Val (lousy names) BEFORE you calculate them?

does this statement

val = lastVal; // 'save' the value of val to next iteration

do what you say it does? I don't think so...

Hi kucza83,
why don't you work only with the current value and old value, instead of calculating two variables:

  val = analogRead(A2);

  if (val > 850)                  //if the read value is more than 850
  {
    lcd.print("high");
  }

  if (val < 300 )
  {
    lcd.print("low");
  }

  if ((val - oldVal)  > 300)          //if the calulated value falls more than 300
  {
    lcd.print("goes up");
  }
  
  if ( (val - oldVal) < -300)              //if the read value is less than 300
  {
    print("goes down");
  }

  oldVal = val;

Old topic i know, but no answer if it working already?
I been searching for the same answer ... how to let hem know when the value goes down...

https://forum.arduino.cc/index.php?topic=555649.msg3793800#msg3793800

thanks alot