Silicon ChipMake it with Micromite - April 2020 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Subscriptions: PE Subscription
  4. Publisher's Letter
  5. Feature: NEWS
  6. Feature: Techno Talk by Mark Nelson
  7. Feature: Net Work by Alan Winstanley
  8. Project: Ultra-low-distortion Preamplifier with Tone Controls by John Clarke
  9. Project: iCEstick by Tim Blythman
  10. Back Issues: BACK ISSUES ON CD-ROMS by Jim Rowe
  11. Project: FLIP DOT DISPLAY by TIM BLYTHMAN
  12. Feature: Circuit Surgery by IAN BELL
  13. Feature: Practically Speaking
  14. Feature: Make it with Micromite by Phil Boyce
  15. Feature: AUDIO OUT by Jake Rothman
  16. Feature: Visual programming with XOD by Julian Edgar
  17. Feature: Max’s Cool Beans by Max the Magnificent
  18. PCB Order Form
  19. Advertising Index

This is only a preview of the April 2020 issue of Practical Electronics.

You can view 0 of the 80 pages in the full issue.

Articles in this series:
  • (April 2020)
  • (April 2020)
  • Techno Talk (May 2020)
  • Techno Talk (May 2020)
  • Techno Talk (June 2020)
  • Techno Talk (June 2020)
  • Techno Talk (July 2020)
  • Techno Talk (July 2020)
  • Techno Talk (August 2020)
  • Techno Talk (August 2020)
  • Techno Talk (September 2020)
  • Techno Talk (September 2020)
  • Techno Talk (October 2020)
  • Techno Talk (October 2020)
  • (November 2020)
  • (November 2020)
  • Techno Talk (December 2020)
  • Techno Talk (December 2020)
  • Techno Talk (January 2021)
  • Techno Talk (January 2021)
  • Techno Talk (February 2021)
  • Techno Talk (February 2021)
  • Techno Talk (March 2021)
  • Techno Talk (March 2021)
  • Techno Talk (April 2021)
  • Techno Talk (April 2021)
  • Techno Talk (May 2021)
  • Techno Talk (May 2021)
  • Techno Talk (June 2021)
  • Techno Talk (June 2021)
  • Techno Talk (July 2021)
  • Techno Talk (July 2021)
  • Techno Talk (August 2021)
  • Techno Talk (August 2021)
  • Techno Talk (September 2021)
  • Techno Talk (September 2021)
  • Techno Talk (October 2021)
  • Techno Talk (October 2021)
  • Techno Talk (November 2021)
  • Techno Talk (November 2021)
  • Techno Talk (December 2021)
  • Techno Talk (December 2021)
  • Communing with nature (January 2022)
  • Communing with nature (January 2022)
  • Should we be worried? (February 2022)
  • Should we be worried? (February 2022)
  • How resilient is your lifeline? (March 2022)
  • How resilient is your lifeline? (March 2022)
  • Go eco, get ethical! (April 2022)
  • Go eco, get ethical! (April 2022)
  • From nano to bio (May 2022)
  • From nano to bio (May 2022)
  • Positivity follows the gloom (June 2022)
  • Positivity follows the gloom (June 2022)
  • Mixed menu (July 2022)
  • Mixed menu (July 2022)
  • Time for a total rethink? (August 2022)
  • Time for a total rethink? (August 2022)
  • What’s in a name? (September 2022)
  • What’s in a name? (September 2022)
  • Forget leaves on the line! (October 2022)
  • Forget leaves on the line! (October 2022)
  • Giant Boost for Batteries (December 2022)
  • Giant Boost for Batteries (December 2022)
  • Raudive Voices Revisited (January 2023)
  • Raudive Voices Revisited (January 2023)
  • A thousand words (February 2023)
  • A thousand words (February 2023)
  • It’s handover time (March 2023)
  • It’s handover time (March 2023)
  • AI, Robots, Horticulture and Agriculture (April 2023)
  • AI, Robots, Horticulture and Agriculture (April 2023)
  • Prophecy can be perplexing (May 2023)
  • Prophecy can be perplexing (May 2023)
  • Technology comes in different shapes and sizes (June 2023)
  • Technology comes in different shapes and sizes (June 2023)
  • AI and robots – what could possibly go wrong? (July 2023)
  • AI and robots – what could possibly go wrong? (July 2023)
  • How long until we’re all out of work? (August 2023)
  • How long until we’re all out of work? (August 2023)
  • We both have truths, are mine the same as yours? (September 2023)
  • We both have truths, are mine the same as yours? (September 2023)
  • Holy Spheres, Batman! (October 2023)
  • Holy Spheres, Batman! (October 2023)
  • Where’s my pneumatic car? (November 2023)
  • Where’s my pneumatic car? (November 2023)
  • Good grief! (December 2023)
  • Good grief! (December 2023)
  • Cheeky chiplets (January 2024)
  • Cheeky chiplets (January 2024)
  • Cheeky chiplets (February 2024)
  • Cheeky chiplets (February 2024)
  • The Wibbly-Wobbly World of Quantum (March 2024)
  • The Wibbly-Wobbly World of Quantum (March 2024)
  • Techno Talk - Wait! What? Really? (April 2024)
  • Techno Talk - Wait! What? Really? (April 2024)
  • Techno Talk - One step closer to a dystopian abyss? (May 2024)
  • Techno Talk - One step closer to a dystopian abyss? (May 2024)
  • Techno Talk - Program that! (June 2024)
  • Techno Talk - Program that! (June 2024)
  • Techno Talk (July 2024)
  • Techno Talk (July 2024)
  • Techno Talk - That makes so much sense! (August 2024)
  • Techno Talk - That makes so much sense! (August 2024)
  • Techno Talk - I don’t want to be a Norbert... (September 2024)
  • Techno Talk - I don’t want to be a Norbert... (September 2024)
  • Techno Talk - Sticking the landing (October 2024)
  • Techno Talk - Sticking the landing (October 2024)
  • Techno Talk (November 2024)
  • Techno Talk (November 2024)
  • Techno Talk (December 2024)
  • Techno Talk (December 2024)
  • Techno Talk (January 2025)
  • Techno Talk (January 2025)
  • Techno Talk (February 2025)
  • Techno Talk (February 2025)
  • Techno Talk (March 2025)
  • Techno Talk (March 2025)
  • Techno Talk (April 2025)
  • Techno Talk (April 2025)
  • Techno Talk (May 2025)
  • Techno Talk (May 2025)
  • Techno Talk (June 2025)
  • Techno Talk (June 2025)
  • Techno Talk (July 2025)
  • Techno Talk (July 2025)
Make it with Micromite Phil Boyce – hands on with the mighty PIC-powered, BASIC microcontroller Part 15: Having fun with a colour touchscreen Fig.1. The 2.8-inch touchscreen used this month lets you create impressive interactive user interfaces. I n a previous column we showed how to connect a tiny 0.96-inch colour IPS screen to the Micromite (see Part 8 in PE, September 2019). We used it to build a clock, and also to demonstrate some of MMBASIC’s graphical commands (Part 9). With a pixel resolution of 160×80, the IPS screen is the perfect add-on for any project that needs to display simple information such as the time, date, or temperature. However, its small physical size does mean that it is rather limited in what can be displayed at any one time. So this month we are going to show you how to implement a bigger TFT (LCD) screen that not only has a higher pixel resolution (320×240), it also has the option of a resistive touch-panel. This bigger screen will allow you to display much more information, and thereby enable you to create some interesting and useful user interfaces (see Fig.1). We start this month by building the required TFT adaptor. This comprises just four connectors, some wire links, a resistor, a buzzer (optional), and four support pillars (see Fig.2). You’ll then insert an ILI9341 2.8-inch TFT display module (with, or without, touch). Once assembled, you can plug the TFT-adaptor directly into your MKC, or better still, plug it into the Bluetooth adaptor board that we built last month. Once a couple of options Micromite code The code in this article is available for download from the PE website. 52 have been set, and the touchscreen has been quickly calibrated, it will all be ready for use. We will work through several user interface examples so that you can be inspired to develop some ideas for your own projects. We will end this month by downloading the classic board game, MasterMind. It’s a great demonstration of how you can use this bigger screen, although we won’t use the touch capability in the game. Instead, in the version presented here you will play against the Micromite with an infrared (IR) remote control. The aim of the game is to use logic and skill to solve the secret colour-code as quickly as possible, in no more than ten guesses. If you’ve never played MasterMind before, you will find it a fun, challenging and addictive game! buzzer (LS1) can be added for audible feedback – it can be used as a ‘click’ effect when the screen is ‘touched’, or alternatively as a piezo buzzer to make sounds at various frequencies. One side of the buzzer is connected to a PWM output (pin 26), and the other to a general purpose I/O (pin 22) which effectively acts as an enable pin (refer back to Part 7 in PE, August 2019, for details of this technique). The stripboard layout is shown in Fig.5. Even though there are only a few items to assemble, the stripboard is large enough to securely mount the TFT using four Building the TFT adaptor The TFT adaptor is a straightforward circuit comprising just a handful of components assembled onto stripboard; see the schematic in Fig.3. Three pin-strips (J1-J3) allow the adaptor to be plugged into the MKC and hence connect to the relevant pins on the Micromite. These also route the MKC’s 5V power-output pins directly to the TFT screen, which is plugged into the 14-way socket (J4). Built into the TFT screen itself is an LED backlight which ultimately controls the overall brightness of the screen. In this design, the screen’s brightness is fixed by the 10Ω resistor (R1) to provide a decent brightness level. The optional Fig.2. The TFT adaptor used to connect the touchscreen to the MKC. Practical Electronics | April | 2020 Next, mount the 14-way socket (J4), and finally solder the buzzer (LS1) into position. Perform the usual visual checks for shorts between neighbouring tracks, and when you’re happy everything looks correct, use four 6mm M3 screws to fix the mounting pillars leaving the top hole ‘free’ in each pillar – see Fig.4a. Next, carefully insert the TFT screen into the 14-way socket – ensure you align the pins correctly. For now, do not screw the screen into position, just let it rest on the four mounting pillars. Finally, you need to carefully insert the TFT adaptor into your MKC (or Bluetooth adaptor and MKC). 26 T _ IR Q 15 T _ D O 14 L S1 P iezo T _ D I 3 bu ze r T _ C S 7 22 T _ C L K SD O (M ISO) L E D 26 SC K 25 R 1 10Ω 2 22 25 SD I (M OSI) 23 D /C 23 J1 R E SE T 6 C S 0V 5V IM PORTANT! In J1 , J2 , J3 r em o ve p ins hig hl ig hted in r ed Before the screen will function properly, there are two configuration options that need to be set. However, you may 5V V CC J4 Configuring and testing 15 G N D J2 T F T 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 7 6 0V 14 J3 Fig.3. Schematic for the TFT adaptor – the only components required are a single resistor and an optional buzzer. nylon mounting pillars (M3, 12mm). The positions marked for the mounting pillars are for use with the 2.8-inch version of the screen. You could use the smaller 2.4-inch version, but you will need to relocate the two pillars shown at holes C4 and C21 (and omit the pillars in holes GG4 and GG21). Begin by cutting out the required size stripboard (24 tracks by 37 holes). Mark the 16 required track cuts, check them (at least twice!), and then manually cut them with a track-cutting tool, or a drill bit. Mark out the mounting pillar holes and use a 3mm drill bit to make the holes. Next, install the 15 wire links, followed by resistor R1. It cannot be stressed enough that you check the positioning of items at least twice before soldering them. J1-J3 need to be prepared prior to installation – see Fig.4c. To do this, remove all unused pins and modify the remainder, which are the downward-facing pins that plug into the MKC. (a) (b) (c) Fig.4. (a) one of the four mounting pillars used to support the TFT module; (b) nylon threaded stand off (12mm, M3) with 6mm M3 screw; and (c) the three modified jumpers that connect the TFT adaptor to the MKC. Practical Electronics | April | 2020 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA BB CC DD EE FF GG HH II JJ KK LS1 J1 J2 J3 R1 J4 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA BB CC DD EE FF GG HH II JJ KK 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 KK JJ II HH GG FF EE DD CC BB AA Z Y X W V U T S R Q P O N M L K J I H G F E D C B A KK JJ II HH GG FF EE DD CC BB AA Z Y X W V U T S R Q P O N M L K J I H G F E D C B A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Fig.5. Stripboard layout of the parts used for the TFT adaptor. Note: blue means downward-facing pins – see Fig.4. 53 Fig.6. Typing the command GUI TEST LCDPANEL should result in the display of this animated test-pattern. Fig.7. The touchscreen is calibrated with the command GUI CALIBRATE. Use a stylus (eg, a pen lid) and follow the onscreen instructions; ensure you accurately touch the centre of each ‘target’. still have the IPS screen driver installed, in which case it will need to be removed first (remember that the Micromite can have no more than one external TFT display attached). Now connect your MKC/TFT to your computer either via the DM/ USB lead, or via the Bluetooth link. Launch your Terminal application (eg, TeraTerm), and connect to the correct COM port and at the correct speed. If all is well, you’ll see the usual Micromite welcome message (or see your auto-running program, in which case simply use Ctrl-C to stop it). If you do not see the command prompt, then unplug everything from your computer, and check that the adaptor boards are indeed inserted correctly. Once you see the command prompt, you are in a position to remove the IPS driver. Simply type OPTION LCDPANEL DISABLE followed by LIBRARY DELETE (you can do this even if you’re not sure you have the IPS driver installed). It is then advisable to reset the MKC before moving on (remove power, then reapply after a couple of seconds). You will probably see the TFT’s LED backlight on (with the screen looking like it has a whitish/grey appearance). If not, then it is worth checking that 5V is getting to the TFT’s LED pin. When you can see the LED backlight on, you can proceed with setting the Micromite to use the built-in screen Fig.9. Check that the basic graphical commands (as listed in the text) display on the TFT screen as expected. Try changing the parameters to experiment with the appearance. driver (which this particular screen uses), along with setting up the ‘Touch’ feature (if you have the touch-panel version of the screen). First, to set the Micromite to use the built-in screen driver, type: OPTION LCDPANEL ILI9341,P,2,23,6 On pressing Enter, the TFT should go blank (it no longer looks whitish/grey). In the above configuration setting, the P sets portrait mode (as opposed to landscape), and the numbers 2,23,6 represent the three Micromite pin numbers used to connect to control signals on the TFT module. Now type GUI TEST LCDPANEL and you’ll see the usual built-in animated test pattern of circles, as shown in Fig.6. If you see this, then congratulations, you’re almost done. If not, check for correct insertion of the adaptor(s), and that there are no shorts between tracks on the TFT adaptor. If you’re still not seeing anything, then type OPTION LIST to check that the pin numbers shown for OPTION LCDPANEL are correct. If not, then type OPTION LCDPANEL DISABLE to remove it, and then repeat the above OPTION again, this time with the correct pin numbers. For those that have the touch-panel feature included with the TFT module, you will now need to set it up by typing: OPTION TOUCH 7,15 Fig.8. Use the command GUI TEST TOUCH to test that calibration has been performed accurately. A good test is writing a few simple words (slowly, and with the aid of a stylus). 54 This tells the Micromite which pins are connect to the touchpanel control signals. You can check that you entered it correctly by immediately typing OPTION LIST. If you make a error, type OPTION TOUCH DISABLE before correcting any typing mistake. Before you can use touch, you have to perform a calibration. To do this, type: GUI CALIBRATE and follow the instructions that appear on the TFT screen. You will see four cross-hairs appear; one at a time (see Fig.7). Using a blunt, pointed implement (plastic stylus), touch over the central point of each ‘target’, and press down until the target disappears. Be careful not to touch any other parts of the touch-screen when touching the cross-hair. After the fourth ‘target’, you Practical Electronics | April | 2020 will see a message (in your terminal app) stating whether or not calibration was successful. If you see, Done. No errors then all is done. If you see, Warning. Inaccurate calibration then repeat GUI CALIBRATE, but this time take extra care to touch the centre of each target. The next step is to test that the touchpanel works accurately – this is vital, especially when it comes to any touch interface that relies on a user touching ‘buttons’ placed on the TFT screen. To test the accuracy of the touch-panel simply type: GUI TEST TOUCH and the screen will turn blank. Now, use a stylus to draw on the screen and check that the pixels appear at the point of contact with the screen. A good test is to slowly write some words and check that they are recognisable (see Fig.8). Press any key to end the test. If there is ‘drift’ between where the stylus is touching the screen, Fig.10. The result of the eleven-line Fig.11. Using two touch-buttons to and where the pixels illuminate, then ‘clock’ program listed in the text. If you control external hardware. Here a re-run the calibration process. have installed the optional buzzer then physical (red) LED on I/O pin 17 is being The final thing to test is the buzzer, you will also hear a ticking sound. controlled, along with a virtual (green) which is connected between pins 22 and LED on the screen. 26. To do this test we will set the PWM output on Pin 26 as a 1kHz square wave, and set pin 22 as a low output to act as 0V. Now type two lines of code: PWM 2,2000,50 PWM 2,1000,50 BOX 0,0,240,320,10,RGB(GREEN),RGB(BLUE) SETPIN 22,DOUT DO TEXT 120,160,TIME$,CM,1,3,RGB(WHITE),RGB(BLUE) On pressing Enter after the second line, you should hear the IF oTime$<>TIME$ THEN piezo buzzer sound. If not, then you will need to check the oTime$=Time$ soldering between the piezo buzzer and pins 22 and 26. To SETPIN(22),DOUT stop the buzzer sounding type SETPIN 22,OFF (or PWM 2, PAUSE 10 STOP or EDIT). SETPIN(22),OFF END IF LOOP Graphical commands Now that you have built and successfully tested the touchscreen On running the code, you should see the time displayed on the adaptor, we can start to use some of MMBASICs graphical screen in the format shown in Fig.10, along with an audible commands with this bigger screen. Back in Part 9 (PE, October ‘tick’ each second. If you don’t see or hear this, then check 2019), we used some of the graphical commands on the IPS the code has been entered correctly. Note, if you do not have screen. All of these are still valid, but we now have more pixels an RTC connected, then you will first need to set the ‘clock’ to play with. Let’s begin by seeing those previous commands by typing the command: TIME$="hh:mm:ss" (replacing the in action. Enter the following, one line at a time, and check relevant values for hh (hours), mm (minutes), and ss (seconds); that you end up with a screen like that shown in Fig.9: eg, TIME$="14:27:00" As a challenge, try converting the IPS Clock code (Part 9) CLS RGB(red) to display the date on this bigger screen. CLS RGB(0,255,128) TEXT 120,160,"Hello!",cm,1,2,rgb(green) BOX 80,200,100,50,3,rgb(magenta),rgb(blue) Using touch LINE 10,10,230,60,1,RGB(red) Having just had a brief look at how to use the touchscreen CIRCLE 30,50,20,5,2,RGB(black),RGB(yellow) for visual output, let’s now explore how to use the touch RBOX 10,260,50,50,1,RGB(red),RGB(white) capability. As usual, MMBASIC makes this easy. We will keep text 35,285,"Exit",cm,1,1,rgb(black),rgb(white) things simple by working step-by-step through the following PIXEL 130,225,RGB(black) ‘how to’ topics:  Read the x and y coordinates of the screen position being Now try adjusting some of the parameters to ensure that you touched understand how to use each command. It is worth referring  Trigger an interrupt on touch to the Micromite User Manual for full parameter details; also,  Create a touch button work through Chapter 8 from the Getting Started with the  Respond to a touch button Micromite manual. The above list shows just some of the building-block examples These ideas will provide building blocks to enable us to put available for displaying information on the screen; overall, the touchscreen to good use. We will be working towards Fig.9 is not a very useful end result, so now let’s enter the a short program that demonstrates how to control external following short program instead: hardware via the touch-screen. In our example, we will be Practical Electronics | April | 2020 55 controlling an LED (connected to an I/O pin) switching it on or off via two touch-buttons drawn on the screen (see Fig.11). Let’s begin. Reading touch position Two built-in system variables, Touch(x) and Touch(y), contain the screen coordinates of the current position being touched on the screen. The coordinate values returned have a direct correlation to the pixel position; the x coordinate responding to the horizontal (left/right) location, and the y coordinate for the vertical (up/down) location. If the screen is not being touched then both return a value of −1. Note that only a single touch-point can be read; so if you are touching more than one point on the screen, then the ‘averaged’ coordinate values are returned (which in most cases is meaningless). That said, a single point of touch is ideal for most situations, such as selecting something on the screen. To see the Touch(x) and Touch(y) system variables in action, type in, and RUN the following five-line program: CLS DO TEXT 10,10,"X="+STR$(Touch(X))+" TEXT 10,60,"Y="+STR$(Touch(Y))+" LOOP ",lt,1,3 ",lt,1,3 The top-left corner represents coordinate x = 0, y = 0 (and the bottom-right corner is 240,320 – this assumes that the orientation setting in OPTION LCDPANEL was set to P for portrait). However, you probably won’t see these exact numbers returned when touching the relevant corners as it will ultimately depend on how accurately the screen was calibrated (ie, how accurately you touched the ‘targets’ after GUI CALIBRATE). Don’t panic if you don’t see values of 0,0 and 240,320 returned in diagonally opposite corners; but do ensure that you see something close to them (otherwise the touch feature won’t be as accurate as it could be). If necessary, re-calibrate with GUI CALIBRATE and try to be as precise as you can be when touching each target centre point. To make the program more interesting, insert the following single line of code (immediately after the second TEXT command, and before the LOOP): PIXEL Touch(x),Touch(y) Using a Touch interrupt MMBASIC makes it very easy to set up an interrupt that is triggered whenever the screen is touched. In fact, it is better to think of the interrupt as being configurable to trigger only when the screen is ‘just touched’, or only when ‘just released’ (or to trigger on either of these). To see this in action, start a new program (ie, type NEW at the command prompt), then enter and RUN the following program (its operation will be explained shortly): SETPIN 15,INTB, TouchInt DO CLS RGB(BLUE) LOOP SUB TouchInt CLS RGB(RED) END SUB On running the program you will see the screen turn blue; and whenever you touch the screen, or release your touch, the screen flashes red briefly (indicating the interrupt being triggered). So how does it work? There are three distinct parts to this program. 56 The first line simply sets up the interrupt by detecting a change of state on pin 15 (the INTB parameter). If you refer to the schematic in Fig.4, you will see that I/O pin 15 connects to the TFT’s touch-interrupt output pin from the TFT module (and we referred to this pin as the touch-interrupt parameter when setting OPTION TOUCH). We are simply saying to jump to an interrupt subroutine (that we have named TouchInt) whenever there is a change of logic state from the TFT touchinterrupt pin – in other words, whenever the screen is ‘just touched’ or ‘just released’ (liken it to ‘pen down’ and ‘pen up’ detection if that helps). The main-program is just three lines long, comprising a simple DO/LOOP that continually turns the screen blue. Hence, the screen remains blue until the interrupt subroutine is called. Looking at the single line of code in the interrupt subroutine, you can see that all it does is change the screen colour to red (but the main program immediately then sets it back to blue – resulting in the brief red flash whenever the interrupt is triggered). Now change the SETPIN line of code by altering the INTB parameter to INTH, and then to INTL. Observe the effect it has on the triggering of the interrupt: either a low-to-high change with INTH (ie, release only) or a high-to-low change with INTL (ie, touch only). One example of how to use this touch interrupt in a practical situation is to trigger an emergency stop of some external hardware; eg, a running motor. Creating a touch-button We will now write a short program to draw a button on the screen. To do this we will use the graphical command RBOX to draw the button, and the TEXT command to give it a label. Now RUN the following program (again, start a new program): CLS RGB(blue) RBOX 70,120,100,50, ,RGB(red),RGB(black) TEXT 120,145,"OFF",cm,1,2,RGB(red) The RBOX command draws a button that is 100-pixels wide, by 50-pixels high; and it is drawn at the location with coordinates x = 70 and y = 120 (this is where the top-left of the button is located). The TEXT command then adds the ‘OFF’ label centre justified at the central position of the button. The x coordinate for the TEXT command is calculated as 70 pixels along (ie, where the left side of the button is) plus half the width of the button (ie, 100/2 = 50) giving a value of x = 120 as the centre of the button. The y coordinate is from the top of the button (y = 120 from the RBOX command) plus half the height of the button (ie, 50/2 = 25) giving a value of 145. Hence x = 120 and y = 145 for the centre of the button – and using CM justification in the TEXT command means that the label will be positioned exactly in the centre of the button. If you didn’t quite follow this, just RUN the program and check that you see an OFF button near the middle of the screen. Next, let’s add a second (ON) button by adding these two lines of code: RBOX 70,220,100,50, ,RGB(red),RGB(black) TEXT 120,245,"ON",cm,1,2,RGB(green) We will be using these buttons to show you how to control an external LED connected to I/O pin 17. In addition, we will mimic the LED status on the touchscreen. To do this, we will use the CIRCLE command and fill it with either black (to indicate the LED is off), or green to indicate that it is on. The fill-colour of the LED (ie, the CIRCLE’s fill-colour parameter) will be stored in a variable that we call FCol. To draw the LED on the screen, insert these two lines at the very start of your program: Practical Electronics | April | 2020 Fig.12. Example screen-shots of numerical and alphabetical touch-keypads. Fig.13. On running MasterMind you will see this screen (it is animated!) DIM FCol as INTEGER FCol = RGB(black) Fig.14. Black and white scoring pegs provide feedback to each guess you make. DO CIRCLE 120,50,10,1,1,RGB(white),FCol LOOP and add this DO/LOOP at the very end: DO CIRCLE 120,50,10,1,1,RGB(white),FCol LOOP Having typed in the above code, RUN the program to see the end result. Note that neither the buttons, nor the LED, will actually do anything at the moment (that comes next) – we are simply creating two buttons and an LED on the screen ready for the next stage. Check that on running the code you see something similar to that shown in Fig.11. Responding to a touch button Now that we have the required elements drawn on the screen, we need to add the code that responds to a press of either touch button. To do this, we use the ‘how-to’ techniques learnt above. In essence, we use an interrupt that is triggered whenever the screen is touched, and within the interrupt subroutine we check the values of Touch(x) and Touch(y) to see if they fall within the position of either button. If so, then we will set the state of the LED (both the on-screen virtual LED, and the external physical LED) and switch it either off or on as required. To achieve all of this we add two lines of code at the start (to set up the touch-interrupt on pin 15, and to configure an I/O pin for the external LED), and then add the touch-interrupt subroutine code (to test if a button is pressed). The complete listing is shown here – the non-bold code is your current program. Just add the bold code (and check each line shown below does indeed match your code). SETPIN 15,INTL,TouchInt SETPIN 17,DOUT) DIM FCol as INTEGER FCol = RGB(black) CLS RGB(blue) RBOX 70,120,100,50, ,RGB(red),RGB(black) TEXT 120,145,"OFF",cm,1,2,RGB(red) RBOX 70,220,100,50, ,RGB(red),RGB(black) TEXT 120,245,"ON",cm,1,2,RGB(green) Practical Electronics | April | 2020 SUB TouchInt xx=Touch(x) yy=Touch(y) IF (xx>70) AND (xx<170) THEN IF (yy>120) AND (yy<170) THEN FCol=RGB(black) PIN(17)=0 ELSE IF (yy>220) AND (yy<270) THEN FCol=RGB(green) PIN(17)=1 END IF END IF END SUB Once your code matches the above, connect an LED (and current-limiting resistor) to I/O pin 17. Then RUN the program and check you can control the LED via the two touch buttons. Ensure that both the physical LED and the virtual LED change state correctly. If not, then by now you should be confident enough to check things over – start with the code, and then check physical connections. Do ensure that you understand each line of code 100% as you will then find it very easy to create your own touch-screen GUI to control external hardware. Keypads From the above example that demonstrates how to implement a couple of buttons, the next logical step is to create a ‘keypad’ that allows for data to be inputted (such as a numeric value). To do this, you essentially create several buttons arranged appropriately on the display with relevant text labels. Two examples of such keypads are shown in Fig.12. Rather than use valuable space here repeating the code from the manuals, please refer to Chapter 8 from the Getting Started with the Micromite manual. You will find the code for a numeric ‘data entry’ keypad on pages 83-85. This is an excellent implementation, so do be sure to take the time and try it out. Some example GUIs Some of you may prefer to learn from other people’s code as opposed to writing it from scratch. If you are one of these 57 Fig.15. The BackPack PCB is a lowcost, yet extremely useful module to explore this month’s topics. (and even if you’re not), it is well worth looking through some of the amazing programs that have been written by Geoff Graham, the creator of the Micromite. On his website (geoffg.net), you will see various projects that he has created using the 2.8-inch touchscreen to great effect. All his code is available to download for free from his website. I strongly recommend you take the time to explore some, if not all, of the following projects (you will see them listed in the top left corner of his home page):  Air Quality Monitor  DDS Signal Generator  Super Clock  Boat Computer  Parking Assistant. Look at how Geoff uses the graphical commands, as well as how he uses ‘touch’ in the various user interfaces. Do feel free to change his code so that you can gain a better understanding of just how easy it is to use the touch-screen in your own projects. The Micromite BackPack For those of you who want to use a 2.8inch touchscreen in your own dedicated Micromite project, or who just want to explore MMBASIC graphics and touch features in more detail, I recommend the Micromite BackPack as a very useful module. In fact, you will see that the above-mentioned projects on Geoff’s website are all based around the Micromite BackPack (it has also been used for many projects featured in PE). Better still, a full description of building one has been published in PE. See May 2017, then updated in May 2018. You can liken version 2 of the BackPack as an MKC and a DM on one compact PCB, with a 14-way socket into which you directly plug the TFT (see Fig.15). This low-cost PCB means that you are able to keep your MKC and DM dedicated to development purposes. The BackPack PCB mostly uses through-hole Questions? Please email Phil at: contactus<at>micromite.org 58 components – apart from the USB socket and two transistors. These transistors allow for software control of the backlight brightness (via PWM, just as we did with the IPS display). These three SMD parts are supplied pre-soldered, meaning there is just a handful of through-hole parts required to complete the module: six capacitors, four resistors, three connectors, an LED, a voltage regulator and a button (plus the Micromite PIC, the MicroBridge PIC, and the TFT). Think of the BackPack as an MKC/DM/touch-TFT ready to run any project you develop. Readers of PE can email me directly for a crazy offer of this fantastic PCB! MasterMind This month’s download demonstrates a practical (and fun) use for the screen in the form of the famous game, MasterMind. It brings together several elements that have been covered in this series, including IR remote control, sound and graphics. We are not going to go into an explanation of the code as this is something you can do as a personal exercise. However, for those of you unfamiliar with the game, we will explain the rules enough for you to have fun playing this addictive game. Put simply, you have to solve the secret colour code as quickly as possible (see Fig.13). The game starts with the Micromite choosing four random coloured ‘pegs’ and sequencing them in a row – this forms the secret colour code. Each peg can be any one of six colours, and the pegs can have colours repeated. Not only do you need to identify the colour of the four pegs, but also the sequence of the pegs has to be solved. After each ‘guess’, the Micromite will score you in the form of black and white scoringpegs. A black scoring peg indicates that you have the correct colour peg in the correct position, and a white scoring peg indicates a correct colour but in the wrong position; so ultimately you’re seeking four black scoring pegs! The skill comes in using the scoring pegs to deduce what the secret code is; and you have no more than ten attempts at guessing the secret code correctly. To give some examples regarding scoring pegs, refer to Fig.14. This shows an actual game played (and won) in which the secret code was red, blue, yellow, red. The first attempt (numbered 1 at the bottom of the screen) shows a guess of cyan, green, red, green. This resulted in a score of just one white peg for the red guess in position 3 (ie, correct colour, wrong position). Likewise, attempt 2 shows a guess of yellow, blue, yellow, blue and scored two black pegs for the correctly guessed coloured pegs in positions 2 and 3. However, during game play, we do not know which pegs in the guess the scoring peg(s) refer to – that is where your skill and powers of deduction come into play. Work through the remaining attempts in Fig.14 to ensure that you understand the scoring pegs for the various guesses entered. You can see that the correct code was entered in attempt 9 (resulting in four black pegs). Note that in attempt 8 we had a total of four scoring pegs meaning that at this point we knew the colours of all four pegs in the secret code – they just needed to be put in the correct order. A few useful points to bear in mind:  The quantity of scoring pegs can be anything from none to four  The position of the scoring pegs has no relevance; they are simply displayed from left to right, with any black peg(s) first, followed by any white peg(s)  A response of no scoring pegs means that none of the colours in the guess are in the secret code  A guess can comprise of up to four pegs; hence you can guess a single peg if you want to find out if a particular colour peg is in the secret code. Download the file MasterMind.txt from the April 2020 page of the PE website and install it onto your MKC. The program code is commented throughout and finished to a level that allows you to enjoy playing the game. However, why not try adding your own features! I hope you enjoy it. Next month Over the last year and a bit we have covered numerous topics in the Make It With Micromite series. We believe it is now time to put many of the things that we have learnt into a fantastically fun project. So what is this project? On several occasions we have mentioned building a robot buggy, so next month we will start this journey by assembling the basic system. The robot will be built on a Zumo chassis (see Fig.16), it will be operated remotely via Bluetooth, and will have a personality that will be developed over several months. You can either follow step-by-step, or alternatively customise the options for your own personal use – after all, you now have the skills to easily control hardware by using the power and simplicity of MMBASIC. Fig.16. The Micromite Robot Buggy featured next month is based on the excellent Zumo chassis. Practical Electronics | April | 2020