I put together a 'special' box to grab some IR laser coded transmissions (eg like TV remotes)
I needed something easily fieldable, so grabbing the defunct camera trigger box (with built in power switch, status LED and signal LED :) ) I velcro'ed a mini breadboard and ATMega328 board in.
The breadboard consists of a regulated 5V supply, a regulated 3V3 supply (for the SD card), an IR phototransistor/amplifier (nabbed from an old TV tuner box) and some voltage level shifters to allow the 5V MCU to talk with the SD card
Code after the break...
/*
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 10
*/
/***************************************************************/
// Includes
#include <SD.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#define TIMER_RESET TCNT1 = 0
#define SAMPLE_SIZE 64
/***************************************************************/
// Variable declaration
int IRpin = 2;
unsigned int TimerValue[SAMPLE_SIZE];
char direction[SAMPLE_SIZE];
byte change_count;
long time;
File myFile;
void setup() {
Serial.begin(115200);
Serial.println("Analyze IR Remote");
TCCR1A = 0x00; // COM1A1=0, COM1A0=0 => Disconnect Pin OC1 from Timer/Counter 1 -- PWM11=0,PWM10=0 => PWM Operation disabled
// ICNC1=0 => Capture Noise Canceler disabled -- ICES1=0 => Input Capture Edge Select (not used) -- CTC1=0 => Clear Timer/Counter 1 on Compare/Match
// CS12=0 CS11=1 CS10=1 => Set prescaler to clock/64
TCCR1B = 0x03; // 16MHz clock with prescaler means TCNT1 increments every 4uS
// ICIE1=0 => Timer/Counter 1, Input Capture Interrupt Enable -- OCIE1A=0 => Output Compare A Match Interrupt Enable -- OCIE1B=0 => Output Compare B Match Interrupt Enable
// TOIE1=0 => Timer 1 Overflow Interrupt Enable
TIMSK1 = 0x00;
pinMode(IRpin, INPUT);
pinMode(3, OUTPUT);
//SD card initialisation
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("SD card initialization failed!");
return;
}
Serial.println("SD card initialization done.");
myFile = SD.open("IR_cap.txt", FILE_WRITE);
if (myFile)
{
myFile.println("IR_cap recording");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
//safely close the file
myFile.close();
}
void loop()
{
Serial.println("Waiting...");
change_count = 0;
while(digitalRead(IRpin) == HIGH) {}
digitalWrite(3, HIGH);
TIMER_RESET;
TimerValue[change_count] = TCNT1;
direction[change_count++] = '1';
while (change_count < SAMPLE_SIZE)
{
if (direction[change_count-1] == '1') {
while(digitalRead(IRpin) == LOW) {}
TimerValue[change_count] = TCNT1;
direction[change_count++] = '0';
} else {
while(digitalRead(IRpin) == HIGH) {}
TimerValue[change_count] = TCNT1;
direction[change_count++] = '1';
}
}
change_count = 0;
time = (long) TimerValue[change_count] * 4;
print_to_SD();
//print_to_serial();
digitalWrite(3, LOW);
}
void print_to_serial()
{
Serial.println("Bit stream detected!");
Serial.print(time);
Serial.print("\t");
Serial.println(direction[change_count++]);
while (change_count < SAMPLE_SIZE) {
time = (long) TimerValue[change_count] * 4;
Serial.print(time);
Serial.print("\t");
Serial.println(direction[change_count-1]);
Serial.print(time);
Serial.print("\t");
Serial.println(direction[change_count++]);
}
Serial.println("Bit stream end!");
}
void print_to_SD()
{
myFile = SD.open("IR_cap.txt", FILE_WRITE);
if (myFile)
{
myFile.println("IR Bit stream");
myFile.print(time);
myFile.print(" , ");
myFile.println(direction[change_count++]);
while (change_count < SAMPLE_SIZE) {
time = (long) TimerValue[change_count] * 4;
myFile.print(time);
myFile.print(" , ");
myFile.println(direction[change_count-1]);
myFile.print(time);
myFile.print(" , ");
myFile.println(direction[change_count++]);
}
myFile.println("IR Bit stream end");
} else {
// if the file didn't open, print an error:
//Serial.println("error opening test.txt");
}
//safe close the SD connection
myFile.close();
delay(2000);
}
No comments:
Post a Comment