If Value has changed...

hey, I'm trying to write an If statement that basically checks if the value has changes since the last loop-through.

I can't find any direct statement, but I assume you'd use 2 variable and have it write the new value upon match.

But I can't seem to get me head around how the code would look.

Suggestions?

This might help.

I wrote this for a remote control project.

Basically you set up a variable (see "int i = 0;" in my code).

Then use "i = i+1;" (in loop) to increment the value

Then use the "If" statement to check the value of "i" and add whatever action you want.

You can also reset the value of "i" when it reaches a certain number of your choosing.

Example

if (i >= 4) {
i = 0;

Hope that helps.

// IR Receiver 6

// by Sid Guglielmino
// Created 30 May 2008

// A Remote Control sends IR signal to an IR Sensor 
// Output from the sensor turns on a LED on Pin 2 

// Lights up 1, 2 or 3 leds according to number of Remote Control button presses
// First press lights led 1, second press lights led 2 etc
// Fourth button press turns all off and in reverse sequence

// Uses IR Receiver Module RPM7100 series ZD 1952 Ex Jaycar
// Sensor Pin 1 Output, Pin 2 GND, Pin 3 Vcc

// WORKS OK


int sensorOutput = 0;                                // select ANALOGUE input pin for the IR sensor Output Pin 
int ledPin1 = 5;                                     // select the pin for the LED 
int ledPin2 = 6;
int ledPin3 = 7;
int val = 0;  
int amp = 0;
int i = 0;                                           // Variable to store number of signals received
  
void setup() {  
  pinMode(ledPin1, OUTPUT);                           // declare the ledPin as an OUTPUT 
  pinMode(ledPin2, OUTPUT);                           // declare the ledPin as an OUTPUT 
  pinMode(ledPin3, OUTPUT);                           // declare the ledPin as an OUTPUT 
}  
  
void loop() {  
  
  val = analogRead(sensorOutput);  
  amp = (val >= 1024) ? val - 1024 : 1024 - val; 
    
  if (amp > 1000) {  
    digitalWrite(ledPin1, HIGH);                      // If IR signal detected turn on ledPin        
    delay(400);                                       // Ignore IR signal for a short time
    i = i+1;                                          // Acts like a button debouncer
  }                                                   // because Remote Controls send repeating pulsing signal


if (i == 2) {  
   digitalWrite(ledPin2, HIGH);                      // If IR signal detected 3 times, turn on ledPin2
}


if (i == 3) {
   digitalWrite(ledPin3, HIGH);   
}

if (i >= 4) {
   digitalWrite(ledPin3, LOW);
   delay(500);
   digitalWrite(ledPin2, LOW);
   delay(500);
   digitalWrite(ledPin1, LOW);
   delay(500);
    i = 0;
     
}
 
}

Yeah, that would work for most purposes, but sadly, I'm not looking for exactly that,

What I have going is a PHP interface running to a VB Backend, which then sends data over the com port to the Arduino, which then goes to a shift register.

So what I'm looking for is something that'll check if it's been updated, so that the interface can write it to the shift register.

The problem I see is that the end user will see the interface as "Next Light ON or OFF" which then sets the variable.
The problem being that if the last was a 0 and the new one's a zero, then it won't "notice"

unsigned int oldvalue = newvalue = 0xFFFF;

newvalue = read_my_data();
if (newvalue != oldvalue) {
   update_my_data(newvalue);
   oldvalue = newvalue;
}
1 Like