count and send pulses simultaneously

Hi everyone,

I'm using an Arduino Due to count and send out pulses. The sending out pulses are suppose to last for 1000 microseconds. How do I keep counting the incoming pulses at the same time?

Here is the code:

int encoderCount = 0;
boolean sampledPin;
boolean lastVal=0;

void setup() {
// put your setup code here, to run once:
pinMode(3,OUTPUT);
pinMode(22,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:

while(true)
{
sampledPin = !!(PIOB->PIO_PDSR & (1<<26));

if(lastVal ^ sampledPin)
{
encoderCount++;
lastVal = sampledPin;
}

if (encoderCount == 3000 )
{
encoderCount = 0;

PIOC->PIO_SODR=1<<28;
delayMicroseconds (1000) ;
PIOC->PIO_CODR=1<<28;
}
}

}

Hi everyone,

I'm using an Arduino Due to count and send out pulses. The sending out pulses are suppose to last for 1000 microseconds. How do I keep counting the incoming pulses at the same time?

Here is the code:

int encoderCount = 0;
boolean sampledPin;
boolean lastVal=0;

void setup() {
// put your setup code here, to run once:
pinMode(3,OUTPUT);
pinMode(22,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:

while(true)
{
sampledPin = !!(PIOB->PIO_PDSR & (1<<26));

if(lastVal ^ sampledPin)
{
encoderCount++;
lastVal = sampledPin;
}

if (encoderCount == 3000 )
{
encoderCount = 0;

PIOC->PIO_SODR=1<<28;
delayMicroseconds (1000) ;
PIOC->PIO_CODR=1<<28;
}
}

}

If you want to be able to do something while delaying, do not use delay(...) or delayMicroseconds(...). Learn to use millis() and micros().

At the top of this forum is a tutorial on how to use millis(). The same principles apply for using micros().

I am puzzled. What does !! accomplish? Doesn't this just waste time unless it gets optimized out?

See the example sketch, Blink without delay.

On the due, you can attach an interrupt handler to each pin.

void ISR1() {

//rising edge
//count up

}

const uint8_t input = 22;
attachInterrupt(input, ISR1, RISING);

JarvisSong:
I'm using an Arduino Due to count and send out pulses. The sending out pulses are suppose to last for 1000 microseconds. How do I keep counting the incoming pulses at the same time?

This is confusing.

You say you are sending out pulses and then you say you want to count incoming pulses. Which are you doing - sending or receiving?

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

What is the application?

Are you counting pulses from an external source and want to gate them ON for a particular number of pulses or time.
OR
Are you producing the pulses in the DUE and gating them ON for a particular number of pulses or time.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

have a look at the tc_lib library for the Due

crosspost: https://forum.arduino.cc/index.php?topic=547718.msg3733727#msg3733727

@JarvisSong, please do not cross-post. Threads merged.

I have got it working.

Thank you all for your helpful advice.

(Sorry I cross-posted, I'm new to the community)

JarvisSong:
I have got it working.

Thank you all for your helpful advice.

(Sorry I cross-posted, I'm new to the community)

To thank us, can you tell us what you did and post your code please.
Thanks..Tom.... :slight_smile: