Pages: [1]   Go Down
Author Topic: Problem when using LANC Protocol and IR Library in the same sketch  (Read 436 times)
0 Members and 1 Guest are viewing this topic.
Kalamazoo Michigan
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the project I am currently working on:
The idea is to control a pan tilt head and a SONY mini DV camcorder with an Arduino, L293D chip for the two stepper motors in the head,
and an old IR TV remote control, using the IR library from Ken Shirriff and the LANC tutorial/code from Martin Koch.
I have designed a PCB with Arduino, an IR receiver and L293D as well as the circuit needed for the LANC commands.

Controlling the camcorder via serial port and LANC works fine, you can see the code below:
Code:
/*
Comprehensive LANC info: www.boehmel.de/lanc.htm

"LANC" is a registered trademark of SONY.
CANON calls their LANC compatible port "REMOTE".

2011, Martin Koch
http://controlyourcamera.blogspot.com/2011/02/arduino-controlled-video-recording-over.html
*/
#include <SoftwareSerial.h>
#include <IRremote.h>
#define IRreceive_pin 8
IRrecv irrecv(IRreceive_pin);
#define cmdPin 11
#define lancPin 12

//Start-stop video recording
boolean REC[] = {LOW,LOW,LOW,HIGH,HIGH,LOW,LOW,LOW,   LOW,LOW,HIGH,HIGH,LOW,LOW,HIGH,HIGH}; //18 33
boolean STOP[] = {LOW,LOW,LOW,HIGH,HIGH,LOW,LOW,LOW,   LOW,LOW,HIGH,HIGH,LOW,LOW,LOW,LOW}; //18 30
boolean ZOOM_IN[] = {LOW,LOW,HIGH,LOW,HIGH,LOW,LOW,LOW,   LOW,LOW,LOW,LOW,LOW,HIGH,LOW,LOW}; //28 04     
boolean ZOOM_OUT[] = {LOW,LOW,HIGH,LOW,HIGH,LOW,LOW,LOW,   LOW,LOW,LOW,HIGH,LOW,HIGH,LOW,LOW}; //28 14
int cmdRepeatCount;
int bitDuration = 96; //Duration of one LANC bit in microseconds.
char bytein = 'q';   
char char_old = 'q';
void setup() {
Serial.begin(19200);
//irrecv.enableIRIn();           // Start the receiver  The LANC part of this sketch works as long as this line of code for the IR receiver is not activated
pinMode(lancPin, INPUT); //listens to the LANC line
pinMode(cmdPin, OUTPUT); //writes to the LANC line

digitalWrite(cmdPin, LOW); //set LANC line to +5V
delay(5000); //Wait for camera to power up completly
}

void loop() {
if (Serial.available() > 0)
   { bytein = Serial.read();
   }
switch (bytein)           
  {
  case 'r':
    lancCommand(REC);
  break; 
  case 'z':
   lancCommand(ZOOM_IN);
   break;
 case 'x':
    lancCommand(ZOOM_OUT);
  break;
   case 's':
    lancCommand(STOP);
  break;
 delay(1000);
}
}

void lancCommand(boolean lancBit[]) {
  cmdRepeatCount = 0;
  while (cmdRepeatCount < 5) {  //repeat 5 times to make sure the camera accepts the command
  while (pulseIn(lancPin, HIGH) < 5000)
  {   //"pulseIn, HIGH" catches any 0V TO +5V TRANSITION and waits until the LANC line goes back to 0V
      //"pulseIn" also returns the pulse duration so we can check if the previous +5V duration was long enough (>5ms) to be the pause before a new 8 byte data packet
      //Loop till pulse duration is >5ms
 }
//LOW after long pause means the START bit of Byte 0 is here
  delayMicroseconds(bitDuration);  //wait START bit duration
//Write the 8 bits of byte 0
//Note that the command bits have to be put out in reverse order with the least significant, right-most bit (bit 0) first
  for (int i=7; i>-1; i--)
  { digitalWrite(cmdPin, lancBit[i]);  //Write bits.
    delayMicroseconds(bitDuration);
  }
//Byte 0 is written now put LANC line back to +5V
  digitalWrite(cmdPin, LOW);
  delayMicroseconds(10); //make sure to be in the stop bit before byte 1
  while (digitalRead(lancPin))
  { //Loop as long as the LANC line is +5V during the stop bit
  }
//0V after the previous stop bit means the START bit of Byte 1 is here
  delayMicroseconds(bitDuration);  //wait START bit duration
//Write the 8 bits of Byte 1
//Note that the command bits have to be put out in reverse order with the least significant, right-most bit (bit 0) first
   for (int i=15; i>7; i--)
   { digitalWrite(cmdPin,lancBit[i]);  //Write bits
     delayMicroseconds(bitDuration);
    }
//Byte 1 is written now put LANC line back to +5V
  digitalWrite(cmdPin, LOW);
cmdRepeatCount++;  //increase repeat count by 1
/*Control bytes 0 and 1 are written, now don’t care what happens in Bytes 2 to 7
and just wait for the next start bit after a long pause to send the first two command bytes again.*/
}//While cmdRepeatCount < 5
}

This program works as long as this line of code is not activated:
irrecv.enableIRIn();           // Start the receiver 

On the other hand the IR control of the pan tilt head is also no problem.
The code is too long to be inserted here but you can find it at this post:
http://christophhartel.com/wireless-control-of-a-pan-tilt-head-with-arduino-l293d-motor-controller-and-tv-ir-remote-control/

As mentioned above the problem occurs when I try to combine these two sketches.
Neither the pan tilt head nor the camcorder can be controlled once these functions are integrated into one sketch.
Any ideas how to resolve this problem are welcome.

Thanks a lot.

Christoph





Logged

Pages: [1]   Go Up
Jump to: