Basic interpreter arduino

BASIC on Arduinos © LGPL

This is a complete BASIC interpreter for Arduino, including I/O and other useful functions.

The original idea

Steve Wozniak once said that creating the Apple 1 integer basic interpreter was the biggest professional challenge of his life. I tried to do what Steve did, write a BASIC interpreter from scratch without using any code from anyone else. I wanted something useful on a very small computer. It turned out that it may be a nice tool for an Arduino. Interactive access to the I/O functions makes it easy to test electronics. All pins can be steered with BASIC commands. Programs and data can be stored in the EEPROM.I implemented most of the standard BASIC language including strings, floating point and most of the Dartmouth language set.

Download and compile the BASIC interpreter

Download the code in TinybasicArduino/TinybasicArduino.ino and import it into your Arduino IDE together with hardware.h and basic.h. Open the code and review the settings section a the very beginning of the code. For standard 8bit AVR systems like the ones mentioned above the initial sections needs to look like this

Don’t change this unless you want to compile for MSDOS or Windows. As a language feature section below these settings can be

The hardware definition section of hardware.h all should be on `#undef` except for

Compile the code with these settings and review the memory consumption. For an UNO this should be a 19 kB executable and there should be 1.4 kB of free RAM reported after compilation. Upload the code.

Use the serial monitor of the Arduino IDE with the following settings * baudrate 9600 * Newline

Источник

My Arduino BASIC interpreter — first steps © LGPL

This is an Arduino BASIC interpreter, written from scratch walking in the footstep of Steve Wozniak’s Apple 1 integer basic.

The original idea

Steve Wozniak once said that creating the Apple 1 integer basic interpreter was the biggest professional challenge of his life. I tried to do what Steve did, write a BASIC interpreter from scratch without using any code from anyone else. I wanted something useful on a very small computer.

It turned out that it may be a nice tool for an Arduino. Interactive access to the I/O functions makes it easy to test electronics. All pins can be steered with BASIC commands. Programs and data can be stored in the EEPROM.

I implemented most of the standard BASIC language but no floating point and no strings because this makes little sense on an Arduino. Strings will come later in a very basic way.

Download and compile the BASIC interpreter

Download the code in TinybasicArduino/TinybasicArduino.ino and import it into your Arduino IDE. Open the code and review the settings section a the very beginning of the code.

For standard 8bit AVR systems like the ones mentioned above the initial sections needs to look like this

Don’t change this unless you want to compile for MSDOS or Windows.

As a language feature section below these settings can be

The hardware definition section all should be on `#undef` except for

Compile the code with these settings and review the memory consumption. For an UNO this should be a 19 kB executable and there should be 1.4 kB of free RAM reported after compilation. Upload the code.

Use the serial monitor of the Arduino IDE with the following settings

The interpreter should show a startup line like

Stefan’s Basic 1.2 Memory 1024 1024

and then prompt for input. The first number is the available RAM for BASIC, the second the available EEPROM.

Enter your first BASIC program

10 PRINT «HELLO WORLD»

should display «HELLO WORLD» and return to the input prompt.

will store the program into the EEPROM. You can try this out by resetting your Arduino and then reload the program with

Type LIST to check it.

The blink program

The equivalent to «Hello World» on Arduinos is the blink program. In BASIC it looks like this

Type it in and start it with RUN command. The little LED on your Arduino should blink now in 1 second intervals. Sending the character ‘#’ will interrupt the program and make the interpreter return to the command prompt. You can download the blink program here blink.bas .

The Arduino LED will start to blink. Sending the character ‘#’ will interrupt the program. All other input is ignored. Typing any other character than ‘#’ will flood the serial buffer and the interrupt character will no longer be recognised.

The Arduino functions work pretty much as in C++. See here for the ported commands . You can try them in interactive mode. This is one of the advantages of running BASIC on an Arduino.

Again, you can save your work to the EEPROM by typing SAVE and reload the program at a later time using LOAD .

Autorun from EEPROM

If you have saved a program to EEPROM you can activate the autorun flag by typing

If you reset the Arduino now using the reset button or disconnecting and connecting the power supply, the program is started automatically. No command prompt will appear. Sending ‘#’ from the serial console will interrupt the program.

now, no program will be shown. Autorun programs are not loaded to RAM but are executed directly from the EEPROM. This way, the entire RAM is available in autorun mode.

The autorun flag can be removed by the command

The command prompt will appear after reset.

There is more on this entire group of commands here » SD Cards, File commands, EEPROM and related stuff «.

You need any analog sensor and a cable for this project. One nice example would be the photo sensor shown in the picture. It returns an analog value 1023 at absolute darkness and 0 at maximum brightness.

Connect the analog output pin of the sensor to the analog port A0 and the digital output to the digital port D2 of the Arduino. Then type in the BASIC program

and save it to the EEPROM with SAVE .

Running the program will record the value of the sensor read every second for 10 times and calculate the minimum and maximum values.

AZERO is a constant containing the lowest analog pin A0. A1 would be AZERO+1 . The program can be downloaded here minmax.bas .

The EEPROM of the Arduino can be used to store measurement data. The sketch eeprom.bas measures the average light intensity for one minute and stores the result in the EEPROM:

After activation of the autorun feature with `SET 1,1` the Arduino can be used without a computer to measure data. The value can be read the next time the Arduino is connected to a computer.

The Arduino IDE has a simple serial monitor but cannot download and upload data. Many solutions for this exist. I recommend and use Roger Meier’s CoolTerm[ CoolTerm. Binaries for Windows, Linux and Mac exist. The program is a full featured serial terminal. I downloaded my Mac binary from MacUpdate .

Before connecting to the Arduino the terminal programs has to be configured in the Options menu:

Set the port to your serial port (the same as the Arduino IDE used), the baudrate to 9600, data to 8, parity to none and stop to 1. These are the default settings of Arduino serial and Stefan’s BASIC.

In the terminal configuration page chose these options:

Raw mode transfers every keystroke, key emulation has to be Unix style LF. In addition to this «Handle DEL and BS» is needed to delete data in a line and «Handle FF» enables clearing the screen from BASIC. These are the minimal terminal functions in the standard BASIC language set.

The terminal settings for the use of Stefan’s BASIC on an Arduino can be downloaded here:[ CoolTerm settings . The program is not a full features vt52/100 compatible terminal program. It offers no cursor control.

Uploading or downloading a program

For file upload goto the Connection menu in CoolTerm and select Send Text/ Binary File. One nice example could the the letter guessing game from 101 BASIC games. It fit’s into a 1k Arduino BASIC memory. Upload the program and enjoy 1975 gaming fun.

With the current settings of CoolTerm there is no flow control. Timing problems can cause the upload to fail as BASIC on the Arduino doesn’t receive characters fast enough. A simple fix would be to add small delays in the Transmit page of CoolTerm’s configuration menu. A 3ms line delay does the job.

A program can be downloaded from the Arduino using the Capture Text/ Binary File function. After starting the capture type `LIST` and then stop the capture. Edit the file on your computer to remove the last and first line.

Alternatively, you can use the very elementary python serial monitor to connect to the Arduino. The serial port has to be edited in the first lines of the program. Control L and Control S are used for upload and download.

LCD shield based BASIC systems

One of the nicest small hardware extensions for Arduino are the 16*2 LCD shield with 4 buttons. They can be used from BASIC programs easily.

Compile the interpreter as before but use the setting

This activates the display driver and the hardware code for the LCD shield. Compile and upload the script.

Connect to the Arduino with the Serial Monitor or CoolTerm. Enter

print &2, «hello world»

and look on the display. It should display the string in the upper left corner of the display.

The modifier &2 redirects the output of the print command to the display. With the settings above the display can be accessed as a mini terminal, capable of scrolling and a few terminal commands. Type

to clear the display screen. 12 is the ASCII code of Form Feed and is used to clear the screen.

Keypad functions of the LCD shield

Type in the code

or upload it to the Arduino with CoolTerm using shield.bas . Type RUN to start the program then press one of the shield keys. The key code is displayed in the second line.

The keys are mapped to single character ASCII values — select = ‘s’, right =’r’, left = ‘l’, up = ‘u’ and down = ‘d’. GET &2, A returns these values into the variable A whenever the key is pressed and 0 if no key is pressed.

This code uses the special variables @X and @Y to position the cursor. They contain the horizontal and vertical position of the cursor starting at 0 and can be read or assigned by the program.

Display buffer array

Type in the following code or download it from this link dispbuf.bas then type RUN .

You can move around the ‘X’ symbol with the keys. Pressing select outputs the position of the symbol on the screen.

The code uses the display buffer array @D() . Writing to it displays a character directly on the screen. @D() starts at array subscript 1 and counts cells from left to right and top to bottom. Note how the program converts strings to integers in line 40 and 60. In assignments strings yield the ASCII code of the first character as their integer values. This only works in assignments or on the right hand side of integer expressions.

The program has the typical structure of Arduino sketches — line 10 and 20 are setup(), lines 30 to 140 are the loop and lines 200 to 220 are a function.

An example for a standalone program is the stopwatch.

Download the program stopw.bas ] and load it into the Arduino with CoolTerm.

Then type SAVE : SET 1,1 to activate autorun from the EEPROM. Powering up the Arduino from an external power source like a little power bar makes it a standalone stopwatch.

Источник

robinhedwards/ArduinoBASIC

Use Git or checkout with SVN using the web URL.

Work fast with our official CLI. Learn more.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Now you can turn your Arduino into an 80’s home computer!

A complete BASIC interpreter for the Arduino, using a PS/2 keyboard, and SPI OLED screen. The BASIC supports almost all the usual features, with float and string variables, multi-dimensional arrays, FOR-NEXT, GOSUB-RETURN, etc. Saving and Loading from EEPROM is supported, as well as auto-running a program on power-up. You can also read and write from the analog and digital pins.

There’s about 1k of RAM available for your BASIC programs and variables, so its roughly equivalent to my first computer (a Sinclair ZX81). The other 1k of RAM (on an UNO) is used for the keyboard and screen buffers, with a small bit of room left for the CPU stack. That works out quite well, since there’s a 1k EEPROM on the arduino so if your program fits in the basic environment, it will fit when saved to EEPROM!

1: An Arduino with at least 2k of RAM. i.e. UNO/Duemilanove/etc. It should also work on the MEGA but is untested. I think the sketch is too big for the Leonardo, since it looks like the bootloader uses more RAM on this model.

3: An SSD1306 based OLED Screen connected using SPI. See e.g. http://www.adafruit.com/product/938. I think it should also work fine with the 128×32 version — you’ll just need to change a couple of defines.

4: (Optional) A Piezoelectric buzzer for keyboard clicks and other sounds.

5: (Optional) A external EEPROM (e.g. 24LC256) lets you save more than one file. You can pick these up for about £2/$2 on ebay.

1: Download the zip file, unpack and copy the folder to your arduino sketches directory.

2: Install the PS/2 keyboard library if required. I’ve included the version I used in the zip file.

3: Install the SSD1306ASCII library. The normal Adafruit library is too RAM hungry for this project, so I’m using a massively cut down driver instead. I’ve modified this library a bit to get fast SPI transfers which improved the screen updating speed by a factor of about 4. The modified version is included in the zip file.

For both libraries, unzip the files and copy the folder to your arduino libraries directory.

4: Check your wiring corresponds to the pins in the comments/defines at the top of the Arduino_BASIC file.

Variables names can be up to 8 alphanumeric characters but start with a letter e.g. a, bob32 String variable names must end in $ e.g. a$, bob32$ Case is ignored (for all identifiers). BOB32 is the same as Bob32. print is the same as PRINT

Array variables are independent from normal variables. So you can use both:

There is no ambiguity, since a on its own refers to the simple variable ‘a’, and a(n) referes to an element of the ‘a’ array.

Источник

Adblock
detector