Saturday, July 11, 2020

The Practical Maximite Part 3

In this tutorial we are going to learn how to take advantage of the Arduino compatibility of the Maximite. All of the Colour Maximites have the headers, but not al of them have the sockets soldered in. Mine came with the sockets, if yours did not, you will have to stop here.

When I was making the choice as to what Arduino hat I wanted for my Maximite, it was actually a pretty easy choice. An LCD hat gives you another readable output, which is good for a number of things like error capturing when debugging programs and providing secondary information. These also provide you with a set of buttons that you can use to trigger events. The hat I got can be found here; 16x2 LCD Keypad Shield 1602. These hats are made by many companies, are readily available and are cheap, since you can generally get them for under $10. Make sure if you are buying one of these from a different manufacturer, that is is built on a HD44780 LCD chip, MMBasic has built in support for this chipset.



There are a couple of caveats with these hats, first, there is a reset button, this is handy for resetting your Maximite if, like me, you do not have a power switch. The downside is, if you accidentally press it, you will loose any unsaved programs you have in memory, so be careful and save often. Second, there is a select button, this does not work, it is hooked up to the 5 volt rail, but I have no idea what Pin it is connected to. Lastly, if at first you cannot get anything to display, the contrast is probably set way too low. The contrast is controlled by a screw on top of the blue plastic thing in the upper left corner of the hat. Give the screw 10 complete turns clockwise, and that should make your text become visible and you can adjust to taste from there.

 Now, lets get on with doing useful things with this hat. I am sure the first thing you want to know is how do you get text on the screen. The first thing you must do is initialize the LCD by telling your program which pins it will be using to communicate, 99% of the time, this is d4,d5,d6,d7,d8,d9 and this is done by simply adding "LCD INIT d4,d5,d6,d7,d8,d9" to the top of the program. Next we issue the LCD command, the first number tells the Maximite which line to print the text on (1 or 2), the second number tells the Maximite what position in the line to put the text (1 - 16), this is then followed by the text you want displayed. The following program displays "Hello World" followed by the date.

'lcdtxt.bas
LCD INIT d4,d5,d6,d7,d8,d9
LCD 1,1,"Hello World"
LCD 2,1, Date$

That is the easy part. The next thing we want to do is make the buttons do something useful. All of the buttons are connected to Pin 35, when in use, Pin 35 will constantly output 3.3 volts, when one of the buttons is pressed, that voltage is reduced and the button can be determined by the voltage. Unfortunately, the voltage differs from board to board, which means you need to determine what these voltages are. The following program configures Pin 35 for Analog Input, this is so we can read the voltage being put out by the pin. The program then starts polling the pin, if no button is pressed and the voltage is 3.3, it does nothing, because no button is being pressed. If the voltage drops, meaning a button is pressed, it prints the voltage on your monitor. Press each button and make a note of the the output for each button.

'bttnvolt.bas
SetPin 35,AIN

Main:

  Volt = Pin(35)
  If Volt <> 3.3 Then Print Volt

GoTo Main

My results were Left=2.01, Right=0, Up=0.49 and Down=1.26, the numbers actually went out 5 decimal places, but don't worry about that. The Right button is easy, Voltage is 0. Left and Down are both fairly close to 2 and 1 respectively, so by using the Int() function, I can round those values down, without worrying about minor variations in the voltage. Up is a bit more complicated, for this, I need to check to see is the voltage is less than 1 and greater than 0, not tough, but something you have to pay attention to. Putting this altogether, the following program configures Pin 35, initializes the LCD, and displays "LCD Button Test" on the first line. The program then polls Pin 35 for a button press and the displays on the second line which button was pressed. The trailing "*" are there simply to make each string the same length, and no characters are left over from the last button press.

'bttntest.bas
SetPin 35,1
LCD INIT d4,d5,d6,d7,d8,d9
LCD 1,1,"LCD Button Test"

Main:
  Volt = Pin(35)
  If Volt = 0 Then LCD 2,1, "*Right"
  If Int(Volt) = 2 Then LCD 2,1, "*Left*"
  If Int(Volt) = 1 Then LCD 2,1, "*Down*"
  If Volt < 1 And Volt > 0 Then LCD 2,1, "*Up***"
GoTo Main

If you have the LED's from the previous tutorial still connected, you can use the buttons to blink the LED's.

'bblink.bas
SetPin 12, DOUT
SetPin 13, DOUT
SetPin 14, DOUT
SetPin 15, DOUT
SetPin 35, AIN
LCD INIT d4,d5,d6,d7,d8,d9
LCD 1,1,"LCD Button Test"

Main:
 Volt = Pin(35)

If Volt = 0 Then
    LCD 2,1, "*Right"
    Pin(15) = 1
      Else
        Pin(15) = 0
  EndIf

  If Int(Volt) = 2 Then
    LCD 2,1, "*Left*"
    Pin(12) = 1
      Else
        Pin(12) = 0
  EndIf

  If Int(Volt) = 1 Then
    LCD 2,1, "*Down*"
    Pin(14) = 1
      Else
        Pin(14) = 0
  EndIf

  If Volt < 1 And Volt > 0 Then
    LCD 2,1, "*Up***"
    Pin(13) = 1
      Else
        Pin(13) = 0
  EndIf

GoTo Main

________________________________________________________

Here are some useful programs for handling the LCD Hat.

'bloff.bas
' This turns the backlight off
SetPin31,DOUT
Pin(31) = 0

'blon.bas
'This turns the backlight on
SetPin31,DOUT
Pin(31) =1

'lcdcls.bas
'This clears all text from the LCD
LCD INIT d4,d5,d6,d7,d8,d9
LCD Clear

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Mastodon