Hi there...
I am trying to count the no of object pass in a conveyor systems and it need to be displayed in a LCD every time a object passes through it. I used IR for this purpose... wen ir detect the Object the led gets turned off and vice versa. Now the problem is wen LED turned off the counter keeps on counting till it get turned on
And also the counter counts only upto value 10 and it gets return to 1...
can any one pls help me.
check the program below
while(True) { if (LED1 == 0) { Count = Count+1; //LED1 = 1; }
// UserMessageStorage[4] = Count; // UserMessageStorage[1]+= 1;
UserMessageStorage[4]= Count/1000; Count=(Count-(UserMessageStorage[4]*100)); UserMessageStorage[4]+=0x30;
UserMessageStorage[5]= Count/100; Count=(Count-(UserMessageStorage[5]*10)); UserMessageStorage[5]+=0x30;
UserMessageStorage[6]= Count%100; UserMessageStorage[6]+=0x30;
TotalCount[0] = UserMessageStorage[4]; TotalCount[1] = UserMessageStorage[5]; TotalCount[2] = UserMessageStorage[6];
// u8Temp = (TotalCount[0] - 0x30) * 100; // u8Temp += (TotalCount[1] - 0x30) * 10; // u8Temp += (TotalCount[2] - 0x30) * 1; //
//TotalCount[0] = UserMessageStorage[1];
// SerTx(TotalCount[0]); // SerTx(TotalCount[1]); // SerTx(TotalCount[2]);
ArrayBasePtr="Total Objects= "; DisplayLCD(1,0,ArrayBasePtr); ArrayBasePtr=&TotalCount[0]; DisplayLCD(2,0,ArrayBasePtr); ArrayBasePtr=" Objects"; DisplayLCD(2,3,ArrayBasePtr); TimeDelay(1000); LCD_CLEAR();
Thanks in advance guys
So you have just learned the difference between flank-trigged and level-trigged signal processing.
Why do you let your counter tick constantly when the light beam is blocked? Why not just count one time and then wait for the light beam to be detected again before you rearm your counter?
UserMessageStorage[4]= Count/1000; Count=(Count-(UserMessageStorage[4]*100)); UserMessageStorage[4]+=0x30; UserMessageStorage[5]= Count/100; Count=(Count-(UserMessageStorage[5]*10)); UserMessageStorage[5]+=0x30; UserMessageStorage[6]= Count%100; UserMessageStorage[6]+=0x30;
So what happens if count is 3697 and you do the above?
Count = 3697 UserMessageStorage[4] = 3697/1000 (3) + 0x30. I.e. '3'. Count = 3697 - 3*100 = 3397;
UserMessageStorage[5] = 3397/100 (33) + 0x30. I.e. 'Q'. Count = 3397 - 33*10 = 3067;
UserMessageStorage[6] = 3067%100 (67) + 0x30. I.e. 's'.
So the counter value 3697 should then be represented as '3Qs'. Whas that really what you intended?
And the other thing is that your printout destroys your counter by modifying it. Isn't that quite silly?
The big question that may be a bit embarrasing: Haven't you tried to debug your code? Haven't you tried to give Count a value and then see what would happen, step-by-step? And see if you get a suitable value to print - and that you then get back to the top of your loop with Count still containing a meaningful value?
So you haven't still solved the problem with count ticking wildly while the light beam is broken?
Didn't you pick up the hint from my previous comment about flank-trigged (also called edge-trigged) and level-trigged events?
What magic thing do happen when the beam gets broken (note the transitional expression "gets broken" as separate from "is broken")?
I even followed up by writing "Why not just count one time and then wait for the light beam to be detected again before you rearm your counter?"
What have you done about it? What do you think "rearm" could mean? What actions do you do when you see the light beam again? Or what actions do you not do?
What would you do, as human being, if someone asks you to count how many times the sun light is blocked by a cloud? Would you count continuously while the sun is blocked? Or would you count just once, and then stop counting and wait until you can see the sun again? Do you really think it makes a difference if it's a microcontroller that counts, or if a human count? Isn't the problem still the same?
Might it help if we write that as "re-arm"?
Another fundamental requirement for a programmer is the ability to analyse a problem.
You need to have done this before you even start to think about coding!
There are many techniques - including flowcharts, etc.
Once you have analysed the problem, you can then start thinking about and then designing a solution to the problem - again, this is before you even start to think about coding!
Finally, once you have a design, you can implement it - which is where the coding starts...
View all questions in Keil forum