Go Down

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



Sorry the ISR stuff wasn't mean't for you.
My counter code is abit verbose coz am a newbie!! I will still modify it as you said.
Still hitting hard rocks on how to call the counting code for my display.


Dec 05, 2012, 03:07 am Last Edit: Dec 05, 2012, 05:46 am by Graynomad Reason: 1
OK here's a program that (I think) will display a counter on your display

Code: [Select]
#define NDIGITS 3

byte digitPins[] = {37, 38, 39};
byte segmentPins[] = {30, 31, 32, 33, 34, 35, 36};

byte display_digits[NDIGITS];

byte current_digit = 0;

byte digitPatterns [] = {

// gfedcba // note the pin assignment are "backwards", makes it harder
B01111110, // 0
B00000110, // 1
B10110110, // 2
B10011110, // 3
B11001100, // 4
B11011010, // 5
B11111010, // 6
B01100010, // 7
B11111110, // 8
B11111010 // 9

void convert_to_digits (byte number) {

for (int i = NDIGITS-1; i > -1; i--) {
display_digits[i] = number % 10;
number /= 10;


void show_next_digit () {

digitalWrite (current_digit, LOW);

PORTC &= B00000001;
PORTC |= digitPatterns[display_digits[current_digit]];

if (current_digit >= NDIGITS) current_digit = 0;

digitalWrite (current_digit, HIGH);


void setup() {

for (int i = 0; i < 7; i++)
pinMode(digitPins[i], OUTPUT);

for (int i = 0; i < 3; i++) {
digitalWrite (i, LOW);
pinMode(segmentPins[i], OUTPUT);


void loop () {
static int  x;

// update number every 1s
if (millis() % 1000 == 0) convert_to_digits (x++);

// update display every 10mS
if (millis() % 10 == 0) show_next_digit();


Note that I got rid of all the digitalWrites() as they are a very inefficient way to do this when you have control over the pin assignment which I gather you do. Often on Arduinos you don't have much choice of pins but in this case you do so you can easily write directly to the port as I've done.

Note also that while the pin assignment seemed good when using Arduino logical IO pin numbers it's not great for direct port IO, as it is the 7 segments are on the upper 7 bits of PORTC and one of the cathode selects is on the bottom bit of PORTC. As with most things Arduino this is not well documented (if at all) but if you have a look at the Mega schematics you can see this. Also the pin-to-segment mapping is "back to front" (seg A on bit 7, seg G on bit 1), the computer doesn't care but it's harder for a human to get his head around it.

Anyway you essentially have two programs to write, the counting and the displaying. Try this to see if it works for the displaying.

Rob Gray aka the GRAYnomad www.robgray.com



I do appreciate your help and I will work on the display code.
My main problem is to call the counting code after the interrupts have happened
to update the display. Does the counting code go into the main loop just before the interrupts?
Anyone willing to help please?



Does the counting code go into the main loop just before the interrupts?

The "counting code" IS the interrupt(s).

Your main problem is to get the two halves of the code working I think.

When you have a working counter and a working display it will be easy to merge the two. But trying to get everything working at once just makes life harder.

So you say my interrupt code doesn't work, it wouldn't be the first time :), can you post what you are using?

Rob Gray aka the GRAYnomad www.robgray.com


Your counting code does not display anything at the moment so I have to work on it.

So you are a bit premature in asking for it to be merged.

Nevertheless I have done it for you in the hope that you can read it and try and learn something.

Go Up