Shop OBEX P1 Docs P2 Docs Learn Events
x band sensor counting pulses with arduino uno — Parallax Forums

x band sensor counting pulses with arduino uno

jedihonorjedihonor Posts: 1
edited 2013-04-01 09:21 in Accessories
HI,
Can someone please help me....


I don't have a Basic Stamp or Propeller but do 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 x bannd pulses and wrote this code which seems to work but a little slow.
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

Comments

  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2013-04-01 09:21
    Not having tried it, but the code looks okay. What may appear slow is that you have a relatively slow baud rate for the serial monitor. You can go (much) higher.

    The Arduino's external interrupts do work in the background, though remember this chip is single core. So whenever there is a pulse on that pin, your main code is temporarily suspended. The interrupt is handled, then the processing resumes where it left off. Your code is simple enough that there is virtually no latency in the processing of the interrupt. Latency becomes a problem when your code is more involved.

    The way you have written it, the print statement is called even when there is no pulse. Though you should keep the code in the interrupt function (count) to a minimum, for your experimentation it's probably okay to call the Serial.print function in your interrupt.

    For your #2 question, you'll need to define "no movement." For this you'll have to set up a timer to see when the last pulse was received. If no pulse in x microseconds or milliseconds, you can clear the pulse variable. Look at the "blink with no delay" example code on the Arduino site to see how to compare the current milliseconds count with a previous one. You can use this as a simple timer.

    All this said, this is definitely what the Propeller is all about. No interrupts to cause latency, and eight self-contained cores to play with. A Propeller Quickstart board might be a good starter for you.

    -- Gordon
Sign In or Register to comment.