Reference notes for A Handful of Chips.
Tidied up for the bench. When a control loop starts hunting or a heater locks on, check the pin caps first and the chip count second.
A.B.C.bus.<dye> to read or write a bus channel; bus wiring is covered in a later chapter.voltage / 5 V, clamped to 0..1. So 2.5 V is 0.5, 1 V is 0.2, and 4 V is 0.8.0, unless a line with a plain number sets a default, such as A = 0.5.0..1 and multiplied by 5 V at the output. NaN outputs 0 V.A, B, C, and output, and standing close shows a tiny graph on the block itself.Uout and Iout.A on the right, and wire the output to a lamp supply's signal input or read it with a MultiMeter. Type A > 0.5, or !A for an inverter, check that the status turns green with 1 Cheap Chip(s) required, and click Save. Flip the input: the output switches between 5 V and 0 V and the graph moves. To test without wiring, enter A = 0.8 on line 1 and A > 0.5 on line 2.;. Define named values with name = expr. The last line produces the output. Start comments with #.A = 0.5 sets the value an input uses when nothing is wired to it.A, B, C, daytime, and all function names such as min, rs, pid, and batteryCharge must match case. Both pi and PI work.2A and 2(A+B) are invalid. Always write *, as in 2*A and 2*(A+B).1e-3 is invalid. Write decimals out in full, such as 0.001.min and max: min(A,B,C) is invalid. Nest calls instead: min(min(A,B),C).= only defines: A single = inside an expression is invalid. Use == and != to compare numbers.sqrt: Use x^0.5 or pow(x,0.5).Operators bind from tightest at the top to loosest at the bottom.
| Precedence | Operators | Notes |
|---|---|---|
| 1, tightest | func(...), ( ) |
Brackets cost 0. Use them whenever a line looks cramped. |
| 2 | ^ |
Power, right-associative: 2^3^2 means 2^(3^2). |
| 3 | -x |
Unary minus. |
| 4 | *, / |
Multiplication and division. |
| 5 | +, - |
Addition and subtraction. |
| 6 | >, <, >=, <=, ==, != |
Comparisons. Return 1 or 0. |
| 7 | ! |
Boolean NOT. Binds looser than comparisons. |
| 8 | & |
Boolean AND. Binds tighter than \|. |
| 9, loosest | \| |
Boolean OR. |
!: !A>0.5 means !(A>0.5), not (!A)>0.5.! is boolean NOT, not 1 - x: !a returns 1 when a <= 0.5 and 0 when a > 0.5. So !0.3 gives 1, which outputs 5 V. For arithmetic inversion, write 1-x.^ is exponentiation, and = only defines names or defaults.== and != treat two values as equal when they are within 0.01 of each other.&, |, !, if, rs, and integrate reset all treat any value above 0.5, over 2.5 V, as on. Logic operators return 1 or 0.if branch that was not taken.Fit chips into the lower-left Chips slot. It holds up to 64 Cheap Chips, or up to 8 Advanced Chips worth 8 Cheap Chips each. Each Cheap Chip pays for 4 operator cost points:
chips required = ceil(total operator cost / 4)
The status line reads N Cheap Chip(s) required in green when enough chips are fitted, and red when you are short. Short of chips, the output drops to 0 V. Chips are never used up.
Numbers, names, and brackets cost 0.
| Syntax | Operator Cost | Behaviour and State Rules |
|---|---|---|
+, -, *, /, -x |
1 | Basic arithmetic. |
!x |
1 | Boolean NOT. Returns 1 when x <= 0.5, else 0. |
>, <, >=, <=, ==, != |
1 | Comparisons returning 1 or 0. == and != compare within 0.01. |
&, \| |
1 | Boolean AND and OR. Inputs above 0.5 count as on; returns 1 or 0. |
abs(a) |
1 | Absolute value of a. |
min(a,b), max(a,b) |
2 | Smaller or larger of two values. |
sin(a), cos(a), asin(a), acos(a) |
2 | Trig in radians. asin and acos outside -1..1 produce NaN, which outputs 0 V. |
pow(a,b), a^b |
2 | a to the power b. |
if(c,t,e) |
2 | Returns t when c > 0.5, else e. Stateful calls inside either branch still step every sub-step. |
ramp(T) |
3 | Stateful. Rises linearly from 0 to 1 over T seconds, then repeats from 0. |
derivate(x) |
3 | Stateful. Rate of change per second. First step outputs 0. Smooth noisy signals with rc first. |
rs(set,reset) |
3 | Stateful. SR latch, set first, reset second. Both trigger above 0.5. If both are on, reset wins and outputs 0. |
rc(x,tau) |
3 | Stateful. Low-pass smoothing filter, signal x first, time constant tau in seconds second. |
integrate(x,reset) |
4 | Stateful. Adds x per second. Held at 0 while reset > 0.5. |
integrate(x,lo,hi) |
4 | Stateful. Adds x per second, clamped to lo..hi. |
scale(x,in0,in1,out0,out1) |
5 | Maps x linearly from in0..in1 onto out0..out1. Not clamped internally. |
batteryCharge(u) |
8 | Converts battery voltage ratio u = U / U_nominal into state of charge 0..1. |
pid(target,meas,P,I,D) |
12 | Stateful. PID controller on target - meas. Integral is clamped to 0..1. |
pid(target,meas,P,I,D,lo,hi) |
14 | Stateful. Same controller, with both its integral and its output clamped to lo..hi. |
The built-in variable daytime equals (ticks mod 24000) / 23999:
0 at sunrise, 0 V0.25 at noon, 1.25 V0.5 at sunset, 2.5 V0.75 at midnight, 3.75 Vdaytime>0.5 is true all night and costs 1 Cheap Chip.
Thermostat latch — 2 Cheap Chips
rs(A<0.6, A>0.8)
Wire a Thermal Probe to A and the output to your heater control. set comes first: when A falls below 0.6, 3 V, the heater turns on; when A climbs above 0.8, 4 V, the latch resets and shuts heat off. Reversing the arguments heats when hot.
Night-time yard lamp switch — 1 Cheap Chip
daytime>0.5
Outputs 5 V from sunset to sunrise and 0 V by day. Use daytime>0.48 to bring the lamps on just before dusk.
PI temperature or flow controller — 4 Cheap Chips
pid(0.7,A,4,0.5,0,0,1)
Holds sensor A at 0.7, 3.5 V, with P=4, I=0.5, D=0, and integral plus output clamped to 0..1. Suits a Pipe Control Valve or Submersible Pump.
Sensor averaging and smoothing — 1 Cheap Chip each
* Mean of three sensors: (A+B+C)/3
* Two-second low-pass filter: rc(A,2) — signal A first, time constant 2 seconds second.
2-out-of-3 redundant sensor vote — 2 Cheap Chips
* Digital inputs: A&B|A&C|B&C — & binds tighter than |, so no brackets are needed.
* Analog inputs with custom threshold: (A>0.5)+(B>0.5)+(C>0.5)>1.5
Battery-charge load shedding — 4 Cheap Chips
q = batteryCharge(0.8+0.4*A)
rs(q>0.6, q<0.3)
Wire a voltage Electrical Probe on a 48 V bank with range set to 38.4–57.6 V, or 184–276 V on a 230 V bank, into A. Naming q pays batteryCharge only once. The latch drops non-essential loads below 30% charge and brings them back above 60%.
Differential gain amplifier — 1 Cheap Chip
(A-B)*3
Clamps at the output automatically. If you feed the result into another line inside the same program, clamp it with min(1,max(0,(A-B)*3)) at 2 Cheap Chips.
Sensor window stretcher — 2 Cheap Chips
scale(A,0.2,0.8,0,1)
Stretches the 1–4 V band of input A across the full 0–5 V output range.
10-second delay timer and flasher
* Delay after release — 3 Cheap Chips:
hi = A>0.5
rs(integrate(1, hi) > 10, hi)
While A stays high, the latch is held reset at 0 V and the integrator is held at 0. Once A drops low, the integrator counts up at 1 per second and sets the output to 5 V after 10 continuous seconds.
* Square-wave flasher — 1 Cheap Chip: ramp(T)<0.5 flashes with period T seconds.
Sudden rate-of-change alarm — 2 Cheap Chips
abs(derivate(rc(A,1)))>0.05
Smooths A with a 1-second filter first, then trips to 5 V whenever A rises or falls faster than 0.05, 0.25 V, per second.