BI-DIRECTIONAL PEOPLE SENSOR AND COUNTER USING ARDUINO BOARD

Mike,

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.

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

Graynomad,

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?

Thanks.

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

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.

merged.ino (2.17 KB)

Mike,

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.

Mike,

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

Steffano.

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.

Mike,

I will do that and let you know.

Thanks.

Mike,

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.

Thanks.

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

byte digitPatterns [] = {
//ABCDEFG    
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.

Mike,

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.

Steffano.


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

Why two schematics, they look the same.

You have

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

//ABCDEFG   
 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

:~

Rob,

I have done all I can and the only segments am getting lit are a,b,c and d, the rest are off and nothing sensible is coming out of my display.

You are not getting what I said.
The change I showed was a bit pattern that had one segment lit up for each of the numbers 0 to 7. If you display just one digit you should see just one segment lit up. Do not have the full code just display one digit at a time.
If you do not then you have not wired up your display like you say you have, you have something wrong with your hardware.

Anyway as that plan does not seem to be working:-
So try something simpler, you are trying to test if the wiring you have is correct. Write a small sketch that turns on only one transistor, and then goes through each segment one at a time and turns it on. Put a one second delay between each so you can see it come on. Turn off that segment before you turn the next one on.
You should see each segment a to g come on in turn. If you do not see this then you know it is physically wired up not like your diagram. Maybe you have misidentified the pins on the display or something. If it does work then we can tick that off with confidence and start looking at the software. But until you verify the hardware is correct it is pointless looking at anything else.

It is also a valuable lesson for you into how to find a fault.

Mike,

I slept 5am this morning working on this, my hardware is perfect. My verbose 000--999 counting code I posted before works flawlessly
have even tried to do another simplified counting code and all counts perfectly. The problem is in the software(the code you sent to me)
No matter what I change 0 or ones the display stays the same all through.

Steffano

Here is the absolute simplest thing I can think of to test the bits along the lines of what Mike is saying.

byte digitPins[] 	= {25,26,27};
byte segmentPins[] 	= {30,31,32,33,34,35,36};

byte digitPatterns [] = {

//	 ABCDEFG    
	B00000000, // 0
	B10000000, // 1
	B01000000, // 2
	B00100000, // 3
	B00010000, // 4
	B00001000, // 5
	B00000100, // 6
	B00000010, // 7
	B11111110, // 8
	B11100110, // 9
};

 
void setup() {
	 
	for (int i = 0; i < 3; i++) {
		digitalWrite (i, LOW);
		pinMode(segmentPins[i], OUTPUT);
	}
 
	pinMode(25, OUTPUT);
 	digitalWrite(25, LOW);
	 
	PORTC = digitPatterns[1];  // <==== change this number to try all 7 possibilities

}
 
void loop () {}

Run that and see what segment lights up. Then play with the indicated number and record the results.

EDIT: I did notice one thing in your code though, you are writing to PORTA to set the segments, your displays are on PORTC are they not?


Rob