Counting length of pulses

Hi!

Im doing a school project and you guys on here are my only hope smiley-confuse

I will have an input from another microcontroller and i need to count the length of pulses,
there will be 3 different pulses one after another.

So, basically there will be one pulse for lets say 21 seconds, i need to store this,
then 5 seconds or so after that pulse has finished another will start on the same pin for lets say 15 seconds, i need to store this separately,
finally 5 seconds after the previous pulse there will be one last pulse for 3 seconds and i need to store this separately from the other 2 pulses,

can anyone help?
I hope it all makes sense :confused:

Thanks in advance!

Tom

Please don't post the same thing in mulitple places - post once, have a little patience.

Read the Learning page on pulseIn( ). It will measure pulse widths, you can put the values in seperate variables, or an array, or whatever.

CrossRoads gave you the hint on what function to use (pulseIn), and here is a nudge for storing your pulses.

You need to store three items, right? Say, "FirstPulse", "SecondPulse", and "ThirdPulse".

Once you get your first pulse determined you would want it to go into "FirstPulse", but only that one time and then move the next one into "SecondPulse", and so on.

So once you get your non-zero value from pulseIn, check to see if any (or all) of your variables currently have nothing in them. If it does, move on to the next one.

You could even report back on the serial line the pulse lengths once you get your third pulse in.

rootboy:
Say, "FirstPulse", "SecondPulse", and "ThirdPulse".

Say, "pulse[0]", "pulse[1]", "pulse[2]".

"Say, "pulse[0]", "pulse[1]", "pulse[2]"."

Sure, if that's what floats your boat. :slight_smile:

I'll trade a programmatically proper method for a human-readable one just about always. I'm irresponsible that way.

But you're right, in a non-trivial program, you want to focus on maintainability and scalability. Often at the expense of readability. And since this youngster is probably going to replace one of us eventually, it would be nice to train them up properly.

But I'm a PLC jockey, and we live in an entirely different world. And we are supposed to know what about a ja-zillion bits are supposed to be doing at any given time. It sure is nice when there is a descriptive name for that bit.

For instance, you have to get into the program to start up a pump on a mix tank. And based on where the terminal is located, you can't directly see the machine. And on either side of the tank that you do want to start are tanks that emphatically must not be turned on.

So would you rather have an array element to look up?, or a variable name that clearly defined it as the pump that you want to run?

When you get old like me, you have more things to worry about than appeasing the stylistic gods (like trying to remember where I put my glasses, my keys, and to remember to zip up my fly after taking a leak - that one always gets me).

All that aside, one of the fundamental principles for software development is don't repeat yourself. If you need to do the same thing three times, you want one lot of code, not three copies. In this case that leads to an array and a for loop, not separate variables and duplicated code to access them.

We intentionally do that. Repeat ourselves, that is. If it's a choice between a hundred rungs of additional logic (with PLCs we program in rungs, not lines) or doing something tricky with indirect moves, we generally choose the extra rungs.

You and I program in entirely different worlds. Until you have programmed for machinery, and not just PCs you won't have the same frame of reference as I do. That said, all of the fancy stuff in the PC world is lost on me.

07lodgeT:
Im doing a school project and you guys on here are my only hope smiley-confuse

I will have an input from another microcontroller and i need to count the length of pulses,
there will be 3 different pulses one after another.

So, basically there will be one pulse for lets say 21 seconds, i need to store this,
then 5 seconds or so after that pulse has finished another will start on the same pin for lets say 15 seconds, i need to store this separately,
finally 5 seconds after the previous pulse there will be one last pulse for 3 seconds and i need to store this separately from the other 2 pulses,
Tom

What have you learnt in school? What type of problem is this? Do you know what a finite state machine is? And how it can be programmed? Is the signal stable? What is the distance, length of the cable? Is there noise? How do you indent to connect the two microcontrollers? Does the solution have to be low power? Will the microcontrollers run on battery? Are there programming languages for this type of problem?

Cheers!

rootboy:
you won't have the same frame of reference as I do

That is certainly true. I'm not familiar with PCL programming, and I'm sure there are lots of practices that make sense there that would be considered poor practice in a procedural programming environment. Just because you're used to working that way, doesn't make it a good way to approach Arduino software development.