# Small engine dyno need help to reset my counter after run

**URL:** <https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215>\
**Category:** General\
**Created:** [25 March 2021 19:27 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215 "2021-03-25T19:27:51Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 March 2021 14:52 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/22 "2021-03-29T14:52:49Z")

</div>

What does the comment on the second line of my function say?

---

<div class="post-metadata">

**Author:** ![dirtydredrt](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@dirtydredrt](https://discourse.nodered.org/u/dirtydredrt)\
**Post date:** [29 March 2021 14:55 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/23 "2021-03-29T14:55:52Z")

</div>

line 2 msec

---

<div class="post-metadata">

**Author:** ![dirtydredrt](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@dirtydredrt](https://discourse.nodered.org/u/dirtydredrt)\
**Post date:** [29 March 2021 14:58 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/24 "2021-03-29T14:58:29Z")

</div>

```auto
[{"id":"8583a880.175fd8","type":"inject","z":"c502d54b.368d98","name":"Sensor","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":310,"y":300,"wires":[["12c451d2.0c1b5e"]]},{"id":"12c451d2.0c1b5e","type":"function","z":"c502d54b.368d98","name":"Count Revs","func":"// starts timing when counts reach low threshold\n// when high threshold is reached then passes on time difference in msec\n// Count can be reset by passing a message with topic \"reset\"\n// Set low and high thresholds by passing in topic of \"lowThreshold\" or \"highThreshold\"\n// with value in payload.\n// Any other topic is assumed to be a sensor input\n\nswitch(msg.topic) {\n case \"reset\":\n // reset counter\n context.set(\"counter\", 0)\n msg = null\n break;\n \n case \"lowThreshold\":\n context.set(\"lowThreshold\", msg.payload)\n msg = null\n break;\n \n case \"highThreshold\":\n context.set(\"highThreshold\", msg.payload)\n msg = null\n break;\n \n default:\n // sensor input\n handleSensor()\n break;\n}\nreturn msg\n\nfunction handleSensor() {\n const lowThreshold = context.get(\"lowThreshold\")\n const highThreshold = context.get(\"highThreshold\")\n counter = context.get(\"counter\") || 0\n counter++\n if (counter == lowThreshold) {\n // we have hit the first threshold, remember the current time in msec\n const now = new Date().getTime()\n context.set(\"startTime\", now)\n node.warn(`startTime: ${now}`)\n msg = null // don't send anything\n } else if (counter == highThreshold) {\n const highNow = new Date().getTime()\n // we have hit the second threshold, work out the time difference\n msg.payload = highNow - context.get(\"startTime\")\n } else {\n // neither threshold so do nothing\n msg = null\n }\n context.set(\"counter\", counter)\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":610,"y":300,"wires":[["4ee39ede.8ba78","50cf5284.3f22ac"]]},{"id":"4ee39ede.8ba78","type":"debug","z":"c502d54b.368d98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":340,"wires":[]},{"id":"acc6affc.931e5","type":"inject","z":"c502d54b.368d98","name":"Clear counter","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"reset","x":370,"y":20,"wires":[["12c451d2.0c1b5e"]]},{"id":"aeac78ed.2ba2b8","type":"inject","z":"c502d54b.368d98","name":"Set low threshold 3","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"lowThreshold","payload":"3","payloadType":"num","x":340,"y":120,"wires":[["12c451d2.0c1b5e"]]},{"id":"62f6b65a.24a0d8","type":"inject","z":"c502d54b.368d98","name":"Set high threshold 10","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"highThreshold","payload":"10","payloadType":"num","x":300,"y":200,"wires":[["12c451d2.0c1b5e"]]},{"id":"2d4bf090.c32f","type":"ui_gauge","z":"c502d54b.368d98","name":"","group":"5d511d53.994404","order":3,"width":"2","height":"2","gtype":"gage","title":"gauge","label":"deg c","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":860,"y":480,"wires":[]},{"id":"7d0425ab.6f341c","type":"ui_gauge","z":"c502d54b.368d98","name":"","group":"5d511d53.994404","order":1,"width":"2","height":"2","gtype":"gage","title":"gauge","label":"%","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":850,"y":560,"wires":[]},{"id":"9f4b9f9e.69796","type":"rpi-gpio in","z":"c502d54b.368d98","name":"","pin":"11","intype":"tri","debounce":"25","read":false,"x":250,"y":360,"wires":[["12c451d2.0c1b5e"]]},{"id":"70ae121d.ffa51c","type":"inject","z":"c502d54b.368d98","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":480,"wires":[["bf3224a0.6e13b8"]]},{"id":"29d6f69c.5693ca","type":"function","z":"c502d54b.368d98","name":"Temperature","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":500,"wires":[["2d4bf090.c32f","61fbe8f9.9bddb8"]]},{"id":"49cb0622.1dd118","type":"function","z":"c502d54b.368d98","name":"Humidity","func":"msg.payload = msg.humidity\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":570,"y":560,"wires":[["7d0425ab.6f341c","22bcffa3.858d7"]]},{"id":"bf3224a0.6e13b8","type":"rpi-dht22","z":"c502d54b.368d98","name":"","topic":"rpi-dht22","dht":"11","pintype":"0","pin":4,"x":280,"y":480,"wires":[["29d6f69c.5693ca","49cb0622.1dd118"]]},{"id":"61fbe8f9.9bddb8","type":"debug","z":"c502d54b.368d98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":520,"wires":[]},{"id":"22bcffa3.858d7","type":"debug","z":"c502d54b.368d98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":620,"wires":[]},{"id":"50cf5284.3f22ac","type":"ui_gauge","z":"c502d54b.368d98","name":"","group":"9e96c901.0264f8","order":2,"width":"6","height":"6","gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":"14000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":820,"y":300,"wires":[]},{"id":"daa1a0cc.a072f","type":"ui_button","z":"c502d54b.368d98","name":"","group":"2c4c6cf8.d77924","order":1,"width":"3","height":"1","passthru":false,"label":"Start Run","tooltip":"","color":"","bgcolor":"","icon":"","payload":"reset","payloadType":"str","topic":"topic","topicType":"msg","x":400,"y":60,"wires":[["12c451d2.0c1b5e"]]},{"id":"3db7f87d.298758","type":"ui_button","z":"c502d54b.368d98","name":"","group":"d8c549ba.2c7f88","order":2,"width":"3","height":"1","passthru":false,"label":"Set Start RPM","tooltip":"","color":"","bgcolor":"","icon":"","payload":"setlowthreshold","payloadType":"str","topic":"topic","topicType":"msg","x":390,"y":160,"wires":[[]]},{"id":"9f265a39.9f3d88","type":"ui_button","z":"c502d54b.368d98","name":"","group":"d8c549ba.2c7f88","order":3,"width":"3","height":"1","passthru":false,"label":"Set Max RPM","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"topic","topicType":"msg","x":360,"y":240,"wires":[[]]},{"id":"5d511d53.994404","type":"ui_group","name":"Weather","tab":"70eb2da0.452ad4","order":5,"disp":true,"width":"6","collapse":false},{"id":"9e96c901.0264f8","type":"ui_group","name":"DYNO","tab":"70eb2da0.452ad4","order":2,"disp":true,"width":"10","collapse":false},{"id":"2c4c6cf8.d77924","type":"ui_group","name":"Run Command","tab":"70eb2da0.452ad4","order":1,"disp":true,"width":"6","collapse":false},{"id":"d8c549ba.2c7f88","type":"ui_group","name":"SETUP","tab":"70eb2da0.452ad4","order":6,"disp":true,"width":"6","collapse":false},{"id":"70eb2da0.452ad4","type":"ui_tab","name":"LANGE RACING","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 March 2021 14:58 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/25 "2021-03-29T14:58:43Z")

</div>

What?  
The second line of the function I exported is  
`// when high threshold is reached then passes on time difference in msec`

---

<div class="post-metadata">

**Author:** ![dirtydredrt](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@dirtydredrt](https://discourse.nodered.org/u/dirtydredrt)\
**Post date:** [29 March 2021 15:00 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/26 "2021-03-29T15:00:44Z")

</div>

yes line 2 high threshold = msec  
I get it.  
Can you open the flow i just sent?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 March 2021 15:02 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/27 "2021-03-29T15:02:31Z")

</div>

Yes, why. What is the problem?

---

<div class="post-metadata">

**Author:** ![dirtydredrt](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@dirtydredrt](https://discourse.nodered.org/u/dirtydredrt)\
**Post date:** [29 March 2021 15:07 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/28 "2021-03-29T15:07:26Z")

</div>

Sorry for my stupidity first node red project  
On my dash board, I have added buttons. I would like to set set the values of Set start RPM and the Set Max RPM  
Then press the Start Run button to reset counter and start the run.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 March 2021 15:09 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/29 "2021-03-29T15:09:42Z")

</div>

Which bit isn't working?

Do you understand now why you got 1305 out of the function?

---

<div class="post-metadata">

**Author:** ![dirtydredrt](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@dirtydredrt](https://discourse.nodered.org/u/dirtydredrt)\
**Post date:** [29 March 2021 15:12 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/30 "2021-03-29T15:12:34Z")

</div>

the run started with 3 counts. when it gets to 10 counts it captures the run time in msec 1305

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 March 2021 15:12 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/31 "2021-03-29T15:12:58Z")

</div>

So what isn't working for you?

---

<div class="post-metadata">

**Author:** ![dirtydredrt](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@dirtydredrt](https://discourse.nodered.org/u/dirtydredrt)\
**Post date:** [29 March 2021 15:15 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/32 "2021-03-29T15:15:30Z")

</div>

it is working great, I'm trying to add functionality to the dashboard.  
When you open the flow can you see the dashboard i have created?

---

<div class="post-metadata">

**Author:** ![dirtydredrt](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@dirtydredrt](https://discourse.nodered.org/u/dirtydredrt)\
**Post date:** [29 March 2021 15:19 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/33 "2021-03-29T15:19:55Z")

</div>

To set the Start RPM and Max RPM i should use a text input, not a button? The variable would be lowThreshold and highThreshold

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 March 2021 15:23 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/34 "2021-03-29T15:23:19Z")

</div>

Yes, Text or Numeric, with the topic set (topic not variable).

However you are talking about RPM again. I presume the gpio is counting once for each turn on the engine, so it is counting total revolutions not speed. So the thresholds are the total number of times the engine has rotated, not how fast it is turning.

I pointed that out at the start but you said that was what you wanted, total number of turns not speed of rotation.

---

<div class="post-metadata">

**Author:** ![dirtydredrt](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@dirtydredrt](https://discourse.nodered.org/u/dirtydredrt)\
**Post date:** [29 March 2021 15:26 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/35 "2021-03-29T15:26:28Z")

</div>

Yes you are correct. gpio will be counting once per revolution

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 March 2021 15:31 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/36 "2021-03-29T15:31:22Z")

</div>

So you don't want to time how long it takes to get from 3000 RPM to 13000 RPM, just how long it takes to count the next 10000 revolutions after has turned the first 3000 times?

---

<div class="post-metadata">

**Author:** ![dirtydredrt](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@dirtydredrt](https://discourse.nodered.org/u/dirtydredrt)\
**Post date:** [29 March 2021 15:42 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/37 "2021-03-29T15:42:34Z")

</div>

The engine will be directly coupled to a flywheel.  
Start the engine and the flywheel turn together. so this will be just below low threshold.  
Im not sure yet at what engine rpm or rotations it will take to idle the engine turning the flywheel.  
Thats why I need the be able to set the low threshold above engine idle.  
Accelerate the throttle and time the run up to at high threshold number that was set.  
Time from low threshold to high threshold in msec is what I what.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 March 2021 15:46 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/38 "2021-03-29T15:46:06Z")

</div>

I am still not convinced you understand what I mean. You talked about 3000 and 13000. Are those RPM or counts of the number of times it has turned over?

---

<div class="post-metadata">

**Author:** ![dirtydredrt](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@dirtydredrt](https://discourse.nodered.org/u/dirtydredrt)\
**Post date:** [29 March 2021 15:49 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/39 "2021-03-29T15:49:27Z")

</div>

the engine lets say idles at 2500 rpm. The flywheel will be turning 2500 rpm also  
That's where i get the start 3000rpm to end 13000rpm  
So i would think rpms

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 March 2021 15:53 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/40 "2021-03-29T15:53:40Z")

</div>

In that case what we have will not work. Suppose the engine is running at 3000 RPM and you start the counter, but leave the engine at 3000 RPM. In 1 minute the counter will get to 3000 and the function will start timing. Leave the engine at 3000RPM and in another 3 minutes and 20 seconds the counter will get to 13000 and will tell you that it has taken 200 seconds, but the engine is still at 3000 RPM.

---

<div class="post-metadata">

**Author:** ![dirtydredrt](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@dirtydredrt](https://discourse.nodered.org/u/dirtydredrt)\
**Post date:** [29 March 2021 15:58 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/41 "2021-03-29T15:58:02Z")

</div>

once the engine is at idle say 2500rpm i start the test.  
engine goes to Full throttle  
the program starts its counts as the flywheel reaches 3000 counts  
and runs until we see 13000 rotations. run finished.  
the run should take less that 10 seconds

[Previous page](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215.md?page=1)

[Next page](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215.md?page=3)
