# 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:** 17
**Page:** 4

<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: [30 March 2021 20:44 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/62 "2021-03-30T20:44:07Z")

</div>

Thanks Colin you are a super star.  
one more issue.  
From the dashboard, start run button is not clearing the counter. inject CLEAR COUNTER is msg.topic = reset  
I need to run this command from the start run button

---

<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: [30 March 2021 21:02 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/63 "2021-03-30T21:02:12Z")

</div>

That is exactly the same issue as the thresholds isn't it? Set the topic in the button and send it to the revs 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: [30 March 2021 21:19 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/64 "2021-03-30T21:19:09Z")

</div>

My start button drives  
RIP-dht22  
date  
count revs  
and resets time, rpm and chart  
I added a function between start button and count revs  
line 1 msg.payload and that seems to fix the reset 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: [30 March 2021 21:22 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/65 "2021-03-30T21:22:29Z")

</div>

now i just need the start button to reset the chart  
I have a function to reset  
msg.payload = ;  
msg.topic = 0

---

<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: [31 March 2021 08:00 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/68 "2021-03-31T08:00:38Z")

</div>

`msg.payload = []`  
That should do it, is it not working?

When posting code please remember to use the same technique as when posting a flow, so the forum does not mess up the text.

---

<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: [31 March 2021 14:05 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/69 "2021-03-31T14:05:28Z")

</div>

i did not have a connection from function to chart revs. I only had a connection from function to chart.  
So now it works.

---

<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: [31 March 2021 14:44 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/70 "2021-03-31T14:44:32Z")

</div>

I now have found a problem.  
The low and high thresholds can not be count revs.  
Start the engine. now the counter is counting because the flywheel is turning making hall effects count.  
low threshold will be made before i start the run.  
I would need flywheel reading in RPM.  
Engine running, flywheel directly coupled. The flywheel is turning the same speed as the engine.  
when the engine accelerates, node red waiting for the flywheel to reach 3000 rpm to start the test.  
And finish the test when the fly wheel reaches highthreshold of 13000.

---

<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: [31 March 2021 15:10 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/71 "2021-03-31T15:10:38Z")

</div>

Is it doing what you asked for?

---

<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: [31 March 2021 15:24 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/72 "2021-03-31T15:24:30Z")

</div>

I gave this explanation in the beginning  
This is an inertia dyno spinning a fly wheel.  
Node red push a start button, have a servo to open the throttle, when the hall effects read 3000rpm start counting  
run up to 13000 rpm and stop the test. servo closes the throttle.  
record the time it takes to reach 13000 rpm.  
I would like to be able to set the Max rpm for different tests,

They way program is running will not make a test run  
the count revs is setup to read counts can we modify it to read 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: [31 March 2021 15:25 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/73 "2021-03-31T15:25:17Z")

</div>

count revs

```auto
[{"id":"db9f7109.1198a","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":390,"y":400,"wires":[["50cf5284.3f22ac","4ee39ede.8ba78"]]}]

```

---

<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: [31 March 2021 16:08 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/74 "2021-03-31T16:08:23Z")

</div>

In post 11 you said

> [@dirtydredrt](#):
>
> I would like the function to start counting at 3000 counts  
> Then run until 13000 counts. this would complete a single run of the dyno.

I said

> [@Colin](#):
>
> You said earlier that you want to run till 13000 rpm, but that is a speed, 13000 revs/min. Are you actually just counting revolutions, not rpm?

Then you said

> [@dirtydredrt](#):
>
> I just need to know how long it takes the engine to reach from 3000 counts to 13000 counts.

Several times I tried to convince you that it was the speed you wanted but you insisted it was counts of the sensor that you wanted. The code you have now does exactly what you asked for.

---

<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: [31 March 2021 16:53 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/75 "2021-03-31T16:53:28Z")

</div>

Sorry for the miscommunication  
You gave me code to capture RPM. I will try and import that code into the count revs function and get the data I need.  
Thanks you for all your help.  
Donn

---

<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: [1 April 2021 20:31 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/76 "2021-04-01T20:31:55Z")

</div>

Colin  
I have tried all day to change the count revs into RPM.  
I guess i don't have a clue how you did it.  
If you get a chance can you please take a look.  
Thanks  
Donn

```auto
[{"id":"83cd19a4.941a08","type":"function","z":"83e7ad7e.f26f7","name":"ver 2 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":760,"wires":[["f5c7a5e9.972df8"]]}]

```

---

<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: [1 April 2021 20:34 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/77 "2021-04-01T20:34:20Z")

</div>

I will understand if you don't want to.

---

<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: [1 April 2021 21:09 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/78 "2021-04-01T21:09:10Z")

</div>

Try this. Feed it with output of the RPM function.

```auto
[{"id":"9d4b9f4a.070d6","type":"function","z":"84405ff5.25fa6","name":"RPM Timing","func":"// starts timing when rpm reach low threshold\n// when high threshold is reached then passes on time difference in msec\n// 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 an rpm input\n\nswitch(msg.topic) {\n case \"reset\":\n // reset rpm\n context.set(\"lastRPM\", 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 // rmp input\n handleRPM()\n break;\n}\nreturn msg\n\nfunction handleRPM() {\n const lowThreshold = context.get(\"lowThreshold\")\n const highThreshold = context.get(\"highThreshold\")\n let complete = false\n \n lastRPM = context.get(\"lastRPM\") || 0\n thisRPM = msg.payload\n if (lastRPM < lowThreshold && thisRPM >= 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 }\n if (lastRPM < highThreshold && msg.payload >= 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 complete = true\n }\n // delete the message if we haven't finished yet\n if (!complete) {\n msg = null\n }\n // Don't save the rpm if it has gone down\n if (thisRPM > lastRPM) {\n context.set(\"lastRPM\", thisRPM)\n }\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":1980,"wires":[["a9c69d5e.91d708"]]},{"id":"272a0f46.abe8","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2000","payloadType":"num","x":170,"y":1920,"wires":[["9d4b9f4a.070d6"]]},{"id":"3bdf8b78.d7dd4c","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3000","payloadType":"num","x":170,"y":1960,"wires":[["9d4b9f4a.070d6"]]},{"id":"9dd9d8e9.299e28","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"9000","payloadType":"num","x":170,"y":2000,"wires":[["9d4b9f4a.070d6"]]},{"id":"a717ebea.471ef","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"13000","payloadType":"num","x":170,"y":2040,"wires":[["9d4b9f4a.070d6"]]},{"id":"efa3728b.06a788","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"14000","payloadType":"num","x":170,"y":2080,"wires":[["9d4b9f4a.070d6"]]},{"id":"e72b8039.f7333","type":"inject","z":"84405ff5.25fa6","name":"Reset","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"reset","x":270,"y":1760,"wires":[["9d4b9f4a.070d6"]]},{"id":"28a72db2.c11e1a","type":"inject","z":"84405ff5.25fa6","name":"Set low threshold 3000","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"lowThreshold","payload":"3000","payloadType":"num","x":200,"y":1800,"wires":[["9d4b9f4a.070d6"]]},{"id":"64fc5e63.ac8488","type":"inject","z":"84405ff5.25fa6","name":"Set high threshold 13000","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"highThreshold","payload":"13000","payloadType":"num","x":170,"y":1840,"wires":[["9d4b9f4a.070d6"]]},{"id":"a9c69d5e.91d708","type":"debug","z":"84405ff5.25fa6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":1980,"wires":[]}]

```

---

<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: [2 April 2021 14:28 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/79 "2021-04-02T14:28:35Z")

</div>

Thanks Colin  
i have it dialed in looks and functions great  
do you have a Venmo or Paypal  
[donnlange@hotmail.com](mailto:donnlange@hotmail.com)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [1 June 2021 14:28 UTC](https://discourse.nodered.org/t/small-engine-dyno-need-help-to-reset-my-counter-after-run/43215/80 "2021-06-01T14:28:36Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.

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