Go Down

Topic: BI-DIRECTIONAL PEOPLE SENSOR AND COUNTER USING ARDUINO BOARD (Read 15003 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.



hahahaha Got you! Thanks alot It still needs abit of tweaking here and there.I think it's the ports now.
Am checking the arduino mega 2560 schematics to see the port of my pins.
Sorry for being a newbie!!

Thank you very much.



I have rearranged the byte digitpatterns and atleast something is coming up on my display,though can't recognise what digits they are apart from digit 1, however, This is not exactly what I want but I will keep working till I achieve this.

This time round I will be mature to ask for any more help as these ideas are great and very helpful.

Much thanks



The trick is to find out the relationship between a bit in your data with the corresponding segment position.
It sounds like there is a miss match between what you have wired up and what the code thinks the wiring is.
So just have a digit with one segment on and see where it shows up. Repeat for all seven segments, then you can write the bit patterns in your code to match the physical layout you have.




I am still struggling with the display code to be honest. it lights my first digit wired to pin 37 and the digit is not clear enough can only see number 1.
my seven segments (a to f) are wired to pins 30 to 36) and my digit enable pins are 37, 38,39 and up to now I can't figure it out.



it lights my first digit wired to pin 37 and the digit is not clear enough can only see number 1.

Not sure I understand this?

and up to now I can't figure it out.

Figure what out.

You know the drill, post your code because it has changed since the last time.

However first try and fix it yourself. Just write a sketch showing one digit and advance the count on a push button at the same time print out the number you are displaying. See how the display differs from the number it is showing. Use that to correct the pattern look up table.


Below is my modified code;

The code needs to sit between the code tags.

Change the pattern defination to this
Code: [Select]
byte digitPatterns [] = {
B0000000, // 0
B1000000, // 1
B0100000, // 2
B0010000, // 3
B0001000, // 4
B0000100, // 5
B0000010, // 6
B0000001, // 7
B1111111, // 8
B1110011, // 9

Then tell us what is displayed for digits 0 to 7. Do this by drawing a diagram of the lit and unlit segments for each number. Then we can tell you what patterns to use.


Dec 16, 2012, 04:27 pm Last Edit: Dec 16, 2012, 05:56 pm by stefanoGizz Reason: 1

I am completely lost even after changing the code it displays some funny characters and only digits  1 and 3 are enabled.
The lit segments on both digits 1 and 3 are a,b,c and d the rest of the segments are off. My verbose digitalwrite long counter
still works fine even after swapping the 37,38 and 39 pins for 25,26 and 27 on PORTA.  
Schematics to follow in a bit.


Code: [Select]


Can you please put the code inside the CODE tags. You must know the drill by now.

Note what Mike said, run your code and have it put out the numbers 0-7, tell us what segments are lit for each of those 7 numbers.

Rob Gray aka the GRAYnomad www.robgray.com


Dec 16, 2012, 05:27 pm Last Edit: Dec 16, 2012, 05:30 pm by Graynomad Reason: 1
Why two schematics, they look the same.

You have

Code: [Select]
a  30  PC7
b  31  PC6
c  32  PC5
d  33  PC4
e  34  PC3
f  35  PC2
g  36  PC1

so you pin mapping should be

Code: [Select]
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110, // 9

But it's 2AM here so I could be out. You could plug this in, but if it doesn't work out of the box go back to Mike's plan.

Stop trying to test everything at once, break the problem down into small pieces. You need to remove all the crap and just have a small program that outputs the bit patterns that Mike had, hardcode the number to 0 and record the lit segment, then hardcode the number to 1, record and repeat until 7.

This will identify exactly which bit in the array values is connected to which pin. Do things slowly and methodically, make one change at a time and ANALYSE the results then present them in a clear tabulated form.

Rob Gray aka the GRAYnomad www.robgray.com

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131