Author Topic: IR Emitter/Detector arduino code question  (Read 8751 times)

0 Members and 1 Guest are viewing this topic.

Offline ang5tTopic starter

  • Jr. Member
  • **
  • Posts: 34
  • Helpful? 0
IR Emitter/Detector arduino code question
« on: April 12, 2009, 03:47:19 AM »
Hey,
   First off, I'm pretty new with arduino (don't know any C variants either) and I'm trying to use an IR Emitter/Detector pair (Radioshack 276-142) to make an LED turn on/off. I've searched the arduino boards, but just found alot of sharp IR and remote control stuff. Is there some kind of "pulseout" command or something like with Pbasic? Or if someone could point me to how to write out a specific frequency (38.4 MHz). I'm using the $50 board with arduino software with an ATmega8 running on an internal CLK @ 8MHz. Not the standard Arduino 16MHz( I think). Any help would be appreciated.

*edit* more appropriate title
« Last Edit: April 12, 2009, 04:12:25 AM by ang5t »
So if the blue wire goes here, then that means...(bzzzt) oops....

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
    • Ro-Bot-X Designs
Re: IR Emitter/Detector arduino code question
« Reply #1 on: April 12, 2009, 05:30:19 AM »
Here are the funtions for doing proximity with 2 IR (Panasonic) sensors and one LED mounted between them:

Code: [Select]
#define IRpin 11
#define LeftProxyPin 16   //analog pin 2
#define RightProxyPin 17  //analog pin 3

int LeftVal = 0;
int RightVal = 0;
int Obstacle = 0;

void setup(){
  pinMode(IRpin, OUTPUT);
  digitalWrite(IRpin, LOW);

  Serial.begin (9600);
  Serial.println("start");               
}

void loop(){
  DoProxy();
  //do something with the value returned...
}

int DoProxy(){
  Obstacle = 0;
  IR38Write();
  LeftVal = digitalRead(LeftProxyPin);
  //delay(10);
  IR38Write();
  RightVal = digitalRead(RightProxyPin);
  if (LeftVal == LOW){
    Obstacle = Obstacle + 1;
  }
  if (RightVal == LOW){
    Obstacle = Obstacle + 2;
  }
  Serial.println ("LeftVal   RightVal");  // debug - remember to comment out
  Serial.print (LeftVal, DEC);            // debug - remember to comment out
  Serial.print ("             ");         // debug - remember to comment out
  Serial.println (RightVal, DEC);           // debug - remember to comment out
  Serial.println (Obstacle, DEC);         // debug - remember to comment out
  return Obstacle;
}

void IR38Write() {
  for(int i = 0; i <= 380; i++) {
    digitalWrite(IRpin, HIGH);
    delayMicroseconds(12);
    digitalWrite(IRpin, LOW);
    delayMicroseconds(12);
  }
}
« Last Edit: April 12, 2009, 05:31:30 AM by Ro-Bot-X »
Check out the uBotino robot controller!

Offline ang5tTopic starter

  • Jr. Member
  • **
  • Posts: 34
  • Helpful? 0
Re: IR Emitter/Detector arduino code question
« Reply #2 on: April 13, 2009, 01:56:24 AM »
Hey, thanks for the reply Ro-Bot-X. I will try this out as soon as I can and report my findings. I just wanna ask something about this program though. Say I need to turn on/off an LED, would it go something like this:

#define LedA 1 //just adding these 2 as an example
#define LedB 2

void loop(){
  DoProxy();
  //do something with the value returned...
  if (Obstacle == 1)    // this would be what is done with said DoProxy value.
 {
    digitalWrite (LedA, HIGH);
  }
  if (Obstacle == 2)
  {
    digitalWrite (LedB, HIGH);
  }
  if (Obstacle ==3)
  {
    digitalWrite (LedA,HIGH);
    digitalWrite (LedB, HIGH);
  }
  else
  {
    digitalWrite (LedA, LOW);
    digitalWrite (LedB, LOW);
  }
}



It will be a few days before I get a chance to check this out so just seeing if I understand this correctly.
So if the blue wire goes here, then that means...(bzzzt) oops....

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
    • Ro-Bot-X Designs
Re: IR Emitter/Detector arduino code question
« Reply #3 on: April 13, 2009, 12:33:53 PM »
I think it will work!
Check out the uBotino robot controller!

 


Get Your Ad Here

data_list