What is the code to define a serial return?

Hello,

I am running an analogue sensor which is returning a distance value every .5 second.

Is it possible to add a line of code which will only return an exceptional reading i.e over 50 or less than 50 of the sensor reading of , for example 350?

//Reads and analog sensor and displays the value int sensePin = 0;

void setup() { //Note: We don't need to specifiy sensePin as an //input, since it defaults to that when we read it

//This is the default value, but we can set it anyways analogReference(DEFAULT); //5V Reference on UNO

//Allows us to listen to serial communications from the arduino Serial.begin(9600); }

void loop() { // print the button state to a serial terminal Serial.println(analogRead(sensePin)); delay(500); //wait half a second, then print again. }

void loop()
{
  int value = analogRead(sensePin);

  if (value > 400 || value < 300)
      Serial.println(value); 
  delay(500);   //wait half a second, then check again.
}

That is great thank you.

Taking the idea further is it possible to change the figure to calculate either side and set it such as -

If the base figure is 300, only return to the serial com if 20% higher or lower output than 300?

I have added the following code to try and smooth out rouge returns. I am now trying to only return the exceptional values but seemed to have caused the COM to send incorrect outputs. Any ideas appreciated. Thanks

/ Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average

int inputPin = A0;

void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}

void loop() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;

// if we’re at the end of the array…
if (index >= numReadings)
// …wrap around to the beginning:
index = 0;

// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
int value = analogRead(inputPin);

if (value > 400 || value < 300)
Serial.println(value);
}

  // calculate the average:
  average = total / numReadings;         
  // send it to the computer as ASCII digits
  int value = analogRead(inputPin);

  if (value > 400 || value < 300)
  Serial.println(value);

Take a bunch of readings to get an average, then ignore that, take another reading, and use that.

What was the purpose of computing an average?

I am now trying to only return the exceptional values but seemed to have caused the COM to send incorrect outputs. Any ideas appreciated.

I have an idea. You might try showing us the output that you are seeing, and explain what you think is incorrect about it.

At the moment the serial com is churning out a number between 400 - 500. Earlier it was about 300.

What I want to achieve is for the serial com only to return a value when something enters a box (the sensor is fixed inside a box)

Any help appreciated