Go Down

Topic: BI-DIRECTIONAL PEOPLE SENSOR AND COUNTER USING ARDUINO BOARD (Read 18138 times) previous topic - next topic


Dec 03, 2012, 04:57 pm Last Edit: Dec 03, 2012, 11:47 pm by Grumpy_Mike Reason: 1

how do I call the counting code after the interrupts have occured so as to display the count/counter
(incrementing or decrementing) on the 3 digit seven segment display??

You have a variable that contains the value you last sent to the display. At the start of the loop you compare this variable with the count variable. If they are not the same you update the display.


Could you give me a starter please? am starting my display with 000, increment and decrement accordingly as the sensors makes and breaks
I need to understand this more please.


It's a really bad idea to put serial I/O in interrupt routines.
It's a good idea to post code inside code tags.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


I'm gonna repeat one thing I said previously, and then go away. I see no reason to
mess with interrupts here. It's much easier to use polled I/O instead, and by far,
the best way to learn programming is to learn the easy stuff first. You don't have
100 people a second going past the counter.


What's wrong with using interrupts?can you elaborate pls.



As mentioned, when learning to program, it's generally easier to start with the
simpler and more straightforward constructs, then once you gain some experience,
go on to the more complex constructs. Then you don't get into the sort of troubles
you're getting yourself into. This is all called the Learning Curve.


Could you give me a starter please?

What is wrong with reply #15.
Also as AWOL says do not print inside ISRs, it screws things up.

If you want help do not ignore it when it is given. If you don't understand it, ask.
Go back and modify that post with code in.
Select the code and then hit the # icon. Then save it.
That will put code tags around the code and we can copy the code into our own system to better help you.


Dec 04, 2012, 02:20 am Last Edit: Dec 04, 2012, 05:48 am by Graynomad Reason: 1
While I did suggest using interrupts I didn't suggest using 4 of them and all that attach/unattach stuff.

My example was simplicity itself, it does however require that the input signal is clean with no bounce. Here's a rejigged version of what you have

Code: [Select]
int last_counter = 0;
volatile int counter = 0;

const int S2_pin = 50; // change to whatever pin it's on

void setup() {
 attachInterrupt(0, S1_RISE, RISING);//hooked to pin2 on mega 2560

void loop(){
 if (counter != last_counter) {
   counter = last_counter;

void S1_RISE(){

 if (digitalRead(S2_pin) == HIGH)

 if (counter < 0) counter = 0;

While I agree with oric_dan(333) that interrupts are not needed here if the input signal is clean I actually think interrupts are easier to use. That said I'll do a polled version as well shortly.

What sensors are you using? Do they provide a clean bounce-free signal?

Rob Gray aka the GRAYnomad www.robgray.com


My infrared sensors do give very clean pulses since they are hooked to a schmitt trigger.
My version of interrupts does work well as it captures all the 4 changes on the 2 pins
of my sensors, having said that I still need help integrating the function to display 0-999
incrementing or decrementing as neccessary. I do take the advice from reply #15.
I am also taking in all the advice so far given and much thanks to all.


I really don't see why you need all that interrupt stuff, but if it works so be it.

I still need help integrating the function to display 0-999 incrementing or decrementing as neccessary

Just to be clear, you currently have working code that prints to the serial monitor?


Rob Gray aka the GRAYnomad www.robgray.com


Hi Rob,

That's true, I can print on the serial monitor and that's where I can see the counting up/down.
And I also have the code that counts from 0 to 999 back to 0. All I want to know is how to fix the counting
code to be called upon the interrupts occurring so as to update the counter and hold that count till the next sequence of interrupts.
Am glad you are willing to help.




I have been thinking of your reply and I feel it makes sense.
Can you please insert an example on my ISR code attached?
this will be a starter for me, bearing in mind that I have the code that
displays 000---999-000, how to call it is a bit of a challenge to me as a newbie.


What we need to know is how you have the seven segment displays wired up. That schematic you posted did not show what pins were connected to what.

Then before you update the display you have to have code that will multiplex the display, do you have that? We need to see it.

Is it written correctly, there is a lot of rubbish about with very verbose code. This link shows you how to do it properly.
But you have to extend it to cope with the multiplexing.



I have tried to go your way and it seems that I need more modification as your example code doesn't give me anything on the serial monitor.


I have also noted that your ISR sketch was more easier

Odd as I don't have an ISR on that page.
Yes that sort of code was just the sort I warned against. So have a good read of that page and write the refresh code properly.

Go Up