Go Down

Topic: Connecting 2 IR sensors to arduino (Read 3 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.

Go Up