arduino attachInterrupt and parallax xband sensor

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jedihonor
 
Posts: 8
Joined: Tue Oct 11, 2011 12:23 am

arduino attachInterrupt and parallax xband sensor

Post by jedihonor »

HI,
Can someone please help me....

I have an Arduino uno r3, and just bought an xband parallax sensor (http://www.parallax.com/Portals/0/Downl ... r-v1.1.pdf)

I would like to learn how to use the attachInterrupt to read the pulses and wrote this code which seems to work but a little slow.

Code: Select all

int xbandTrigger = 0; // interrupt pin #2
volatile int pulse = 0; // count the # of times the incoming xband
                       // pulse changes from low to high
int oldpulse = 0;                       

void setup()
{
  Serial.begin(9600);
  attachInterrupt(xbandTrigger, count, RISING);
  pulse = 0;
}

void loop()
{
  Serial.println(pulse);
 
  delay(100);
   
}

void count()
{
  pulse++;
}
My questions. 1. Does the interrupt work in the background and counts the pulse on each rise, even while processing the print statement in the loop section? or on each rising pulse the program goes to the count() function, and when there is no pulse the program is allowed to print?

2. When there is no movement, how would I reset the pulse to 0?

Thank you
Scott

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: arduino attachInterrupt and parallax xband sensor

Post by adafruit_support_bill »

We don't carry this sensor and are not familiar with its operation. Parallax should be able to answer your questions.

The interrupt should invoke your count() function on the rising edge of each pulse - as long as interrupts are not disabled. Serial.println is also interrupt driven, so it will process an interrupt per character. Given the low serial output volume, you shouldn't experience much interference between interrupts and pulse counts.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”