BI-DIRECTIONAL PEOPLE SENSOR AND COUNTER USING ARDUINO BOARD

OK here's a program that (I think) will display a counter on your display

#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]];
	 
	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