taking two sensor readings over time

Hi,

Im pretty new to ardunio and programming, so there may be a better way to achive what im looking to do but maybe you can help me out.

Im using a IR sensor to light up leds in a sequence based on proximity to the sensor, but when you jump straight into the closest range, all the leds come in at once. what id like to do is for arduino to reconise that this has happened and initiate the sequence of LED’s coming on.

The way I have thought about doing this is to take two values from the sensor, currentValue and previousValue, compare them, and if theres a big jump, initial a digitalWrite sequence with delays in there.

I guess I need to have to variables stored from the sensor, current and previous one

and the code read something like
if ((outputValueOld > 20) && (outputValueNew <150)) {
Do something
}

How can I store a previous reading from the sensor?

thanks in advance, Phil

Ah that's great, just what I was looking for.

After a good lot of searching im still none the wiser

does anyone have any useful tutorials on Arrays, im finding it hard to work out how arduino knows where to get values from and how to deal with them for my problem above.

Thanks, Phil

Although I don’t know all about Arduino programming, I am a fairly decent programmer in C++/Java.

You may not need to use an array to store your previous values, if you are only comparing the 2 previous values.

In this case, in your loop() function, (or if you are using a separate function, just put this in the for/while loop or whatever)

First off, declare your variables to be global, so they can be used in any function. Do this by putting the variable name BEFORE any of the functions.

Example:

int oldValue;
int currentValue;

void setup()
{
oldValue = 0;
currentValue = 0;

//Set the values to be 0 when we first start
}

void loop()
{
oldValue = currentValue; //Every iteration the Arduino runs through, //assign oldValue to be what the previous currentValue is.
currentValue = getValueFromIR()  //assign currentValue to the what //the function returns

/*
Okay, so I used getValueFromIR(), because I don't know how the exact function for the arduino to read a analog/digital value. That's up to you. It might be like digitalRead(pinNumber) or something?
*/


//do our logic tests
if( (oldValue > 20) && (currentValue <150))
{
blowUpWorld(); //Don't do that please. :(
}


} //End Loop();

What this will basically do, is each time the Arduino runs through the “loop” cycle, (which as I understand, is constantly, unless hung up in another “loop” in a separate function) it will update the variables for you to use.

You MAY wish to wait for the arduino to run through at least one loop cycle first, because in the first iteration, the oldValue will always be 0.

In order to wait for at least 1 iteration, simply add a counter variable, (unless the Arduino environment has something for that)

I hope that makes sense…

If someone who knows more about the Arduino environment chimes in about why this won’t work, then oopsie-daisy.

Instead of using traditional arrays, you might try saving the data in a string variable that is modified as needed to store the desired info.

Thanks nightfigther, got it working ;D

Code below:

int analogInPin = A0; // define IR Sensor
int analogOutPin = 9; // define LED
int oldValue;
int currentValue;

void setup() {
  oldValue = 0; //Set the values to be 0 when we first start
  currentValue = 0;
  Serial.begin(9600); 


}

void loop() {
oldValue = currentValue; //Every iteration the Arduino runs through, //assign oldValue to be what the previous currentValue is.
currentValue = analogRead(analogInPin);  //assign currentValue to the what //the function returns
 


//do our logic tests
if((oldValue < 50) && (currentValue >= 400)) {
digitalWrite(analogOutPin, HIGH);
delay(1000);
}
else
{
digitalWrite(analogOutPin, LOW);
}
// print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.println(currentValue);      
   
  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(1000);     

} //End Loop();

Oh cool, I was able to help someone, even though I'm a noob! Haha.

BTW- In your code at the end, it says "Wait 10 milliseconds..." but you actually wait 1 whole second.

Also, now that I am thinking about it, you might want to not use the name "current"Value, because current could mean the actual...CURRENT of the pin or something.

I ran into this problem a year ago, working with a cRIO, Jaguar Speed Controller, and an impeller... I was grabbing the actual CURRENT through a speed controller, and I ended up having about 9 variables... they all used current...so one of my variables was "currentCurrent".

Not good. ;D