I try to use my arduino read the ir code from the ir deivce without irremote library.
my idea is create a array store the count of every status persist time and then use show_data function print it. But when I try to read data from my telecontroller. The array seems less then the 32bit, only six bit. It must have some problem in it.
#define STATUS_HIGH 0
#define STATUS_LOW 1
#define STATUS_RESET 2
#define STATUS_IDLE 3
#define START_MARK_COUNT 100
#define END_MARK_COUNT 100
#define SIG_HIGH 0
#define SIG_LOW 1
#define MAX_ARRAY_LEN 256
typedef struct _ir_data {
int buffer[MAX_ARRAY_LEN];
int len;
bool finish = false;
} ir_data;
ir_data mydata;
ir_data *dp = &mydata;
int count = 0;
int status = STATUS_IDLE;
int RECV_PIN = 11;
// temp recive data
int data;
// set interval to 50us
void init_timer() {
cli();
TCCR1A = 0;
TCCR1B = 0;
// set timer for 50us
OCR1A = 99;
// turn CTC mode;
TCCR1B |= (1 << WGM12);
// set CS11 for 8 prescaler;
TCCR1B |= (1<<CS11);
// enable timer compare interrupt
TIMSK1 = (1 << OCIE1A);
sei();
}
void init_system() {
Serial.begin(9600);
init_timer();
pinMode(RECV_PIN, INPUT);
}
ISR(TIMER1_COMPA_vect) {
data = digitalRead(RECV_PIN);
switch(status) {
case STATUS_IDLE: {
if(data == SIG_HIGH) {
Serial.println("to high");
status = STATUS_HIGH;
count = 0;
dp->len = 0;
Serial.println("set index = 0");
}
break;
}
case STATUS_LOW: {
// signal jump from low to high
if(data == SIG_HIGH) {
Serial.println("low to high");
Serial.println(count);
dp->buffer[dp->len] = count;
dp->len++;
status = STATUS_HIGH;
count = 0;
}else {
// end
if(count > END_MARK_COUNT) {
//Serial.println(count);
status = STATUS_IDLE;
dp->finish = true;
count = 0;
}
count++;
}
break;
}
case STATUS_HIGH: {
// signal jump from high to low
if(data == SIG_LOW) {
//Serial.println(count);
count = 0;
status = STATUS_LOW;
} else {
count++;
}
break;
}
}
}
void show_data() {
cli();
Serial.println("get new data:");
//Serial.println(dp->len);
for(int i = 0; i < MAX_ARRAY_LEN&&i <dp->len; i++) {
Serial.println(dp->buffer);
}
sei();
}
void setup() {
// put your setup code here, to run once:
init_system();
}
void loop() {
// put your main code here, to run repeatedly:
while(dp->finish) {
show_data();
dp->finish = false;
}
}
the show_data output is
76
52
17
45
9
57
only six bits, the telecontroller's protocol is NEC protocol. It have at least 32bit data actually. But I don't know what problem occur. Anybody can help me? Thanks.