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);
}
}
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.
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.
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.
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.