G
G
Ghost Doc2019-01-28 07:00:17
Delphi
Ghost Doc, 2019-01-28 07:00:17

How to implement dynamic plotting in Delphi from data with Arduino uno?

There is a program for constructing a static graph according to data from the Arduino. The graph needs to be built in real time.

The code
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, CPort, CPortCtl,
  VclTee.TeeGDIPlus, VCLTee.TeEngine, Vcl.ExtCtrls, VCLTee.TeeProcs,
  VCLTee.Chart, VCLTee.Series, Vcl.Grids, Data.DB, VCLTee.TeeData;

type
  TForm1 = class(TForm)
    ComPort1: TComPort;
    Memo1: TMemo;
    Button1: TButton;
    Button3: TButton;
    OpenDialog1: TOpenDialog;
    Button2: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Chart1: TChart;
    Series1: TFastLineSeries;
    Button7: TButton;
    Memo2: TMemo;
    procedure ComPort1RxChar(Sender: TObject; Count: Integer);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  i,n: Integer;
  a: array[1..10000] of real;
  ff: TextFile;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
comport1.ShowSetupDialog;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
comport1.Open;
Memo1.Clear;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
comport1.Close;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
 t:TStringList;
begin
t:=TStringList.Create;
t.Add(Memo1.text);
t.SaveToFile('C:\Users\len\Desktop\sbor_data\data.txt');
t.Free;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
   Begin
    AssignFile(ff,OpenDialog1.FileName);
    Reset(ff);
    i:=0;
    while not eof(ff) do
      begin
      i:=i+1;
        Readln(ff,a[i]);
      end;
    end
     else
      exit;
  CloseFile(ff);
end;

procedure TForm1.Button6Click(Sender: TObject);
var x,y: real;
begin
  Series1.Clear;
  for n := 1 to i-1 do
  begin
    x:=n;
    y:=a[n];
    Series1.AddXY(x,y,'',clred);
  end;
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
Memo2.Clear;
  for n := 1 to i-1 do
  begin
    Memo2.Lines.Add(FloatToStrF(a[n],ffGeneral,4,4));
  end;
end;

procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var
  S:string;
begin
  ComPort1.ReadStr(S, Count);
  Memo1.Text:= Memo1.Text + S;
end;
end.

<b>Скетч:</b>

float Step = 5.0F / 1024; // Вычисляем шаг U опорн / на градацию 

void setup() { 
Serial.begin(9600); // Задаем скорость работы монитор порта 
} 

void loop() { 
int analogValue = analogRead(0); // Задаем переменную analogValue для считывания показаний 
float voltageValue = analogValue * Step; // Переводим в вольты (показание * шаг) 
Serial.println(voltageValue); // Выводим значение в вольтах в порт 
delay(500); // Ждем пол секунды 
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kalapanga, 2019-01-28
@kalapanga

The formal answer is very simple - we received a value through the com port, added it to the Series and that's it. Well, maybe there needs to be an update.
And then there are the details and decorations, which strongly depend on the task and your tastes. Will there be a lot of points, will it be necessary to scale the graph on the go, etc.

V
vanyamba-electronics, 2019-01-29
@vanyamba-electronics

You might find the Arduino Host Client in C article helpful . It explains how to set up a COM port to communicate with the Arduino.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question