RodBird
Feb 2, 2016
=Having fun with the Arduino=
//rodbird//
[[toc]]
[[image:arduino.png]]
The Arduino is a much loved general purpose input and output board. It has a huge following and lots of support. This article is about how you can interface the board via the serial port and so give Liberty BASIC access to all that the board has to offer.
----
=Start simply=
If this is your first step into microprocessor control board electronics my advice would be to start simply. Purchase a Uno Kit. There are several kits all easily obtainable, each provides the board, electronic components and usually a booklet to get you up and running quickly.
Some kits use soldered components, others offer plug in breadboard connection or ready made plug and socket connectors. Probably best to opt for plug in breadboard style which lets you build circuits quickly while you play and learn.
This is all the kit you need to get started:
[[image:arduino3.png]] [[image:arduino.png]] [[image:arduino2.png]]
Purchase this kind of kit and play with it for a day or so, follow the tutorials available online or use the booklet. Get to the point where you have downloaded your own sketch to make the Arduino board LED flash. Now you are ready to unleash Liberty BASIC.
=The Sketch=
You will now know that the Arduino board uses a BASIC like programming language but there are significant differences to Liberty BASIC. These programs are called Sketches and you write them on your PC and download them to the Arduino board which fires up and runs autonomously. You can go and learn this language and you probably will over time, right now we are going to use a general purpose Sketch written by the Liberty BASIC community. This Sketch allows simple serial messages to control and receive info from the board. So the board is running autonomously but it is dedicated to interpreting and actioning our control requests. It has become a slave to Liberty BASIC.
Here is the Sketch, you will need to download it to your Arduino board, follow the booklet instructions on how to do that.
[[code format="lb"]]
#include <Servo.h>
// set up 14 servo objects in an array
Servo myservos[14] ;
void setup() {
Serial.begin(9600);
Serial.println("Com Open");
}
void loop() {
// you can call your own functions here
// lbRun() is only called by interrupt
// when there is serial data available
}
// serialEvent checks to see if data is available on serial port
// when a message comes we just invoke our lbRun() function
void serialEvent(){
// check if data available, if so call the lbRun() routine
if ( Serial.available()) {
lbRun();
}
}
void lbRun()
{
while (Serial.available() > 9)
{
// look for the next valid integer in the incoming serial stream:
int cmd = Serial.parseInt();
// do it again:
int pin = Serial.parseInt();
// do it again:
int val = Serial.parseInt();
// look for the "*", That's the end of our command string
if (Serial.read() == '*')
{
//now select the command to run and use the pin and val argument
if ( cmd == 0)//add servo
{
myservos[pin].attach(pin);
}
if ( cmd == 9)//detach servo
{
myservos[pin].detach();
}
if ( cmd == 1)//getdigital
{
pinMode(pin, INPUT);//set to input
digitalWrite(pin, HIGH);//turn on pullup resistor
Serial.print(pin);
Serial.print(",");
Serial.print(digitalRead(pin));
Serial.print("*");
}
if ( cmd == 4)//setdigital
{
pinMode(pin, OUTPUT);//set to ouput
digitalWrite(pin, val);
}
if ( cmd == 5)//setanalog
{
analogWrite(pin, val);
}
if ( cmd == 2)//getanalog
{
analogRead(pin);
delay(10);
Serial.print(pin);
Serial.print(",");
Serial.print(analogRead(pin));
Serial.print("*");
}
if ( cmd == 6)//setservo
{
myservos[pin].write(val) ;
}
if ( cmd == 7)//settone
{
if (val == 0)
{
noTone(pin);
}
else
{
tone(pin, val);
}
}
if ( cmd == 3)//getpulse
{
Serial.print(pin);
Serial.print(",");
Serial.print(pulseIn(pin, val));
Serial.print("*");
}
if ( cmd == 8)//ping
{
// establish variables for duration of the ping,
long duration;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delayMicroseconds(2);
digitalWrite(pin, HIGH);
delayMicroseconds(5);
digitalWrite(pin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pin, INPUT);
duration = pulseIn(pin, HIGH);
Serial.print(pin);
Serial.print(",");
Serial.print(duration);
Serial.print("*");
}
}
}
} // end or lbRun()
// add your own functions here
[[code]]
Very briefly this Sketch loads the servo control library then starts polling the serial buffer looking for complete messages that we have sent over the serial link from Liberty BASIC. When it finds a complete message it interprets or parses that message to establish what we are asking it to do. Then on a simple select case basis it actions that request returning results if needed. We will pick out and explain what each part of the sketch achieves as we discuss the commands it understands.
=The message format=
A Liberty BASIC message is sent in the following fixed length format, numeric values have leading zeros, commas are inserted to aid parsing:
"CC,PP,AAAA*"
Where:
CC = a two digit number specifying which command to action
PP = a two digit number specifying which pin to act on
AAAA = a four digit number passing the value to apply
* = the terminator character, this tells the Arduino it has a complete message
Responses, if issued, are received in a variable length message comma delimited the following format:
"PP,RRRR*"
Where:
PP= a two digit number specifying the pin producing the result
RRRR= up to four digits providing the result
* = the terminator character
=Add a GUI=
Text here.
[[code format="lb"]]
'code here
[[code]]
=Third Part Title=
Text here.
[[code format="lb"]]
'code here
[[code]]
----
[[toc|flat]]