Difference between revisions of "Arduino"
From Free Pascal wiki
Jump to navigationJump to searchLine 3: | Line 3: | ||
== Serial communication == | == Serial communication == | ||
+ | We will explain how to communicate with your board through serial port connection. | ||
+ | |||
+ | === Lazarus serial application === | ||
There are many ways for [[Hardware Access#Serial Communication|serial]] communication in Lazarus and FPC. | There are many ways for [[Hardware Access#Serial Communication|serial]] communication in Lazarus and FPC. | ||
− | === Lazarus serial application === | + | ==== Minimal Lazarus Synaser serial application ==== |
We will use [http://synapse.ararat.cz/doku.php Synaser library] in this example. Create Lazarus application with one form and two buttons. Include Synaser unit and add Synapse package as a new requirement to your project. In OnClick event of both buttons put something like this: | We will use [http://synapse.ararat.cz/doku.php Synaser library] in this example. Create Lazarus application with one form and two buttons. Include Synaser unit and add Synapse package as a new requirement to your project. In OnClick event of both buttons put something like this: | ||
Revision as of 09:22, 30 May 2017
This article is about Arduino boards and how can your Lazarus/FPC application communicate with them.
Serial communication
We will explain how to communicate with your board through serial port connection.
Lazarus serial application
There are many ways for serial communication in Lazarus and FPC.
Minimal Lazarus Synaser serial application
We will use Synaser library in this example. Create Lazarus application with one form and two buttons. Include Synaser unit and add Synapse package as a new requirement to your project. In OnClick event of both buttons put something like this:
procedure TForm1.Button1Click(Sender: TObject);
var
ser: TBlockSerial;
begin
ser := TBlockSerial.Create;
try
ser.Connect('COM1'); // write here Arduino COM port number (on linux it's something like '/dev/ttyUSB0')
Sleep(250);
ser.config(9600, 8, 'N', SB1, False, False);
ser.SendString('on'); // button 2 should have 'off' here
finally
ser.free;
end;
end;
Arduino serial application
You can program your Arduino in several languages.
Minimal Arduino C sketch
You can follow this nice step by step tutorial. In short, you need to add a led with proper resistor to your input pin 13, compile and download this sketch to Arduino:
int led = 13; // Pin 13
void setup()
{
pinMode(led, OUTPUT); // Set pin 13 as digital out
// Start up serial connection
Serial.begin(9600); // baud rate
Serial.flush();
}
void loop()
{
String input = "";
// Read any serial input
while (Serial.available() > 0)
{
input += (char) Serial.read(); // Read in one char at a time
delay(5); // Delay for 5 ms so the next char has time to be received
}
if (input == "on")
{
digitalWrite(led, HIGH); // on
}
else if (input == "off")
{
digitalWrite(led, LOW); // off
}
}
Ethernet communication
TBD
Lazarus ethernet application
TBD
Arduino ethernet application
TBD