Compare "Current" to "Previous" Sensor Value

Hi there. I have done versions of this before, but for some reason can't wrap my head around this. I am trying to compare an ever changing variable, the most recent number it gives me to the second most recent.

More specifically, I am adding up the total of two sensors in 'fuzzy logic sets'.
So if I have a sensor that prints "It's Dark!" when its dark and "It's Light!" when it's light, if it is dark, my sensor will just keep on printing out over and over: "It's Dark! It's Dark! It's Dark! It's Dark!..."

My solution is to subtract the CURRENT value of the sensor from the LAST value, and if that number is ZERO, then don't do anything... because zero means it got two of the same number, so nothing in the external world has changed.

I will include my entire code, but the area I am addressing is around the first "while" you see, within void loop, ... cheers.

//Thinking Machine - Connor Crawford
//Plants Require Allot Of Care
//----------------------------
//This is a machine that prompts you when the lighting isn't ideal for the plant
//which is inside it, so you are required to move the entire machine to a more
//optimal spot. Carefull as you do so however, you need to keep the machine
//and therefore plant, steady and upright.
//
//The "Serial Print Lines" of the robot's responces to it's environment, are
//intended to be auditory cues to the user.


int NorthSouthTilt;
int EastWestTilt;
int LightValue;

int action = 1;

int stabilityNS;
int stabilityEW;
int targetLight;

int difference;
int lastActionState; //previous "action number"
int actionState; //current "action number"
int led = 8;


void setup() {                
  Serial.begin(9600);
  pinMode(led, OUTPUT);     
}

void loop() {
  //Sensors keep readind thier environments//
  NorthSouthTilt = analogRead(A0);
  EastWestTilt = analogRead(A1);
  LightValue = analogRead(A5);
  

  //voids to fuzzy the sensor data into easy sets > numbers//
  stabilityFuzzyNS();
  stabilityFuzzyEW();
  lightFuzzy();
  
  //math. Adding the fuzzies together to make a decision//
  action = stabilityNS + stabilityEW + targetLight; //add fuzzies together to make a choice
  actionState  = stabilityNS + stabilityEW + targetLight;//save this also as the current state
  
  difference = actionState - lastActionState;
  while (difference = 0){
     digitalWrite(led, HIGH);
  }
  lastActionState = actionState;
  Serial.println(difference);
  
  actionTable();
}


void stabilityFuzzyNS() {
      /////////////////////////////////////////////////////
      //  0 is stable, upright.
      /////////////////////////////////////////////////////
      if(NorthSouthTilt <= 345 && NorthSouthTilt >= 335){
      stabilityNS = 0;
      }
      /////////////////////////////////////////////////////
      //Tilt North, 1 = carefull, 2 = woah buddy! 3 = FUBAR
      /////////////////////////////////////////////////////
      if(NorthSouthTilt <= 365 && NorthSouthTilt >= 346){
      stabilityNS = 1;
      }
      if(NorthSouthTilt <= 405 && NorthSouthTilt >= 366){
      stabilityNS = 2;
      }
      if(NorthSouthTilt <= 426 && NorthSouthTilt >= 406){
      stabilityNS = 3;
      }
      /////////////////////////////////////////////////////
      //Tilt South, 1 = carefull, 2 = woah buddy! 3 = FUBAR
      /////////////////////////////////////////////////////
      if(NorthSouthTilt <= 334 && NorthSouthTilt >= 320 ){
      stabilityNS = 1;
      }
      if(NorthSouthTilt <= 319 && NorthSouthTilt >= 260){
      stabilityNS = 2;
      }
      if(NorthSouthTilt <= 280 && NorthSouthTilt >= 259){
      stabilityNS = 3;
      }
}



void stabilityFuzzyEW() {
     /////////////////////////////////////////////////////
      //  0 is stable, upright.
      /////////////////////////////////////////////////////
      if(EastWestTilt <= 345 && EastWestTilt >= 335){
      stabilityEW = 0;
      }
      /////////////////////////////////////////////////////
      //Tilt EAST, 1 = carefull, 2 = woah buddy! 3 = FUBAR
      /////////////////////////////////////////////////////
      if(EastWestTilt <= 365 && EastWestTilt >= 346){
      stabilityEW = 1;
      }
      if(EastWestTilt <= 405 && EastWestTilt >= 366){
      stabilityEW = 2;
      }
      if(EastWestTilt <= 426 && EastWestTilt >= 406){
      stabilityEW = 3;
      }
      /////////////////////////////////////////////////////
      //Tilt WEST, 1 = carefull, 2 = woah buddy! 3 = FUBAR
      /////////////////////////////////////////////////////
      if(EastWestTilt <= 334 && EastWestTilt >= 320 ){
      stabilityEW = 1;
      }
      if(EastWestTilt <= 319 && EastWestTilt >= 260){
      stabilityEW = 2;
      }
      if(EastWestTilt <= 280 && EastWestTilt >= 259){
      stabilityEW = 3;
      }  
}

void lightFuzzy() {
      /////////////////////////////////////////////////////
      //Light Value, 1 = Optimum, 20 = Dimming 30 = Pitch Black
      /////////////////////////////////////////////////////
      if(LightValue <= 1005 && LightValue >= 800){
      targetLight = 1;
  }
      if(LightValue <= 799 && LightValue >= 600){
      targetLight = 20;
  }
      if(LightValue <= 599 && LightValue >= 540){
      targetLight = 30;
  }
}


void actionTable() {
  switch(action){
    
    /*case 0:
    //aint no case ZERO
    break;*/
    
    /////////////////////////////////////
    //if the lights are PERFECT//////////
    /////////////////////////////////////
    case 1:
    Serial.println("This is perfect! Thank you, surely here the plant will thrive. Now leave us be, I'll call for you if I need you.");
    break;
    
    case 2:
    Serial.println("This is almost half decent, be sure we are sitting level, otherwise this plant will grow wonky.");
    break;
    
    case 3:
    Serial.println("Ya, uh, not to be rude, but does this look right to you?! Ya I didn't think so. CHOP CHOP LET'S GET US SITTING PROPER!");
    break;
    
    case 4:
    Serial.println("Wow. You realize I might have died, and MY plant may be dead now too, no thanks to you. Don't just stand there, pick me back up.");
    break;
    
    case 5:
    Serial.println("WOAH!");
    break;
    
    case 6:
    Serial.println("AAAAAAAAAAA!");
    break;

    case 7:
    Serial.println("AAAAAAAAAAA!");
    break;
    
    
    /////////////////////////////////////
    //if the lights are dim...///////////
    /////////////////////////////////////
          case 20:
          Serial.println("Hey genius! Could use a bit more light here, starting to worry about this plant..., You'll have to move us. Can't sit here all day.");
          break;
          
          case 21:
          Serial.println("Woah, carefull with us, maybe your primitve human eyes havent adjusted to the shitty lighting in here.");
          break;
          
          case 22:
          Serial.println("AAAH! JESUS WHAT'S WRONG WITH YOU. YOU'LL KILL US! The lighting isn't the ONLY thing that's dim in here.");
          break;
          
          case 23:
          Serial.println("Fantastic. I can't belive my only purpose is to keep this plant alive and now look at us. What a joke of an existance.");
          break;
          
          case 24:
          Serial.println("AAAAH!");
          break;
          
          case 25:
          Serial.println("AAAAH!");
          break;
          
          case 26:
          Serial.println("AAAAH!");
          break;
          
                /////////////////////////////////////
                //If there is NO light///////////////
                /////////////////////////////////////
                case 30:
                Serial.println("Wow! Do you really expect the plant to live with no light at all? Move us into light!");
                break;
                
                case 31:
                Serial.println("Carefull! Not only is there no light, but you're going to drop us. Who the hell are you? Hurry up and move us into the light!");
                break;
                
                case 32:
                Serial.println("Oh my god, this is how it end, isn't it? I'm going to die. It's been a terrible life.");
                break;
            
                case 33:
                Serial.println("Great, this is just great. How long do I have to lay here? Do I run on batteries? Just kill me now.");
                break;
                
                case 34:
                Serial.println("AAAAAAAAAH!");
                break;
                
                case 35:
                Serial.println("AAAAAAAAAH!");
                break;
            
                case 36:
                Serial.println("AAAAAAAAAH!");
                break;
                

    default:
    digitalWrite(led, HIGH); 
  }
}

Make two new variable, like new and old.

Before calling your function that return LIGHT or DARK, adress old = ''variable name that store LIGHT or DARK''.
After calling your function, adress new = ''''variable name that store LIGHT or DARK''.

Then, make a if before doing anything else. IF (new!=old){ current program that print DARK or LIGHT } ELSE{ NOTHING}

This iis a program I just made that use this same logic:

void loop(){
time_2 = millis();
  last_st = state;
  read_state();
  st = state;
  if(st != last_st){
    process_state();
    process_display();
    //delay(45);
    time_1 = millis();
  }
  time = time_2 - time_1;
  time = time/1000;
  if(time >=10){
    pinMode( lcdBacklightPin, OUTPUT);
  }else{
    pinMode( lcdBacklightPin, INPUT );
  }
  delay(90);

}

Jonnyboy,
I am trying to follow the outline you gave me which is the similar to others I have seen. However, now my results or unwieldily and the code is carrying out BOTH the if{ } AND the else{ }....
not sure what to do, can you give any more information? I tried like this:

int lastActionState; //previous "action number"
int actionState; //current "action number"

int stabilityNS;
int stabilityEW;
int targetLight;

void setup() {                
  Serial.begin(9600);
  pinMode(led, OUTPUT);     
}

void loop() {
  lastActionState = action;
  action = stabilityNS + stabilityEW + targetLight;
  actionState = action;
  if(actionState != lastActionState){
      actionTable();
  }
  else{
  digitalWrite(led, HIGH);
  }  
}

Some servo/pot test code that compares current value to previous value to establish a deadband to minimize the servo hunting.

//zoomkat dual pot/servo test 12-29-12

#include <Servo.h> 
Servo myservo1;
Servo myservo2;

int potpin1 = 0;  //my pot pin
int potpin2 = 1;

int newval1, oldval1;
int newval2, oldval2;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  Serial.println("testing dual pot servo");  
}

void loop() 
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){  
    myservo1.write(newval1);
    Serial.print("1- ");
    Serial.println(newval1);
    oldval1=newval1;
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print("2- ");    
    Serial.println(newval2);
    oldval2=newval2;
  }
  delay(50);
}