Arduino is a cheap and popular way to build your own electronics, typically with small sensors and motors, and learn a bit of programming to control it.
Controlling it with Small Basic enables you to display data it generates or change settings interactively from your PC.
This blog is not about Arduino features, but how to communicate with it with Small Basic using a serial port extension, in this case
LDCommPort in the LitDev extension (see LDCommPort API). Basically, we can send and receive data as bytes or strings to the Arduino via its USB connection. I used strings since we can always cast these to anything else like numbers in Arduino and Small Basic.
You could use any convention for your data, but you will need an event (
LDCommPort.DataReceived) in Small Basic to detect that data has been received. No event is required in the Arduino program, since it operates in a continual loop, you just check for new data each time the loop runs.
Send Data to Arduino from Small Basic
When sending data to the Arduino, I used the first character to define the data type:
- i for an integer
- f for a float
- s for a string
- 1-8 for one of 8 buttons I created in the Small Basic interface
Send Data to Small Basic from Arduino
When sending data from the Arduino to Small Basic, I just sent whatever data I wanted as an ascii string, testing integers, floats, strings and characters. I used a line-feed character '\n' (ascii code 10) to terminate all data for transfer. This is automatically added by the Arduino command
Serial.println, but not by
The following are the Arduino settings I used for an Arduino UNO, note it uses COM3 (yours may be different) port and ArduinoISP programmer.
Small Basic Interface
This is the Small Basic interface, import VBZ059.
The Small Basic code for this interface is a bit longer than it could be, most of it is just creating the interface and button events.
The Arduino code is similar to C++, and there are lots of samples for all sorts of electronic projects out there. It is compiled and sent to the Arduino using a special Arduino IDE. My sample below is just testing the serial communication, with no electronics at all. It could be shorter if you just wanted to send data to Small Basic or only have a couple buttons (code size is quite limited on Arduino), but I wanted to test various data types in the communication.
Serial.begin(9600)sets the serial communication speed, also set in Small Basic,
Serial.setTimeout(10) prevents partial data from being used, you may need to change this. If you are reading bytes consider using
Serial.available() in a
while loop to read all available bytes.
sInput = Serial.readString();
cInput = '0';
if (sInput.length() > 0)
cInput = sInput.charAt(0);
iInput = sInput.substring(1).toInt();
fInput = sInput.substring(1).toFloat();
sInput = sInput.substring(1);
fInput = 3.14;
iInput = random(256);
Serial.println("A text result");
Enjoy playing with Small Basic and Arduino.