Recent

Author Topic: Lazarus control Arduino Nano with Enc28J60  (Read 25118 times)

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #15 on: June 24, 2017, 09:50:18 pm »
Now I trying to get small Lazarus program that I create a button for one and another button for off. I think I am not sure how to use serial code and where do we put in Lazarus.
Everything is already in the wiki and it should be beginner friendly. Please try it and report what is not clear so that we can eventually improve wiki. You can find there lazarus example with 2 buttons sending on and off serial commands to Arduino via synapse/synaser serial lib. And there is also Arduino code that you compile in Arduino IDE that responds with lighting LED on/off as a response to these serial commands. You need to have a valid serial connection and find your serial port number to replace COM1 in the example if needed. It is advised to first establish a serial link with Arduino through some simple serial terminal on your pc, and then light LED on and off by typing on and off commands to the terminal. Then, and only after that, when you have established a successful serial communication, you try with Lazarus. That way you go step by step and we know that you have serial working before trying lazarus example.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

microguy

  • New Member
  • *
  • Posts: 49
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #16 on: June 25, 2017, 07:03:48 am »
Avra,

I went to wiki and I am not sure if I got the right place. http://wiki.freepascal.org/Arduino
Click on Minimal Lazarus Synaser serial example. I still can't find sample or instruction how to install it on my Win10. I am very frustrated and can't get it working. You mention there is step by step. I don't see it.

Reid

McStep

  • Newbie
  • Posts: 6
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #17 on: June 25, 2017, 07:58:48 am »
May be this thread will helb you, as it hleped me.
http://forum.lazarus.freepascal.org/index.php?topic=20481.0
The installation procedure is explain in the beginning of that thread.
https://github.com/JurassicPork/TLazSerial
This ist where you can get the lib mentioned. It now works for me. I had some trouble with using chart for my sensor data caming from the nano together with the serial lib of jurassic pork, but in Laz 1.8 everything seems to do what it should.

Stephan

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #18 on: June 25, 2017, 06:14:50 pm »
I went to wiki and I am not sure if I got the right place. http://wiki.freepascal.org/Arduino
Click on Minimal Lazarus Synaser serial example. I still can't find sample or instruction how to install it on my Win10. I am very frustrated and can't get it working. You mention there is step by step. I don't see it.
Wiki says this:
Quote
We will use Synaser from Synapse 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:
Did you follow it? Did you download and install Synapse library into Lazarus? What Lazarus/FPC version you have? Did you create Lazarus application with one form and two buttons? Did you include synaser unit and added Synapse package as a new requirement to your project? If you skipped any of these steps then it is no wonder why you can't make it work. If you don't know how to do any of these steps then you need to learn how to do these things with Lazarus first. Without that you really can't follow wiki instructions. You need to be more descriptive so that we know what is the problem, and be able to assist you.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #19 on: June 25, 2017, 06:17:54 pm »
May be this thread will helb you, as it hleped me.
http://forum.lazarus.freepascal.org/index.php?topic=20481.0
The installation procedure is explain in the beginning of that thread.
https://github.com/JurassicPork/TLazSerial
This ist where you can get the lib mentioned.
No sorry. Wiki has example with Synapse/Synaser library, and not with TLazSerial (which is a fork of synaser btw). That is not the same lib and wiki example will not work with TLazSerial out of the box. It works with Synapse/Synaser and that is what should be used.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

microguy

  • New Member
  • *
  • Posts: 49
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #20 on: June 26, 2017, 12:39:01 am »
Did you follow it? I think so....

Did you download and install Synapse library into Lazarus? Yes I download synapse40.zip create a new folder for synapse  c:\lazarus  (that is where main program is.  download and unzip to c:\lazarus\synapse  I installed package from from c:lazarus\snapse\source\lib\laz_synapse.lpk

What Lazarus/FPC version you have? I have Lazarus IDE v1.6.4

Did you create Lazarus application with one form and two buttons? Yes I did.

Did you include synaser unit? I assume you mean to add synaser.pas on it. I went to project Inspector to add on it.

here is my source codes.

procedure TForm1.Button1Click(Sender: TObject);
var
  ser: TBlockSerial;
begin
  ser := TBlockSerial.Create;
  try
    ser.Connect('COM5'); // write here your USB virtual COM port number
    Sleep(250);
    ser.config(9600, 8, 'N', SB1, False, False);
    ser.SendString('on'); // button 2 should have 'off' here 
  finally
    ser.free;
  end;
end;   

I don't see which pin we can turn it on. Am I missing something?


Reid

microguy

  • New Member
  • *
  • Posts: 49
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #21 on: June 26, 2017, 01:08:58 am »
One more question  I am wondering why not we use tlazserial package. It already have componet. Is it easier to use it than synapse?

what is the difference and pro / con to use it.

Reid


McStep

  • Newbie
  • Posts: 6
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #22 on: June 26, 2017, 05:48:06 am »
Other struggled with the communication too. Big Dan had some success an his blog migth be of interest.
https://bigdanzblog.wordpress.com/2014/11/06/serial-communications-between-a-pc-and-an-arduino-using-lazarus-free-pascal/
Hope it helps.

Stephan

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #23 on: June 26, 2017, 12:13:28 pm »
Did you create Lazarus application with one form and two buttons? Yes I did.
Can you compile without errors? Your button1 seams to have proper OnClick code. Did you type the same code for button2 OnClick event but just changed
Code: Pascal  [Select][+][-]
  1. ser.SendString('on');
to
Code: Pascal  [Select][+][-]
  1. ser.SendString('off');
as code comment says? Is your Arduino really on COM5 as your code shows? If everything compiles then you should have a working application that sends 'on' and 'off' serial commands to COM5 when you press button1 and button2. That is all that your Lazarus application  needs to do in this simple demo.

Quote
I don't see which pin we can turn it on. Am I missing something?
Lazarus application just sends simple 'on' and 'off' commands. Arduino needs to interprete these commands and do some actions. For this you need to compile and download Arduino application into your board. Wiki gives the code and provides link to detailed step by step instructions. You need to confirm that this was a success. If it was, then your Arduino board now has proper firmware. If you attached leds with proper resistors everything is now ready for testing.

Quote
To test it use some terminal software, connect to proper serial port (check that port speed and other settings match Arduino port settings), and type on and off commands. Verify that Arduino led obeys your commands. For all this you can find a nice tutorial here:
http://www.chrisbrand.co.za/2013/06/30/arduino-serial-port/
If your Arduino board lights led on when you type in your terminal 'on' command (without quotes), and lights led off on your 'off' command, then you have a working communication and everything is ready for your lazarus application. Please confirm if you have this step working. If you have, and if you did everything as explained then you should also have a lazarus application buttons working too.

Please report back...
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #24 on: June 26, 2017, 12:27:51 pm »
One more question  I am wondering why not we use tlazserial package. It already have componet. Is it easier to use it than synapse?
what is the difference and pro / con to use it.
Although I have started that wiki page, that is not my property and everyone is free to expand it. If someone wants to explain how to do the same with LazSerial then be my guest. That is the whole purpose of wiki pages. Contributions are simple to add. Anyway, if you search the forum you will be able to find examples of LazSerial and DataPort components and try to adapt them for your purpose if you wish.

The reason why I didn't use visual components is that they are ok for simple use, but sooner or later your GUI will suffer because your application has single thread which is used for GUI updating, mouse response and your serial communication. Since they are all in the same que if one of them is slow (usually serial) then others suffer. This reflects as erratic mouse movement and non responsive application from time to time. This problem is solved by adding a separate communication thread which then syncs with your GUI application when needed. That brings back the responsivity to your application, although is a very big step for beginners. Anyway, when you write a separate communication thread then visual async serial components are not of much help, and sync serial lib like Synapse/Synaser is the right way to go. I hope my reasoning is more clear now.
« Last Edit: June 26, 2017, 12:31:14 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #25 on: June 26, 2017, 12:35:09 pm »
Big Dan had some success an his blog migth be of interest.
His work is already honored at the end of the wiki in 'Resources' section.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

microguy

  • New Member
  • *
  • Posts: 49
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #26 on: June 27, 2017, 04:31:06 am »
When I click on 'on' button or 'off' button. The led went stay on. When I click on 'off' button or 'on button again the led blink off then back to stay on. (fast blink) I couldn't find a way to click off button  to turn led off.

Here is my codes on arduino ide:

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
    }
}

Here is Laz code

procedure TForm1.Button1Click(Sender: TObject);
  var
    ser: TBlockSerial;
  begin
    ser := TBlockSerial.Create;
    try
      ser.Connect('COM5'); // 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;

procedure TForm1.Button2Click(Sender: TObject);
  var
    ser: TBlockSerial;
  begin
    ser := TBlockSerial.Create;
    try
      ser.Connect('COM5'); // 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('off'); // button 2 should have 'off' here
    finally
      ser.free;
    end;
end;

There is no error message on both Arduino IDE and Lazarus.

Am I missing something?  By the way if I unplug USB that connect to Arduino. The Led went off and plug back in and led still off. When I click on off button and led went on and stay on.
 

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #27 on: June 27, 2017, 10:48:26 am »
Can you change "delay(5)" into "delay(20)" in Arduino code and try again (although it should work with both values)? Can you also try to put Sleep(1000) before both "ser.Free" and try again?

You still haven't tried with the terminal. We need that to eliminate if you are having a problem on Lazarus application side or on Arduino side. Try any terminal which can send whole ascii message at once (like HTerm or Bray Terminal), connect it to COM5 and then type 'on' (without quotes) and send to Arduino, and then 'off'. Report if your Arduino led stay on and off after these two commands, or you still have short flash with off command.
« Last Edit: June 27, 2017, 11:03:35 am by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

McStep

  • Newbie
  • Posts: 6
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #28 on: June 27, 2017, 03:51:51 pm »
@avra so you would put the code into the wiki, if I had samples for example from reading sensors (alcohol, CO2 etc)? I would like to keep my stuff on TLazSerial.

@microguy is there a kind of steady reconnect? Arduino Uno restarts on every reconnect I think.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus control Arduino Nano with Enc28J60
« Reply #29 on: June 27, 2017, 05:28:51 pm »
@avra so you would put the code into the wiki, if I had samples for example from reading sensors (alcohol, CO2 etc)? I would like to keep my stuff on TLazSerial.
You login to wiki with your forum login credentials. Then you can edit and expand the wiki. I added basic led on/off example with Synaser. You are free to add sensor examples with TLazSerial.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018