Pages: [1] 2   Go Down
Author Topic: Serial Monitor to read IR Sensor and control LEDs  (Read 1472 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:

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
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (3000 < distance < 6000)
It compiles but doesn't do what you think
Code:
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Anyone?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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: 
Code:
  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 Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so like: 
Code:

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
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

okay, could you show me how to do that.  It needs to happen over and over as well
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so would something like this work?
Code:
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
Online Online
Brattain Member
*****
Karma: 553
Posts: 46282
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so would something like this work?
Well, did it? You did test it, right?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just replaced it with: 
Code:
  int distancesum;
  int distanceavg; 
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 553
Posts: 46282
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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.

Code:
  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

Pages: [1] 2   Go Up
Jump to: