Go Down

Topic: Connecting 2 IR sensors to arduino (Read 3406 times) previous topic - next topic

dingenot

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: [Select]

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

liudr

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

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

TomS

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: [Select]
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

dingenot

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

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?

AWOL

Quote
we got it from this tutorial:


You didn't get
Code: [Select]
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: [Select]
const float voltsFromAnalog = 0.0048828125;
float rightVolts = analogRead(RightIR) * voltsFromAnalog;
[code]float leftVolts = analogRead(LeftIR) * voltsFromAnalog;

[/code]

"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.

dingenot

#5
May 03, 2011, 09:19 am Last Edit: May 03, 2011, 09:22 am by dingenot Reason: 1
We got inspired from it and tried to make it work with the right and the left sensor.



Alright so that might work?
Code: [Select]
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: [Select]
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
}


AWOL

Code: [Select]
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?
"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.

dingenot

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

Code: [Select]
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?

liudr

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.

TomS

#9
May 05, 2011, 10:50 am Last Edit: May 05, 2011, 10:54 am by TomS Reason: 1
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: [Select]
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: [Select]

float distanceRight = 65*pow(rightVolts, -1.10);
Serial.println(distanceRight);

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


Hope that helps...

TomS

One more thing:

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

Go Up