Lesson 4. Without touching: closer than 20 cm

In pictures

After this lesson the robot notices a wall in advance: as soon as it is closer than 20 cm ahead, it does the same manoeuvre as from a bump — backs off and turns, without touching. The bumper stays as a safety net.


4.1. Idea: «closer than 20 cm» — compare the tens — CLOSE

What we want: CLOSE = 1 when the distance is less than 20 cm — that is 00…19. All these numbers have tens of 0 or 1, the rest have 2 or more. So the units can be ignored: tens < 2.

The CD4585 compares numbers — a comparator of two 4-bit numbers A and B with three outputs: LT (A < B), EQ (A = B), GT (A > B). We feed A = the counter tens CT3…CT0, B = 2 (0010: B1 = VCC, the rest GND):

Tens (A)DistanceCLOSE = LT
000…09 cm1
110…19 cm1
2…920…99 cm0

The LTIN, EQIN, GTIN pins are for a chain of several CD4585s (to compare 8, 12 bits…). We have one package, and TI says: GTIN = 1, the others — 0.

00…0910…1920…2930…3940…4950…5960…6970…7980…8990…99distance, cm — the tens:closer than 20 cm = tens 0 or 1CD4585A < B → LTA = CT3…CT0B = 0010 (2)LT = CLOSE
«Closer than 20 cm» needs no full compare: 00…19 have tens 0 or 1, the rest have 2 or more. The CD4585 compares the counter’s tens with the number 2.

Want another threshold — change B: B = 3 (0011) — 30 cm, B = 1 — 10 cm.

4.2. Idea: remember at the show step — NEAR

CLOSE looks straight at the counter, and on the PING step the counter is reset to 00 — and at that moment CLOSE = 1 («wall right here!»), which is not true. So CLOSE is latched by a D flip-flop — the second one in the CD4013 — on the SHOW step, when the count is finished and the number is right:

  • D2 = CLOSE, CL2 = SHOW → Q2 = NEAR holds until the next measurement.
step0123…9counter000816243235CLOSESHOWNEARPING and the start of the count: 00…19 —CLOSE = 1, but it is not trueSHOW: the count is done, 35 cm —the latch writes NEAR = 0: far
Why CLOSE is latched: the wall is 35 cm away, but on the PING step the counter sits at 00 and then runs through 00…19 — and all that time CLOSE shouts «close». The CD4013 flip-flop writes it only on the SHOW step, when the number is right.

It is the same trick as the display in lesson 3: show only what is finished.

4.3. Idea: a bump or close — TRIP

What we want: the manoeuvre starts if the bumper fires or it is close ahead:

HITNEARTRIP
000
101
011
111
  • TRIP = HIT OR NEAR — an OR gate of the CD4071.

And in the CD4001 (the HOLD inhibit, lesson 2) TRIP now stands instead of HIT: HOLD = NOR(NFWD, TRIP). Everything else — the manoeuvre, the wheels — stays: the «bug» simply got a second, contactless reason to turn.

Why not remove the bumper? The ultrasound looks straight ahead with a narrow beam. If the robot drives to a wall at an angle, the beam can slide along it while a body corner already touches. Then the bumper saves it.

4.4. Build

On top of lesson 3. Two new chips and a few rewirings: to rename the label on a pin — double-click the label, type the new name; for an empty pin — double-click the pin.

Step 1. Closer than 20 cm

PartPin / actionLabel / where
TI CD4585BA0
A1
A2
A3
B0
B1
B2
B3
LTIN
EQIN
GTIN
LT
CT0
CT1
CT2
CT3
GND
VCC
GND
GND
GND
GND
VCC
CLOSE

Leave unconnected: EQ and GT of the CD4585.

Step 2. Bump or close

PartPin / actionLabel / where
TI CD4071BA
B
J
C
D
E
F
G
H
HIT
NEAR
TRIP
GND
GND
GND
GND
GND
GND

Leave unconnected: K, L, M of the CD4071.

Step 3. The «close» LED

LED colour — Alt/⌘+click it until blue.

PartPin / actionLabel / where
LED, blueinputNEAR

Step 4. Rewiring

The second CD4013 flip-flop (the one that sat on GND in lesson 3) becomes the «close» latch, and the CD4001 now starts the manoeuvre not from a bump but from TRIP — a bump or «close».

PartPinDetach fromAttach to
TI CD4013B, the one whose output = ECHOSR2GNDRESET
TI CD4013B, the one whose output = ECHOSD2GNDCLOSE
TI CD4013B, the one whose output = ECHOSCL2GNDSHOW
TI CD4013B, the one whose output = ECHOSQ2— (not connected)NEAR
TI CD4001B, the one whose output = RDIRCHITTRIP

4.5. Check

  1. Switch the power on, press «Reset», switch GO on.
  2. The robot drives towards a wall, the number melts. At about 20 the blue NEAR lights, and the robot, without touching, does the same manoeuvre: back (BACK), turn (TURN), forward.
  3. After the turn, if it is farther than 20 cm ahead, NEAR goes out at the next measurement and the robot drives on.
  4. The bumper stays as a safety net: if the robot comes to a wall at an angle, the ultrasound looks past it — then the bump works, as in lesson 2. That is rare.
  5. On the paper — a path that hardly touches the walls: turns 20 cm away from them.
AheadNEAR (blue)Robot
farther than 20 cmoffdrives forward
closer than 20 cmonback, turn, forward
bumper pressed (glancing)—the same manoeuvre, by bump

Lesson 4 paste (Cmd+I)

Paste into a new project — it puts lessons 1–4 whole (that is the whole robot). The chips from robot-chips.json are needed.

# Lesson 4 — without touching: closer than 20 cm (CD4585) — the same manoeuvre as from a bump; the bumper stays as a safety net.# Paste: Cmd+I into a new project (puts lessons 1-4 whole).# ---------- power ----------power(VCC)ground(GND)# ---------- arena: paper, contour over it, obstacle ----------mpaper p{w=800, h=500}mwall w{w=800, h=500}mblock ob{w=80, h=80}at(p, 0, 0)at(w, p, 0, 0)at(ob, p, 440, 210)# ---------- robot: chassis, pen on the rear point ----------mchassis rb{spd=60}(L_ON=GO, R_ON=GO, L_DIR=LDIR, R_DIR=RDIR)at(rb, p, 80, 210)mpen pen(DOWN=VCC)mount(pen, rb.r)# ---------- GO — motors (stays to the end of the kit) ----------input(GO)# ---------- clock and reset ----------clock{rt=10}(CK)button(RESET)# ---------- bumper on the front point of the chassis ----------mbumper bp{len=110}(HIT=HIT)mount(bp, rb.f)# ---------- CD4017: manoeuvre steps ----------"TI CD4017B"(CLOCK=CK, RESET=RESET, INH=HOLD, O0=FWD, O1=B1, O2=B2, O3=B3, O4=B4, O5=T1, O6=T2, O7=T3, O8=T4)# ---------- CD4072: back = steps 1-4, turn = steps 5-8 ----------"TI CD4072B"(A=B1, B=B2, C=B3, D=B4, J=BACK, E=T1, F=T2, G=T3, H=T4, K=TURN)# ---------- CD4069: not-forward, left wheel ----------"TI CD4069UB"(A=FWD, G=NFWD, B=BACK, H=LDIR, C=GND, D=GND, E=GND, F=GND)# ---------- CD4001: right wheel, hold step 0 ----------"TI CD4001B"(A=BACK, B=TURN, J=RDIR, C=TRIP, D=NFWD, K=HOLD, E=GND, F=GND, G=GND, H=GND)# ---------- manoeuvre LEDs ----------led{color=green}(FWD)led{color=yellow}(BACK)led{color=red}(TURN)# ---------- ultrasound on the centre of the chassis ----------ustx us(TRIG=PING, ECHO=ECHO)mount(us, rb.c)# ---------- clocks for the steps and the centimetres ----------clock{rt=60}(CK60)clock{hz=1}(CK8K)# ---------- CD4017: step 0 — ping, 1 — echo, 2 — show ----------"TI CD4017B"(CLOCK=CK60, RESET=RESET, INH=GND, O0=PING, O2=SHOW)# ---------- CD4069: NOT of the 8 kHz clock, NOT full ----------"TI CD4069UB"(A=CK8K, G=NCK8K, B=FULL, H=NFULL, C=GND, D=GND, E=GND, F=GND)# ---------- CD4013: echo synced to the clock (second flip-flop — lesson 4) ----------"TI CD4013B"(S1=GND, R1=RESET, D1=ECHO, CL1=NCK8K, Q1=ECHOS, S2=GND, R2=RESET, D2=CLOSE, CL2=SHOW, Q2=NEAR)# ---------- CD4081: 99 is full ----------"TI CD4081B"(A=CT3, B=CT0, J=X9, C=CU3, D=CU0, K=Y9, E=X9, F=Y9, L=FULL, G=GND, H=GND)# ---------- CD4011: count while echo and not full ----------"TI CD4011B"(A=ECHOS, B=NFULL, J=CIU, C=GND, D=GND, E=GND, F=GND, G=GND, H=GND)# ---------- CD4029 x 2: centimetre counter (units, tens) ----------"TI CD4029B"(CLOCK=CK8K, PE=PING, CI=CIU, UD=VCC, BD=GND, J1=GND, J2=GND, J3=GND, J4=GND, Q1=CU0, Q2=CU1, Q3=CU2, Q4=CU3, CO=COU)"TI CD4029B"(CLOCK=CK8K, PE=PING, CI=COU, UD=VCC, BD=GND, J1=GND, J2=GND, J3=GND, J4=GND, Q1=CT0, Q2=CT1, Q3=CT2, Q4=CT3)# ---------- CD4029 x 2: the number for the display (loaded on the show step) ----------"TI CD4029B"(CLOCK=GND, PE=SHOW, CI=VCC, UD=GND, BD=GND, J1=CU0, J2=CU1, J3=CU2, J4=CU3, Q1=DU0, Q2=DU1, Q3=DU2, Q4=DU3)"TI CD4029B"(CLOCK=GND, PE=SHOW, CI=VCC, UD=GND, BD=GND, J1=CT0, J2=CT1, J3=CT2, J4=CT3, Q1=DT0, Q2=DT1, Q3=DT2, Q4=DT3)# ---------- display: tens DT3...DT0, units DU3...DU0 ----------display{base=HEX, bits=8}(DT3, DT2, DT1, DT0, DU3, DU2, DU1, DU0)# ---------- CD4585: closer than 20 cm ----------"TI CD4585B"(A0=CT0, A1=CT1, A2=CT2, A3=CT3, B0=GND, B1=VCC, B2=GND, B3=GND, LTIN=GND, EQIN=GND, GTIN=VCC, LT=CLOSE)# ---------- CD4071: bump or close ----------"TI CD4071B"(A=HIT, B=NEAR, J=TRIP, C=GND, D=GND, E=GND, F=GND, G=GND, H=GND)# ---------- the close LED ----------led{color=blue}(NEAR)

Stuck? Something doesn't work →