American Dream Code

1st step - remove all the shit.

2nd step -

low will have a minus

high will have a plus

  • That sensor has a 1 to 25 PSI but

Dec 13, Cody

To be more specific, it needs to detect when the average value of the loop has a variation of under 1 and update a new “average pressure” variable. Then the other variables need to be changed to avgpressure + x for activation and avgpressure - z for deactivation

Prev Cody

Super messy and miss labeled, sorry about that. I think going through commenting each line, then assigning the important variables to the top is a good first step

Cody said we’re sending power in through the USB of the Arduino which you’re not meant to do bc it becomes deregulated.

image
image
image

Log of atmospheric pressure changes across time

Dec 13 - 4:30pm: 1011 and 1012

image

if Raw hPa jumps more than 5 values suddenly - then we’re pumping.

But can’t activate the pump immediately is pressure jumps one time - it needs to be a number of times.

Actually what needs to happen is from the moment there’s a spike, then either start the timer or start adding to a counter.

How many pumps does it take for the balloon to be full? vs how many do we see in the code.

Number of pumps, lightly done for it to be full + what values do we see.

Variables that now have changed between long and short

sampleInterval = 100;

windowSizeShort (10secs) vs windowSizeLong (60seconds);

float pressureBufferShort[windowSizeShort]; int bufferIndexShort = 0; float runningSumShort = 0;

float pressureBufferLong[windowSizeLong]; int bufferIndexLong = 0; float runningSumLong = 0;

American dream in china, russia, britain,

On point 4 - "If someone pumps for a long time, your “ambient” will drift toward the pumped pressure because avgLong includes those samples. That might be fine; just know that your thresholds are tied to it." How can I then keep this from happening? 5) Two “turn pump on” paths can fight each other (logic overlap) - this is the goal, as a fail safe.

To stop Ambient (= avgLong) from “getting dragged” by sustained pumping, you need one idea:

Don’t feed “pumping samples” into the long buffer (or feed them with much lower weight).

You can do that with very small changes — no new state machines, no fancy failsafes. Here are three clean options, from simplest to most robust.

Option A (simplest): Freeze the LONG buffer while pumping

If pumpingNow is true, you still update the short buffer (so detection remains responsive), but you skip updating the long buffer. That keeps ambient stable.

Key detail: pumpingNow currently depends on avgLong, so compute pumpingNow using the previous avgLong, then decide whether to update the long buffer this cycle.

What it looks like in your loop

  1. Update short buffer → compute avgShort
  2. Compute avgLongPrev = runningSumLong / windowSizeLong
  3. Compute pumpingNow = (avgShort - avgLongPrev) >= pumpStartDelta
  4. If NOT pumping, update long buffer and recompute avgLong; else keep it unchanged
  5. Set ambientPressure = avgLong

Minimal code pattern:

Pros: dead simple, prevents drift completely.

Cons: if someone pumps for 10 minutes, ambient is “frozen” for 10 minutes (atmospheric pressure won’t change meaningfully in 10 minutes, so usually fine).

Option B (still simple): Clamp long updates to “near ambient”

Instead of freezing, you allow long buffer updates only when the raw reading is close-ish to the existing ambient (or avgLongPrev). That lets atmospheric drift through, but rejects pumping spikes.

Example gate:

float gateBand = 3.0; // hPa
bool allowLongUpdate = fabs(pressure_hPa - avgLongPrev) <= gateBand;

if (allowLongUpdate) { update long buffer }

Pros: ambient can keep slowly drifting even during mild activity.

Cons: if pumping shifts pressure by only a few hPa, you might still contaminate; if the gate is too tight you’ll effectively freeze anyway.

Option C (most robust, still not too complex): Keep long buffer updating, but “winsorize” input

This means: when pressure is wild, you cap what you feed the long buffer:

float capped = pressure_hPa;
float maxStep = 2.0; // hPa
if (capped > avgLongPrev + maxStep) capped = avgLongPrev + maxStep;
if (capped < avgLongPrev - maxStep) capped = avgLongPrev - maxStep;

// feed capped into long buffer

Pros: ambient never freezes; pumping can’t drag it fast.

Cons: slightly more math; you must pick maxStep.

What I’d pick for your goals

Given your stated preference (remove complication) + running for hours:

  • Start with Option A (freeze long buffer while pumping).
  • If you later see “ambient feels stale during long pumping sessions,” switch to Option C.

Atmospheric pressure changes over hours, not seconds; freezing for the duration of “active pumping” is usually perceptually invisible but functionally stabilizing.

About point 5 (“two turn-on paths as fail safe”)

That’s fine. If you want both:

  • Keep hysteresis as the “physics-based control”
  • Keep time-based as the “guarantee after sustained pumping”
  • Just be aware that when you freeze long/ambient during pumping, your thresholds won’t slide under you — which actually makes the two-path safety strategy more predictable.

Three prompts to continue

  1. “Patch my exact code to implement Option A (freeze long buffer while pumping) with the smallest possible diff.”
  2. “Show me the exact diff-style changes for Option B (gate long updates by a band around avgLongPrev).”
  3. “Which values should I use for gateBand or maxStep given my sensor noise, and what should I log to tune it in 2 minutes?”

Previous pumpingNow

// test 2 pumps in a 35 sec window doesnt kick in

Key changes:

  • Time based logic is now if someone hits the pump for X amount of times
    • Which you can change to be 1
  • 30 seconds is way too little, where it’s almost impossible for anyone to activate the pump the other way.
  • 35 seconds
  • It can detect really well if someone is pumping or not, even just after pump has kicked in. Code currently doesn’t register that someone is pumping while it’s suction pump is on. increasing the low threshold can have the same effect.

AmbientPressure is the result of a circular buffer running for 3 minutes that already excludes any hPa samples from when the pump is running.

For the timed logic, I made it a little more robust to avoid someone just pumping once and walking away and the suction still kicking in. If in a space of windowDurationMs (aka 35 seconds), if the pump has been hit 4x counting from the first one, then suction pump will be turned on. Both the 35 secs and the 4x can be changed - to 1x for example.

about code

  • We’re not updating atmospheric pressure when pump is on.

Do we agree on this - suction can only be ON if measured pressure is above ambient pressure?

image

I want “ambient that drifts slowly over ~30 minutes” with minimal code and memory: Option B (EMA).

Take 32 readings;

take the 8th smallest moment

Take away the bottom 25% hypothetically &

don’t update it when the pump is on.

Taking the one reading above

A median of the 25%.

he’s saying that I take a lower value from every minute and then add it to a larger buffer that counts towards the hour.

Connecting Arduino

Power from brown cable was on!

Post 4am Michael call

6) Your pumping detector can generate fake “hits” because it’s tied to pump state

You define:

pumpingNow = (!suctionOn) && ((pressure_hPa - ambientPressure) >= pumpStartDelta);

So whenever suction turns ON, pumpingNow is forced false. When suction turns OFF, it can become true again immediately, creating rising edges that are caused by your pump toggling, not by a person pumping.

That can inflate pumpHits and cause self-triggering.

If you want “user pumping” events, base it on input signal behavior, not on the actuator state, or add a guard like:

  • Only count rising edges if pump has been OFF for at least N seconds
  • Or count events from avgShort - ambient rather than raw pressure_hPa - ambient
  • Or freeze the hit window entirely while suction is ON

Pump kicked in after

Raw hPa: 989.65 | AvgShort(10s) hPa: 990.07 | AvgLong(60s) or ambientpressure: 1007.25 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 969.47 | AvgShort(10s) hPa: 989.89 | AvgLong(60s) or ambientpressure: 1007.22 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 985.70 | AvgShort(10s) hPa: 989.64 | AvgLong(60s) or ambientpressure: 1007.21 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 977.72 | AvgShort(10s) hPa: 989.57 | AvgLong(60s) or ambientpressure: 1007.19 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 975.05 | AvgShort(10s) hPa: 988.99 | AvgLong(60s) or ambientpressure: 1007.17 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 983.13 | AvgShort(10s) hPa: 988.81 | AvgLong(60s) or ambientpressure: 1007.16 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 970.67 | AvgShort(10s) hPa: 988.16 | AvgLong(60s) or ambientpressure: 1007.14 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 984.52 | AvgShort(10s) hPa: 987.92 | AvgLong(60s) or ambientpressure: 1007.12 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 965.15 | AvgShort(10s) hPa: 987.27 | AvgLong(60s) or ambientpressure: 1007.09 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 987.05 | AvgShort(10s) hPa: 987.02 | AvgLong(60s) or ambientpressure: 1007.06 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 959.52 | AvgShort(10s) hPa: 986.39 | AvgLong(60s) or ambientpressure: 1007.02 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 990.34 | AvgShort(10s) hPa: 986.17 | AvgLong(60s) or ambientpressure: 1007.01 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 948.64 | AvgShort(10s) hPa: 985.46 | AvgLong(60s) or ambientpressure: 1006.97 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 993.19 | AvgShort(10s) hPa: 985.28 | AvgLong(60s) or ambientpressure: 1006.96 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 946.89 | AvgShort(10s) hPa: 984.59 | AvgLong(60s) or ambientpressure: 1006.93 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 991.53 | AvgShort(10s) hPa: 984.40 | AvgLong(60s) or ambientpressure: 1006.92 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 967.91 | AvgShort(10s) hPa: 983.92 | AvgLong(60s) or ambientpressure: 1006.89 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 971.52 | AvgShort(10s) hPa: 983.52 | AvgLong(60s) or ambientpressure: 1006.87 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 985.70 | AvgShort(10s) hPa: 983.22 | AvgLong(60s) or ambientpressure: 1006.84 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 960.28 | AvgShort(10s) hPa: 982.71 | AvgLong(60s) or ambientpressure: 1006.80 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 988.80 | AvgShort(10s) hPa: 982.43 | AvgLong(60s) or ambientpressure: 1006.78 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 950.74 | AvgShort(10s) hPa: 981.82 | AvgLong(60s) or ambientpressure: 1006.75 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 993.99 | AvgShort(10s) hPa: 981.58 | AvgLong(60s) or ambientpressure: 1006.74 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 944.90 | AvgShort(10s) hPa: 980.91 | AvgLong(60s) or ambientpressure: 1006.70 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 992.26 | AvgShort(10s) hPa: 980.65 | AvgLong(60s) or ambientpressure: 1006.69 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 948.47 | AvgShort(10s) hPa: 980.03 | AvgLong(60s) or ambientpressure: 1006.66 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 991.79 | AvgShort(10s) hPa: 979.77 | AvgLong(60s) or ambientpressure: 1006.64 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH Raw hPa: 962.28 | AvgShort(10s) hPa: 979.29 | AvgLong(60s) or ambientpressure: 1006.60 | PumpingNow: N | windowActive: N | pumpHits: 0 | windowAgeMs: 0 | PumpPin: HIGH

5:30am sun

AllowSuction logic has to go.

image

Decent working code.

Even more decent and more working