This program plots a square wave graph by clipping a sine wave to produce a flat-topped waveform, simulating the effect of a limiter or saturation circuit. It sets up a black-background display with ink colours for axes and labels, draws coordinate axes using PLOT and DRAW, then iterates 401 steps computing volts = 2.5 + 10·SIN(x/40) and clamping the result to the range 0–5 V. Each sample is plotted pixel-by-pixel using PLOT with INK 7, and the current voltage and time values are continuously printed to the screen during the loop. A DIM a$(9) string is used as a blank-field trick to overwrite trailing digits left by PRINT when numeric values change length.
Program Analysis
Program Structure
The program is divided into clearly labelled sections using REM comments. Lines 10–12 are the title block, lines 100–150 handle display setup, and lines 200–360 contain the main plotting loop. The structure is straightforward and linear, with no subroutines or branching beyond the two IF statements used for clamping.
- Lines 110–150: Screen setup — colours, axes, title, and string buffer declaration
- Lines 210–360: Main FOR loop from
x=0to400, computing, clamping, plotting, and printing each sample - Line 370: STOP
Waveform Generation
The voltage is computed at line 230 as volts = 2.5 + 10*SIN(x/40), producing a full sine wave centred at 2.5 V with an amplitude of 10, which naturally exceeds the 0–5 V display range. Lines 240 and 250 clamp the result: any value above 5 is set to 5, and any value below 0 is set to 0. This hard clipping transforms the sine into a square wave — the flat tops and bottoms represent the saturated regions, while the near-vertical transitions remain from the sine’s steep zero-crossings.
Graphics and Coordinate Mapping
Axes are drawn in INK 3 (yellow) using PLOT 7,151: DRAW 0,-120: DRAW 247,0, creating a vertical axis at x-pixel 7 and a horizontal axis at y-pixel 31. Each plotted sample maps the loop variable x to a screen x-coordinate via 8+(24*x/40) and the clamped voltage to a y-coordinate via 32+volts*24. With x ranging 0–400 and divided by 40, the effective time axis spans 0–10 units across 240 pixels. The voltage scale maps 0–5 V across 120 pixels (24 pixels per volt), consistent with the axis tick labels printed at lines of every 3 character rows in line 120.
Voltage Axis Labels
Line 120 prints the Y-axis labels using FOR x=1 TO 6: PRINT AT x*3,0;6-x: NEXT x, placing values 5, 4, 3, 2, 1, 0 at rows 3, 6, 9, 12, 15, 18 respectively. This reuses x as a loop variable before the main plotting loop claims it — there is no conflict since setup is complete before line 210.
Blank-Field Overwrite Idiom
Line 150 declares DIM a$(9), creating a 9-character string filled with spaces. This string is appended to the PRINT statements at lines 330 and 340, which display volts and x/40 respectively. Because numeric values printed by BASIC can vary in character width (e.g. “5” vs “4.986…”), printing a trailing block of spaces ensures any digits left over from a previously longer value are erased. This is a well-known BASIC display-hygiene technique.
Key Variables
| Variable | Purpose |
|---|---|
x | Loop counter (0–400); also reused for Y-axis label loop |
volts | Computed and clamped signal value (0–5) |
a$ | 9-space string used to blank trailing print fields |
Notable Techniques
- Hard clipping of a sine wave via sequential IF comparisons to simulate a square wave without any dedicated square-wave formula
- Inline PLOT attribute override using
PLOT INK 7;to draw the waveform in white while axes remain in a different ink - Reuse of loop variable
xfor both axis labelling and the main plot loop, relying on sequential execution order - Live on-screen numeric readouts updated every iteration, giving an oscilloscope-style display during plotting
Potential Anomalies
The loop runs 401 iterations (x=0 TO 400), and the x-pixel formula 8+(24*x/40) reaches a maximum of 8+240 = 248 at x=400. The screen is 256 pixels wide (0–255), so the rightmost plotted point falls within bounds. However, the horizontal axis drawn with DRAW 247,0 from pixel 7 ends at pixel 254, giving a slightly asymmetric margin at the right edge compared with the left. This is a minor cosmetic inconsistency rather than a functional bug.
Content
Source Code
10 REM Square wave graph
11 REM plotting program
12 REM
100 REM Set up
101 REM
110 PAPER 0: INK 6: BORDER 0: CLS
120 PRINT "VOLTS = ": FOR x=1 TO 6: PRINT AT x*3,0;6-x: NEXT x
130 INK 3: PLOT 7,151: DRAW 0,-120: DRAW 247,0: INK 6
140 PRINT AT 1,17;"Square Wave"
150 DIM a$(9)
200 REM
201 REM Plotting Loop
202 REM
210 FOR x=0 TO 400
211 REM
220 REM Calculate volts
221 REM
230 LET volts=2.5+10*SIN (x/40)
240 IF volts>5 THEN LET volts=5
250 IF volts<0 THEN LET volts=0
300 REM
301 REM Plot values
302 REM
310 PLOT INK 7;8+(24*x/40),32+volts*24
320 REM
321 REM Print values
322 REM
330 PRINT AT 0,7;volts;a$
340 PRINT AT 21,15;x/40;a$
350 REM
351 REM End loop
352 REM
360 NEXT x
370 STOP
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
