Help with arduino Interrupts (for IR receiver)
hello everybody,
going straight point, trying make project , gets interrupted when ir sensors detect infrared commands.
the problem having when press button on remote control (this triggers interruption, arduino stops completely, set serial.println("something") first line in interruption function , doesn't print, makes me think problem not loops inside interrupt function.
in code not keeping values of ir sensor, time takes between each change (low high or high low) in code, time information should enough simple things have in mind.
other thing consider later change "while not change" loops (the whiles inside "while not ended") conditions , remove timeout constant , set timer interruption should make code more efficient since spend less processing time in while. timer interruption change boolean variable checked "while not change" loops condition. problem of setup have remove nointerrupts(), since interrupts in controlled environment don't think issue. guys think?
here code:
when ir receives counter stops , never comes back.
thanks in advance , support.
going straight point, trying make project , gets interrupted when ir sensors detect infrared commands.
the problem having when press button on remote control (this triggers interruption, arduino stops completely, set serial.println("something") first line in interruption function , doesn't print, makes me think problem not loops inside interrupt function.
in code not keeping values of ir sensor, time takes between each change (low high or high low) in code, time information should enough simple things have in mind.
other thing consider later change "while not change" loops (the whiles inside "while not ended") conditions , remove timeout constant , set timer interruption should make code more efficient since spend less processing time in while. timer interruption change boolean variable checked "while not change" loops condition. problem of setup have remove nointerrupts(), since interrupts in controlled environment don't think issue. guys think?
here code:
code: [select]
#include <liquidcrystal.h>
#define sample_size 64
#define ir_timeout 500 //milisseconds
const int ledpin = 13;
const int irsensorpin = 2;
liquidcrystal lcd(12, 11, 6, 5, 4, 3);
unsigned int changetimes[sample_size];
unsigned int changecounter;
unsigned long inittime;
unsigned long currenttime;
boolean ended;
unsigned int i;
void setup() {
serial.begin(115200);
pinmode(ledpin, output);
pinmode(irsensorpin, input);
digitalwrite(ledpin, high);
attachinterrupt(0, receiveir, low);
lcd.begin(16, 2);
}
void receiveir()
{
serial.println("receiving ir transmission: ");
nointerrupts();
currenttime = inittime = millis();
changecounter = 0;
ended = false;
while(!ended)
{
if (digitalread(irsensorpin) == high)
while ((digitalread(irsensorpin) == high) && (currenttime - inittime < ir_timeout))
currenttime = millis();
else
while ((digitalread(irsensorpin) == low) && (currenttime - inittime < ir_timeout))
currenttime = millis();
changetimes[changecounter] = currenttime-inittime;
if ((changetimes[changecounter] > ir_timeout) || (changecounter == sample_size-1))
ended = true;
changecounter++;
}
interrupts();
for (i = 0; < changecounter; i++)
serial.println(changetimes[i]);
}
void loop() {
lcd.clear();
lcd.setcursor(0, 1);
lcd.print(millis()/1000);
delay(100);
}
when ir receives counter stops , never comes back.
thanks in advance , support.
there quite few things wrong here:
basically rework everything. interrupt routine should set flag. rest elsewhere, particularly serial prints.
maybe can measure pulse, forget debugging stuff. if must measure time use micros() queries clock hardware, whereas millis() won't change inside routine.
btw there ir library, might show nice techniques.
code: [select]
attachinterrupt(0, receiveir, low);
lcd.begin(16, 2);
}
void receiveir()
{
serial.println("receiving ir transmission: ");
nointerrupts();
currenttime = inittime = millis();- inside interrupt routine interrupts off. don't need nointerrupts().
- you shouldn't turn interrupts on - routine might interrupted itself, rid of interrupts().
- serial.print won't work inside it.
- millis() clock stops ticking, don't keep calling that,
- the interrupt routine should short - yours incredibly long
basically rework everything. interrupt routine should set flag. rest elsewhere, particularly serial prints.
maybe can measure pulse, forget debugging stuff. if must measure time use micros() queries clock hardware, whereas millis() won't change inside routine.
btw there ir library, might show nice techniques.
Arduino Forum > Using Arduino > Project Guidance > Help with arduino Interrupts (for IR receiver)
arduino
Comments
Post a Comment