Offline
Newbie
Karma: 0
Posts: 47
|
 |
« on: October 11, 2012, 08:32:25 pm » |
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); } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19003
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: October 11, 2012, 08:36:30 pm » |
if (3000 < distance < 6000) It compiles but doesn't do what you think if (distance > 3000 && distance < 6000) or whatever your conditions happen to be.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« Reply #2 on: October 11, 2012, 09:33:36 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« Reply #3 on: October 12, 2012, 04:24:51 pm » |
Anyone?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19003
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: October 12, 2012, 04:27:39 pm » |
Anyone what? Your description doesn't make much sense.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« Reply #5 on: October 12, 2012, 04:41:29 pm » |
Well I am using the serial readout to initialize another function (right now it is to turn on a light but eventually will be more complicated with servos) and my problem is that i occcasionally get random numbers and i need to find a way to eliminate these random numbers so that the function only takes place when it really needs to. I just need a way to average 10 numbers from the serial monitor and then use them in/ to control this: if (distance > 3000 && distance < 6000) so instead of this function being tested for every time the serial monitor produces a number, it is only tested every ten times with the average of the ten
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« Reply #6 on: October 12, 2012, 04:44:55 pm » |
so like: Serial.println(distance1); Serial.println(distance2); Serial.println(distance3); Serial.println(distance4); Serial.println(distance5); Serial.println(distance6); Serial.println(distance7); Serial.println(distance8); Serial.println(distance9); Serial.println(distance10); delay(100); distance1 + distance2 + distance3 + distance4...... / 10 = distanceavg if (distanceavg > 3000 && distanceavg < 6000){ digitalWrite(led, HIGH); }
i know this wont work but something like this
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19003
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: October 12, 2012, 04:45:44 pm » |
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.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« Reply #8 on: October 12, 2012, 04:56:32 pm » |
okay, could you show me how to do that. It needs to happen over and over as well
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« Reply #9 on: October 12, 2012, 07:00:51 pm » |
so would something like this work? int IRpin = 1; int led = 13;
void setup() { Serial.begin(9600); pinMode(led, OUTPUT); digitalWrite(led, LOW); }
void loop() { float volts = analogRead(IRpin)*0.0048828125; 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); float distancesum = 65*pow(volts, -1.10); float distanceavg = 65*pow(volts, -1.10); Serial.println(distance1); delay(20); Serial.println(distance2); delay(20); Serial.println(distance3); delay(20); Serial.println(distance4); delay(20); Serial.println(distance5); delay(20); distancesum = distance1 + distance2 + distance3 + distance4 + distance5; distanceavg = distancesum / 5; if (distanceavg > 3000 && distanceavg < 6000){ digitalWrite(led, HIGH); } else { digitalWrite(led, LOW); } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #10 on: October 12, 2012, 07:12:40 pm » |
so would something like this work? Well, did it? You did test it, right?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« Reply #11 on: October 12, 2012, 07:43:10 pm » |
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. 
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19003
I don't think you connected the grounds, Dave.
|
 |
« Reply #12 on: October 13, 2012, 03:17:21 am » |
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.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« Reply #13 on: October 13, 2012, 06:06:25 pm » |
Just replaced it with: int distancesum; int distanceavg;
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #14 on: October 13, 2012, 06:27:36 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|