Serial Monitor to read IR Sensor and control LEDs

Hello, I am trying to figure this out to add to a bigger project and then eventually a robot, I need to get a value from the IR Sensor (just an IR light next to an IR resistor) to measure two different values, one when it is close to a hard surface and one when it is far away (on top of table, off of table) I am getting about 3000-6000 on the serial monitor for on the table and about 10,000+ off the table. How to I make this control an LED. Have it turn on when the sensor is facing hard surface, turn off when facing open air.
Thanks!

The code i have so far:

int IRpin = 1;
int led = 13;

void setup() {
  Serial.begin(9600);                             // start the serial port
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

void loop() {
  float volts = analogRead(IRpin)*0.0048828125; 
  float distance = 65*pow(volts, -1.10);          
  Serial.println(distance);                     
  delay(1000);                                     
  if (3000 < distance < 6000){                   
  digitalWrite(led, HIGH);
  }
  else {
  digitalWrite(led, LOW);  
}
}
if (3000 < distance < 6000)

It compiles but doesn’t do what you thinkif (distance > 3000  && distance < 6000) or whatever your conditions happen to be.

That works great! thank you. Say, what if I wanted to change the delay in between the time it checks to be 100 or even 10 and then just average it over a set period of time, say if its 10ms and then average those over 100ms so it is more accurate because i sometimes get random readings and I want to eliminate those because eventually it will be operating a motor in a more complicated sequence. thanks.

Anyone?

Anyone what? Your description doesn't make much sense.

.

.

I just need a way to average 10 numbers from the serial monitor

I don't understand hat the serial monitor has to do with the problem; it has no arithmetic capability. If you want to average 10 numbers on the Arduino, just do it the normal way; sum ten readings and divide by ten.

.

.

so would something like this work?

Well, did it? You did test it, right?

wow, it actually did work. I had so little faith that it would that I didnt even test it because i knew i got something wrong. guess i should have a little more faith. :)

float distancesum = 65*pow(volts, -1.10);
  float distanceavg = 65*pow(volts, -1.10);

Completely unnecessary. Let's hope the compiler optImised those calculations away.

Just replaced it with:

  int distancesum;
  int distanceavg;
  float distance1 = 65*pow(volts, -1.10);
  float distance2 = 65*pow(volts, -1.10);
  float distance3 = 65*pow(volts, -1.10);
  float distance4 = 65*pow(volts, -1.10);
  float distance5 = 65*pow(volts, -1.10);

Why? 5 calls to pow() with the same input are going to produce the same output, most days.

  float distancesum = 65*pow(volts, -1.10);
  float distanceavg = 65*pow(volts, -1.10);  
  distancesum = distance1 + distance2 + distance3 + distance4 + distance5;
  distanceavg = distancesum / 5;

The first waste of time was what AWOL was pointing out. Though the second part doesn't make any sense, either.

Changing the type to int did nothing to stop wasting the time.

that doesn’t work, i need something to store a number that is like 400,000. I dont think unsigned int will work. any thoughts?

that doesn't work, i need something to store a number that is like 400,000. I dont think unsigned int will work. any thoughts?

What doesn't work? The problem wasn't the data type. The problem was the stupid code.

so what are you recommending i change it too? How long does it take to make those calculations?, is it causing a measurable delay?

How long does it take to make those calculations?, is it causing a measurable delay?

No, the onus is on you to explain WHY you are doing what you are doing. It makes no sense. You are not averaging anything. Pretending that you are doesn't make it so.