counting pulses without using interrupt

I want to count pulses without interrupt. i am using arduino nano and don’t want to block controller when it counting pulses. i am interfacing Bluetooth with arduino so can’t use interrupt. so please help me

here is my code:
[int pin=7,s=0,ch=0;
#define check 8
unsigned long time_period=0 ,end_time=0,start_time=0,frequency=0;
int previous_pin=0;
int p=0,a=0;
int temp;
void setup() {
// put your setup code here, to run once:
pinMode(pin,INPUT);
pinMode(check,OUTPUT);
Serial.begin(9600);

DDRB = B11111111;
previous_pin = 0;
}

void loop() {
// put your main code here, to run repeatedly:
//temp=digitalRead(pin);
if(a==0 || a==2){
if (digitalRead(pin)== 1 && previous_pin == 0 ){
previous_pin=digitalRead(pin);
p++;
Serial.print(“p”);
Serial.println(p);
/Serial.print("Digital pin1 ");
Serial.println(temp);
Serial.print("previous pin1 ");
Serial.println(previous_pin);
/

PORTB = PORTB | B00000001;// start_time= micros();
/Serial.print(“start_time”);
Serial.println(start_time);
/

previous_pin=digitalRead(pin);
/Serial.print(" previous else 1.1= ");
Serial.println(previous_pin);
/
a=1;
}
}
if(a==1){
if (digitalRead(pin)== 0 && previous_pin == 1 ){
previous_pin=digitalRead(pin);
a=2;
}
}
if(p==5){
PORTB = PORTB & B11111110;
delay(5000);
p=0;
}
}
]

sorry for poor English

It will be more helpful if you provide more details and what you did on this project, where exactly you are stuck and what specific help you want! Please read How to use forum so you can get maximum out of here!

Please use code tag to post your code, it helps us to discriminate and understand it properly!

yatin: Please use code tag to post your code, it helps us to discriminate and understand it properly!

Also, please put all your parentheses on their own separate lines and autoformat your code with ctrl-T in the IDE before you do that.

kalpeshpatil102: I want to count pulses without interrupt. i am using arduino nano and don't want to block controller when it counting pulses. i am interfacing Bluetooth with arduino so can't use interrupt.

This does not make sense.

Using an interrupt to count pulses is probably the least intrusive way to do so.

If you don't use an interrupt you must poll the relevant pin at regular intervals. That polling has to be sufficiently regular and frequent so that you can be CERTAIN to poll the pin during the period while the pulse is high and during the period while it is low. The requirements for polling will almost certainly have more impact on the rest of your program than an interrupt that is (for example) triggered on the rising edge of the pulse.

The code in the ISR can be as short as

void myISR() {
   pulseCount ++;
}

Also, you have not told us how frequent the pulses are or how long is each HIGH and LOW interval.

...R

aarg: Also, please put all your parentheses on their own separate lines

I got sick of doing that manually all the time when testing other people's code, so added a line to my "formatter.conf" file to do it automatically when I Auto Format:-

#Move opening brackets onto new line
--style=allman --style=bsd --style=break -A1

thank you for reply. Sorry for late reply . i can't use polling function. because i have connected Bluetooth to pin D2 and D3. i think due to polling controller may be block . can i use timer interrupt ? for counting pulses

sorry for poor English

kalpeshpatil102: sorry for poor English

The problem is not poor English - it is the failure to read the replies you have already received.

can i use timer interrupt ? for counting pulses

Why do you think a timer interrupt might have any relevance to counting pulses?

You need to tell us what you are thinking if we are to give useful advice or explanations.

...R

i think by using timer interrupt there will be no communication problem between Bluetooth device and counting pulses at same time. so i think use timer1 for counting pulses.

kalpeshpatil102: i think by using timer interrupt there will be no communication problem between Bluetooth device and counting pulses at same time. so i think use timer1 for counting pulses.

You can do that. Get a hold of the AVR datasheet for the device you're using and begin. On the Uno you can use the T0 or T1 input.

aarg: You can do that.

How?

I can understand the use of an interrupt to count pulses - I suggested the code in Reply #4

I can't understand what the role of a timer interrupt would be.

In my opinion the OP is chasing a non-existent problem.

...R

because i have connected Bluetooth to pin D2 and D3

OK, because of your pin selection you have eliminated the use of an external interrupt.

I think you will find it easier, and with more commonly found documentation and tutorials, to pursue a pin change interrupt on some other available pin than to implement a pulse counting routine using the input capture pin or the external clock source pin on Timer1.

cattledog: OK, because of your pin selection you have eliminated the use of an external interrupt.

Full marks for figuring that that was what the OP was trying to say.

Isn't the simple answer to relocate the Bluetooth device to free up one of the external interrupt pins ?

...R

@cattledog i am working in industry. i can't change Bluetooth pins. i will check for pin change function. thank you

This is a XY problem,definately. Can you tell us what you want to do actually ?? and post the code ?

OldSteve:
I got sick of doing that manually all the time when testing other people’s code, so added a line to my “formatter.conf” file to do it automatically when I Auto Format:-

#Move opening brackets onto new line

–style=allman --style=bsd --style=break -A1

Wizard!

OldSteve:
I got sick of doing that manually all the time when testing other people’s code, so added a line to my “formatter.conf” file to do it automatically when I Auto Format:-

#Move opening brackets onto new line

–style=allman --style=bsd --style=break -A1

I have many versions of IDE. How can I make all versions do that ?

MalharD:
I have many versions of IDE. How can I make all versions do that ?

I have no idea if it works with all versions of the IDE, or which it works with, for that matter, except the version I’ve tested it with - V1.6.5

There’s one easy way to find out - make a backup copy of your ‘formatter.conf’ file, then experiment.

‘formatter.conf’ can originally be found in \lib

For anyone interested, there are all sorts of little tricks that can be adopted in ‘formatter.conf’, listed here:-
Artistic Style 2.05

This is recommended in the file too:-
(I haven’t got around to trying this yet, and don’t know if it means to also delete the original file or not.)

This configuration file contains a selection of the available options provided by the formatting tool “Artistic Style”

Artistic Style

If you wish to change them, don’t edit this file.

Instead, copy it in the same folder of file “preferences.txt” and modify the copy. This way, you won’t lose your custom formatter settings when upgrading the IDE

@OldSteve Thanks,

-Malhar

@MalharD , i am using remote XY library.and i want to count pulses and show on smart phone. for this i am using Bluetooth HC05 .with remote XY application for smart phone.