Hello Guest it is November 29, 2021, 12:14:01 AM

### Author Topic: How to do a Counter in Brains.............  (Read 9524 times)

0 Members and 1 Guest are viewing this topic.

#### poppabear

• 2,235
• Briceville, TN, USA
##### How to do a Counter in Brains.............
« on: February 03, 2008, 03:27:04 PM »
Greetings,

Here is how to do a "counter" in brains, each time "Input1" is triggered the DRO(1500) increments by 1. When you push "Input2" it resets the DRO to "0".  You can change the formula to reflect your count multiplier.

Also at this post: http://www.machsupport.com/forum/index.php/topic,5568.0.html my post at the bottom of this thread you can download a working brian that increments by One on each press of an external button. It compares the DRO, then scales first the X, then the Y, then the Z, the next press sets the scaling of all axis back to normal, the next press resets the system back to Zero for a practical application example.

Scott
fun times

#### dhookings

• 39
##### Re: How to do a Counter in Brains.............
« Reply #1 on: February 04, 2008, 12:31:27 PM »
The resolution on my screen makes some of the letters difficult to read and when I tried to print out the brain I got everything compressed to a half inch line at the top of the page.
Input 1 is zero not pressed, one pressed. This is paused by a timer (10msecs?) presumably for debouncing though I can't see how. It is then passed to V0 which is presumably an internal register of some kind.
Lines two and three continuously add V0 and a DRO, passing the result to the DRO, effectively incrementing it. I can understand this so far although I can't see a need for the nops, and the timer has to ignore inputs while it is counting.
But the fourth lline has me puzzled. A*0 is 0 in any language so this line is continuously clearing the DRO negating the above functions.
What am I missing here?

Regards

Dave Hookings

#### poppabear

• 2,235
• Briceville, TN, USA
##### Re: How to do a Counter in Brains.............
« Reply #2 on: February 04, 2008, 10:16:56 PM »
Dave, the Zip file has a WORKING brain for your review. The Input1 goes through a OneShot op lob that only has a .1 second pulse to roughly match the scan time of mach. The Input2 is used to clear the counter using the A*0.

I only showed the beginning and end(clearing rung), between these two places you can add your own comparing code or what ever to act on the incrments.

scott
fun times

#### dhookings

• 39
##### Re: How to do a Counter in Brains.............
« Reply #3 on: February 05, 2008, 08:42:20 AM »
Thanks Scott.
I did download the zip file which had 4 inputs and three outputs if it is the correct file. I think I need to get the overall strategy, especially with the timers. I'm sure I read somewhere that these brains are super quick. That makes the 100 milleseconds delay seem long.
You have four inputs, with input #1 and the timer on the first line. Does the programme just stop functioning during the timer period or does the delay just hold up that particular terminating function?
In other words, does the 'Formula A+B' operate every few microseconds or just once every 100 milliseconds?
Then I have to work out how the timer actually works

Regards

Dave

#### poppabear

• 2,235
• Briceville, TN, USA
##### Re: How to do a Counter in Brains.............
« Reply #4 on: February 05, 2008, 04:10:38 PM »
Dave,

Open the Brain under mach view brain, and look at it, there is only 2 (TWO) inputs, one of them activates the timer which functions as a "One Shot" like in a PLC, I put the pulse time at 0.1 second since Mach cycles at 10 hrts, this allows the Input to only be on for ONE scan, and thus increments the DRO by one. IF, for some reason your counter is incrementing MORE than one per singular press of your input, then lower the Pluse time of your OneShot to 0.05 seconds. On my computer, I have had no problems with the oneshot set at 0.1. The other Input just resets your counter to 0 that is all it does.
So for each press of your input (and please note that Mach services all its I/O at 10 hrts, thus your brain isnt going to change something faster than that, even if it does Mach only comes by every 1/10 sec to update the changes..........

The Pulse time of your Timer has to be Smaller than Machs scan time for the One Shot to work and put out only a single pulse for that scan. If you Pulse time is set longer than machs scan time then it will quickly increment up your counter by what ever value per scan of mach.

Scott
fun times

#### dhookings

• 39
##### Re: How to do a Counter in Brains.............
« Reply #5 on: February 06, 2008, 05:49:46 AM »
Scott

A slight misunderstanding here. When I said four inputs I was referring to the Brains logic diagram, so I was including V0 and OEMDRO:1500 as inputs. You thought I meant four physical inputs and wondered WTF is this guy on:-)

I'm trying to get a better idea of how the brains timers work, within the Brains environment.

As I see it, in Mach, a physical switch input has two states, on/true/1 or off/false/0 with isactive() returning either 1 or 0 The timer in your example appears to be configured as a positive edge triggered one shot with a period of 100 milliseconds. So, as soon as input#1 switch is made, V0 (which I assume is an implicit variable local to this Brain) becomes 1.
Now my problem: What happens then. Does this Brain pause for 100ms before carrying out the next step of incrementing? If so, what happens to other Brains in the system?

Handling of input#2. This still does not make sense to me. I would think the formula needs to be along the lines of "If input#2 then 0". I don't know the syntax to do this in Brains.

Thanks for your help so far.

Regards

Dave

#### poppabear

• 2,235
• Briceville, TN, USA
##### Re: How to do a Counter in Brains.............
« Reply #6 on: February 06, 2008, 08:55:34 AM »
Dave,

Nothing happens to the other parts of the brain.......  The Brains status is only updated ever 100 milliseconds (or 10hrtz). I wanted the input1 to not stay on longer than the scan time so, for all practical purposes, the input was only on for ONE scan.  If you need to know deeper, ask Brian. They are just a logic function that does its thing on the Rung that it is on, its will stay on ("1") for the duration of the pulse time.

Input2 is a ("1") when it is on, the formula Multiplies "1" by "0" giving a Zero, then writes that zero to the counter DRO which in effect resets the counter.

Scott
fun times

#### dhookings

• 39
##### Re: How to do a Counter in Brains.............
« Reply #7 on: February 06, 2008, 12:42:23 PM »
Scott,

I'm sorry, but I really can't understand the logic behind this. If the code only triggers once every 100 milliseconds I can't see the point in having a 100 millisecond delay.

And the input#2 thing. 0 multiplied by 0 is also zero, so whenever that code is run the counter will be reset, regardless of the state of input#2.?

I'll try some experimenting when I get a chance to see if I can work it all out by trial and error.

Regards

Dave

#### poppabear

• 2,235
• Briceville, TN, USA
##### Re: How to do a Counter in Brains.............
« Reply #8 on: February 06, 2008, 03:37:19 PM »
Dave,

On the oneshot timer, the code doesnt trigger every 100 milliseconds, it only goes high for 100 milliseconds when INPUT1 goes high. The pulse duration once it goes high is 100 miliseconds, the same as the scan time of Mach.
Perhaps someone else here with better communication/explaination skills can chime in here, since I am not able to explain it good enough.

INPUT2,  the formula doesnt even come into play to reset the DRO unless you first trigger INPUT2, otherwize that rung is ignored. It only will reset to 0 if you trigger INPUT2.

Scott
« Last Edit: February 06, 2008, 03:42:23 PM by poppabear »
fun times

#### Chaoticone

• 5,626
• Precision Chaos
##### Re: How to do a Counter in Brains.............
« Reply #9 on: February 06, 2008, 03:53:06 PM »
I think what Scott is trying to say is this, the timer is the same as holding the button in for 100 ms, no more and no less ensuring Mach sees it once and once only. It it weren't for the timer, you could hit the button very fast, less than 100ms and Mach may not see it. Depending on the timeing of the scan vs button press. If you held it in for 200 ms, Mach would recognize it as being hit 2 twice without the timer.

Hope this helps,
Brett

Edit
If you held it in for 200 ms, Mach would recognize it as being hit 2 twice without the timer.
is wrong
The pulse on a one shot will only trigger one time for its duration, as long as the button is held
If the button was held for 10 minutes it would still only pulse once for 100ms
you have to release and repress to retrigger the oneshot as long as you dont press it faster than its pulse rate
« Last Edit: February 06, 2008, 04:40:20 PM by Chaoticone »
If you could see the things I have in my head, you would be laughing too.

My guard dog is not what you need to worry about!