I'm trying to compare the value of a variable from the previous loop to the value in the current loop. Clearly, it's not working, I'm not sure what I'm doing wrong.
The variable in question is 'lastVal', the value is assigned by analogRead(0). In the if/else statement, the sketch only ever performs the 'else' portion:
int x;
int led = 13;
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop()
{
int lastVal;
x = analogRead(0);
if (x < lastVal)
{
digitalWrite(led, HIGH);
Serial.print("x: ");
Serial.print(x, DEC);
Serial.println(". Light On.");
}
else
{
digitalWrite(led, LOW);
Serial.print("x: ");
Serial.println(x, DEC);
}
x = lastVal;
delay(100);
}
Your help is greatly appreciated.