This page covers the basics :) of programming Lego robotics in BASIC on the Apple II series, BBC Micro, Commodore (PET, VIC-20, 64, 128), or ISA-equipped PC (IBM and clones).
Continue reading or jump to my YouTube video explaining all of this.
The best way to program Lego Interface A in BASIC is by individually controlling the decimal equivalent of each port's binary value. This method is agnostic of your BASIC variant. But first, each computer requires a bit of code to prime itself for such work. Jump ahead to the Apple, BBC, Commodore, or IBM sections.
Page 11 of the Lego 9767 (Apple II card) manual states, "The machine's BASIC software is too slow to control the interface by means of normal subroutines." THIS IS FALSE. In fact, Lego documents using Applesoft BASIC in an appendix of the Lego Lines teacher's manual (see the Archive.org link on the BrickHacks homepage). As explained on pages 4.13-4.16 of that manual, programs must begin with an initialization routine. I like to start it at line 1000 but that is just personal preference.
1000 REM Activate the Lego interface card
1010 S = 7:L = 49280 + S * 16
1020 POKE L + 3,1: POKE L + 2,63: POKE L + 1,0
1030 POKE L,0: REM CLEAR ALL PORTS
1040 RETURN
Line 1000 is a comment to remind you of the routine’s purpose: write it however you like. Line 1010 designates the slot number of the card and its memory address. Change the slot number to match your machine configuration. Don’t forgot order of operations when referring to L in your program -- multiply first, then add -- so address L will be (S*16)+49280. Line 1020 activates each chip on the card. As explained in the manual, "This sets up the 6522 registers to that bits 0-5 are output bits, and bits 6-7 are input bits. All I/O is then done through address L." Line 1030 ensures that you program begins with memory address L set to value 0. Line 1040 exits the subroutine and returns to your main program. Or you could insert the code atop your program and avoid needing a subroutine if you wish.
Now you can enter the routines that turn on ports, turn off ports, and read sensors. Lego provided these routines, but they're designed to be absracted from the hardware, because of the company focus on schoolchildren. That made perfect sense at the time. I do not like using the Lego official method, so jump to my direct method below.
Begin by telling the computer's user port to set its first six spots as outputs. The command is ?&FE62=63. I don't know if Lego offered another way to do this, because I have not yet viewed their documentation. If you have it, then please contact me. My email address is evan(at)snarc(dot)net.
Begin by telling the computer's user port to set its first six spots as outputs. The command is POKE 56579,63. You'll then POKE individual commands to the port address, which is 56577, but you can shorten that to a variable such as L. Now proceed to the direct method below.
As with the Apple version, Lego provides an official method which is good for schoolchildren but not good for adults. To use direct control instead, just set a variable for the default card address, which is 925. L=925 works fine. You can also make the address 926 in order to run two cards. To do, cut the jumper wire (labeled "adr.sw" on the card) and use a variable letter that's different from the first card. Proceed to the direct method below to start programming.
The direct method
Dan Roganti taught me this method. Here's how it works. To directly control Lego peripherals via Interface A, you need to POKE or PEEK (or whichever commands are appropriate for your computer's BASIC variant) to the location of the hardware and the decimal-encoded value of the interface ports that you would to command or listen to.
Ports 0-5, which are the outputs, have decimal values 1, 2, 4, 8, 16, and 32. Send the sum of the decimal values of the desired ports. Suppose you want to turn on all six ports. That's 1+2+4+8+16+32 = 64. So for Applesoft BASIC, type POKE L,63. BBC: ?&FE60=63. Commodore: POKE L,63. IBM: OUT L,63. Adjust the output value as needed for the port(s) you wish to activate. Replacing the value with another one disables the no-longer-needed ports, or you can output value 0 to disable all ports.
Ports 6-7, which are the inputs, have decimal values 64 and 128. Write your code to act on the values they report back to you. To determine these value, try the following. Applesoft: PEEK(L). BBC: L=?&FE60. Commodore: PEEK(L). IBM: INP(L). You can use IF-THEN statements, such as IF PEEK(L) = (0, 64, 128, or 192) THEN (do a thing).
Watch me demonstrate everything above in a video!
Home - History - Universal Buggy - Computers - Interface A - Connections - Peripherals - Code - Analog - Beyond - Lego Chevy V8
Copyright: Evan Koblentz, 2018-2024