Small Basic - Arduino
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.
Serial Communication
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 Serial.print
.
Arduino Settings
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.
Initialise()
'
'==================================================
'MAIN LOOP
'==================================================
'
While ("True")
If (newData <> "") Then
tbData = Text.Append("Data received : "+newData+nl,Controls.GetTextBoxText(tb))
Controls.SetTextBoxText(tb,tbData)
newData = ""
EndIf
Program.Delay(10)
EndWhile
'
'==================================================
'SUBROUTINES
'==================================================
'
Sub Initialise
gw = 600
gh = 215
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.Title = "Small Basic Arduino Interface"
GraphicsWindow.BackgroundColor = "Gray"
GraphicsWindow.BrushColor = "Black"
nl = Text.GetCharacter(10)
'
status = LDCommPort.OpenPort("COM3",9600)
LDCommPort.SetEncoding("Ascii")
LDCommPort.DataReceived = OnDataReceived
'
nButton = 8
spacing = (gw-30)/nButton
width = (gw-40)/nButton-10
For i = 1 To nButton
buttons[i] = Controls.AddButton("Action"+i,20+spacing*(i-1),175)
Controls.SetSize(buttons[i],width,25)
LDShapes.BrushColour(buttons[i],"SteelBlue")
EndFor
Controls.ButtonClicked = OnButtonClicked
'
clear = Controls.AddButton("Clear",20,20)
Controls.SetSize(clear,width,25)
LDShapes.BrushColour(clear,"SteelBlue")
'
send = Controls.AddButton("Send",20+spacing,20)
Controls.SetSize(send,width,25)
LDShapes.BrushColour(send,"SteelBlue")
'
value = Controls.AddTextBox(20+2*spacing,20)
Controls.SetSize(value,gw-2*spacing-40,25)
LDShapes.PenColour(value,"White")
LDShapes.BrushColour(value,"DimGray")
'
tb = Controls.AddMultiLineTextBox(20,60)
Controls.SetTextBoxText(tb,"Connection status : "+status)
Controls.SetSize(tb,gw-40,100)
LDShapes.PenColour(tb,"White")
LDShapes.BrushColour(tb,"DimGray")
EndSub
'
'==================================================
'EVENT SUBROUTINES
'==================================================
'
Sub OnDataReceived
dataIn = Text.Append(dataIn,LDCommPort.RXAll())
If (Text.IsSubText(dataIn,nl)) Then
newData = LDText.Trim(dataIn)
dataIn = ""
EndIf
EndSub
'
Sub OnButtonClicked
button = Controls.LastClickedButton
If (button = clear) Then
tbData = ""
ElseIf (button = send) Then
val = Controls.GetTextBoxText(value)
If (val = Math.Round(val)) Then
tbData = Text.Append("*** "+val+" sent as integer ***"+nl,Controls.GetTextBoxText(tb))
val = "i"+val
ElseIf (val+0 = val) Then
tbData = Text.Append("*** "+val+" sent as float ***"+nl,Controls.GetTextBoxText(tb))
val = "f"+val
ElseIf (val <> "") Then
tbData = Text.Append("*** "+val+" sent as string ***"+nl,Controls.GetTextBoxText(tb))
val = "s"+val
Else
tbData = Text.Append("*** Nothing sent ***"+nl,Controls.GetTextBoxText(tb))
EndIf
If (val <> "") Then
LDCommPort.TXString(val)
EndIf
Else
For i = 1 To nButton
If (button = buttons[i]) Then
tbData = Text.Append("*** Action "+i+" instruction sent ***"+nl,Controls.GetTextBoxText(tb))
LDCommPort.TXString(i)
EndIf
EndFor
EndIf
Controls.SetTextBoxText(tb,tbData)
EndSub
Arduino Code
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.
The Serial.begin(9600)
sets the serial communication speed, also set in Small Basic, LDCommPort.OpenPort("COM3",9600)
.
The 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.
char cInput;int iInput;float fInput;String sInput;void setup(){ Serial.begin(9600); Serial.setTimeout(10);}void loop(){ Input(); delay(10);}void Input(){ sInput = Serial.readString(); cInput = '0'; if (sInput.length() > 0) { cInput = sInput.charAt(0); switch (cInput) { case 'i': iInput = sInput.substring(1).toInt(); Serial.println(iInput); break; case 'f': fInput = sInput.substring(1).toFloat(); Serial.println(fInput); break; case 's': sInput = sInput.substring(1); Serial.println(sInput); break; case '1': fInput = 3.14; Serial.println(fInput); break; case '2': iInput = random(256); Serial.println(iInput); break; case '3': Serial.println("A text result"); break; case '4': Serial.print('A'); Serial.print('B'); Serial.println('C'); break; default: Serial.println("Error"); break; } }}
Enjoy playing with Small Basic and Arduino.
Comments
- Anonymous
March 24, 2016
Woohoo! Now we're talking! Thanks so much, LitDev! - Anonymous
March 30, 2016
Wow, that's great :) - Anonymous
October 16, 2016
Thank you litdev. I tried this sample codes with my OSOYOO UNO R3 compatible Arduino board and succeeded. - Anonymous
October 29, 2016
Check out Nonki's new blog post with Arduino samples: https://blogs.msdn.microsoft.com/smallbasic/2016/10/17/small-basic-arduino-sample-programs