Pages: [1]   Go Down
Author Topic: Connecting 2 IR sensors to arduino  (Read 3146 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey, my study group and I are working on a project to make a Sharp IR 2Y0A02 sensor work as a keyboard button in a game.

So far we succesfully connected 1 IR sensor and made it turn a diode on and off.

Now we want to connect an additional IR sensor and try and connect them to a chip we got from a keyboard.

My question is, how do we program our arduino to register 2 IR sensors and assign them a button each?

So far we've got this

Code:
int RightIR = 1;
int LeftIR = 2;                                      // analog pin for reading the IR sensor
int keyZ = 10;                                      // Setup keyboard button on Digital Pin 10
int keyX = 11;

void setup() {
  pinMode(keyZ, OUTPUT);
  pinMode(keyX, OUTPUT);
  Serial.begin(9600);                              // start the serial port
}
void loop() {
  float volts = analogRead(RightIR && LeftIR)*0.0048828125;    // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
  float distance1 = 65*pow(volts, -1.10);          // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
  Serial.println(distance1);                       // print the distance
  delay(100);                                      // arbitary wait time. 

//  float volts = analogRead(Left)*0.0048828125;     
//  float distance2 = 65*pow(volts, -1.10);         
//  Serial.println(distance2);                       
//  delay(100);
 
  if (distance1 < 180 )
  {
    digitalWrite(keyX, HIGH); //High Reflectance. 
  }
  else
  {
    digitalWrite(keyX, LOW); // Low Reflectance.
  }

  else if (distance2 < 180 )
  {
    digitalWrite(keyZ, HIGH); //High Reflectance. 
  }
  else
  {
    digitalWrite(keyZ, LOW); // Low Reflectance.
  }

The point is that we want to assign the X button of our keyboard to the first IR sensor and the Z button to the other IR sensor.
We thought about making a distance1 and a distance2 so only the Left IR sensor registers as a Z-press.

We don't know exactly how to connect 2 IR sensors and then assign a different button to each IR sensor


Hope you can help us smiley
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 76
Posts: 7305
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What do you intend to do with this line of code? I can't guess it.

float volts = analogRead(RightIR && LeftIR)*0.0048828125;
Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 110
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are pins 1 and 2 analog pins on your Arduino?

Apparently you already tried something which you then commented out, but that doesn't look too bad.

Reading 2 sensors on different analog inputs should be easy:
Code:
void loop()
{
    int valueFromRightSensor = analogRead(A0);
    int valueFromLeftSensor = analogRead(A1);

    // do something
}
Now you can process the input data to calculate your distances etc.

If it doesn't work give us some more details on what your setup is, what you tried and what the results were.

Good luck!
Tom
Logged

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

liudr --> I think it is to make the readings more stable. I don't know if it is the best way to do it but, we got it from this tutorial:
http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/comment-page-1/


TomS --> Yes the pins 1 and 2 are analog pins on our Arduino smiley

Alright so after int ValueFromLeftSensor, we can put in the loop with float volts? and should we have a float volts and distance for each of our IR sensors?
Logged

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

Quote
we got it from this tutorial:

You didn't get
Code:
float volts = analogRead(RightIR && LeftIR)*0.0048828125;
from the page you linked.

This version of C doesn't have any parallel constructs, so if you want to read two analogue pins, you have to do them one at a time.
The C compiler has a nasty habit of allowing you to compile all sorts of crazy things that will actually run, but may not do what you think.
Code:
const float voltsFromAnalog = 0.0048828125;
float rightVolts = analogRead(RightIR) * voltsFromAnalog;
[code]float leftVolts = analogRead(LeftIR) * voltsFromAnalog;
[/code]

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: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

We got inspired from it and tried to make it work with the right and the left sensor.



Alright so that might work?
Code:
const float voltsFromAnalog = 0.0048828125;
float rightVolts = analogRead(RightIR) * voltsFromAnalog;
float leftVolts = analogRead(LeftIR) * voltsFromAnalog;

So after this code, should we make a float distanceRight and a float distanceLeft? to seperate the output of our IR sensors.
Code:
const float voltsFromAnalog = 0.0048828125;
float rightVolts = analogRead(RightIR) * voltsFromAnalog;
float leftVolts = analogRead(LeftIR) * voltsFromAnalog;
float distanceRight = 65*pow(volts, -1.10);
Serial.printIn(distanceRight)
float distanceLeft = 65*pow(volts, -1.10);
Serial.printIn(distanceLeft)

if (distanceRight < 180) {
//do something
}
else {
//do something else
}

if (distanceLeft < 180) {
do something
}
else {
//do something else
}
« Last Edit: May 03, 2011, 02:22:00 am by dingenot » Logged

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

Code:
float distanceRight = 65*pow(volts, -1.10);
Serial.printIn(distanceRight)
float distanceLeft = 65*pow(volts, -1.10);

Where is the variable "volts" declared and initialised?
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: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do it have to be declared and initialised? We got inspired from this code:

Code:
int IRpin = 1;                                    // analog pin for reading the IR sensor
void setup() {
  Serial.begin(9600);                             // start the serial port
}
void loop() {
  float volts = analogRead(IRpin)*0.0048828125;   // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
  float distance = 65*pow(volts, -1.10);          // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
  Serial.println(distance);                       // print the distance
  delay(100);                                     // arbitary wait time.
}


Would it be better to code it in another way?
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 76
Posts: 7305
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just go through some basic tutorials on cplusplus.com

Inspiration doesn't seem to do you any good in programming, which you know nothing about.

You should think about programming as learning a language, except your language partner, the arduino compiler doesn't know your human language and only knows C. So you need to do all the work to make every sentence correct when you speak to it. DO NOT BE CREATIVE. The machines can't understand you or will misunderstand you. You have to know exactly what each line of code does.
Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 110
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I assume what AWOL was pointing out to you is that you replaced the variable "volts" that you used previously for one sensor with "rightVolts" and "leftVolts".
But you didn't replace it when calculating the distance:
Code:
float rightVolts = analogRead(RightIR) * voltsFromAnalog;
float leftVolts = analogRead(LeftIR) * voltsFromAnalog;
float distanceRight = 65*pow(volts, -1.10);
Serial.printIn(distanceRight)
float distanceLeft = 65*pow(volts, -1.10);
Serial.printIn(distanceLeft);

This should now be
Code:
float distanceRight = 65*pow(rightVolts, -1.10);
Serial.println(distanceRight);

float distanceLeft = 65*pow(leftVolts, -1.10);
Serial.println(distanceLeft);

Hope that helps...
« Last Edit: May 05, 2011, 03:54:31 am by TomS » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 110
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One more thing:

It is
Serial.printLN (short for print LiNe) not Serial.printIN.
Logged

Pages: [1]   Go Up
Jump to: