How to let a program on your PC command your Arduino?

In the old days I used the printerport of my PC or laptop as an interface to various hardware like an EPROM-emulator. Then the idea rose to let an Arduino take over those tasks.
I wrote a sketch for my Arduino that listens to my PC to hear what bit it should change or what bit it should read and then send the info to the PC. The program on the PC is written in Lazarus (free Delphi clone).

So far so good but things are not stable. But before inventing the wheel again I want to ask what libraries, programs or whatever is available to let the Arduino act like a slave for that program.

Thank you very much for any input!

Kind regards, Ruud Baltissen.

You can use the Processing IDE to develop Java programs that can "talk" to your Arduino. I've been able to make it work.

It seems that Processing is some sort of ancestor of the Arduino : Link

-jim lee

There is at least one library but I have forgotten the name.

Define "not stable".

Have you had a look at Robin's updated serial basics?

Hi Jim,

despite the link to the mainpage which seems to have "examples" in general
Do you have a link handy that directly leads to a demo that does exactly that
communicate with an arduino?

best regards Stefan

So if I understand right you use the USB-connector of the arduino to link to your PC?
Which means you are using a serial connection.
You wrote that "things are not stable" what does that mean? Your lazarus-code crashes after some minutes of operation?
The send and receive data between arduino and PC is unreliable?

best regards Stefan

First file the GUI bit :

/* =========================================================
 * ====                   WARNING                        ===
 * =========================================================
 * The code in this tab has been generated from the GUI form
 * designer and care should be taken when editing this file.
 * Only add/edit code inside the event handlers i.e. only
 * use lines between the matching comment tags. e.g.

 void myBtnEvents(GButton button) { //_CODE_:button1:12356:
     // It is safe to enter your event code here  
 } //_CODE_:button1:12356:
 
 * Do not rename this tab!
 * =========================================================
 */

public void TextOutChanged(GTextField source, GEvent event) { //_CODE_:TextOut:323147:
  //println("textfield3 - GTextField >> GEvent." + event + " @ " + millis());
} //_CODE_:TextOut:323147:

public void sendButtonClick(GButton source, GEvent event) { //_CODE_:sendButton:951386:
  String outStr = TextOut.getText();
  if (outStr!="") {
    sendString(outStr);
    TextOut.setText("");
  }
} //_CODE_:sendButton:951386:

public void outScrChanged(GTextArea source, GEvent event) { //_CODE_:outScr:882004:
  println("outScr - GTextArea >> GEvent." + event + " @ " + millis());
} //_CODE_:outScr:882004:

public void portList_click(GDropList source, GEvent event) { //_CODE_:portList:775264:
  
  if (myPort!=null) {
    myPort.stop();
  }
  int index = source.getSelectedIndex();
  String portName = Serial.list()[index-1];
  myPort = new Serial(this, portName, 57600);
} //_CODE_:portList:775264:



// Create all the GUI controls. 
// autogenerated do not edit
public void createGUI(){
  G4P.messagesEnabled(false);
  G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
  G4P.setMouseOverEnabled(false);
  G4P.setDisplayFont("SCHEME_8", G4P.PLAIN, 12);
  surface.setTitle("Sketch Window");
  TextOut = new GTextField(this, 76, 218, 236, 16, G4P.SCROLLBARS_NONE);
  TextOut.setOpaque(true);
  TextOut.addEventHandler(this, "TextOutChanged");
  sendButton = new GButton(this, 8, 217, 60, 17);
  sendButton.setText("Send");
  sendButton.addEventHandler(this, "sendButtonClick");
  outScr = new GTextArea(this, 8, 28, 300, 185, G4P.SCROLLBARS_VERTICAL_ONLY);
  outScr.setOpaque(false);
  outScr.addEventHandler(this, "outScrChanged");
  portList = new GDropList(this, 8, 6, 300, 176, 10, 10);
  portList.setItems(loadStrings("list_775264"), 0);
  portList.addEventHandler(this, "portList_click");
}

// Variable declarations 
// autogenerated do not edit
GTextField TextOut; 
GButton sendButton; 
GTextArea outScr; 
GDropList portList;

Second file the main program bit :

import g4p_controls.*;
import processing.serial.*;
import java.util.*;

Serial myPort;  // Create a serial port object from Serial class.
String inBuff;  // Create a string buffer for incoming data.

// Method to setup the port listing popup selctor thing.
public void setupPortList(GDropList portList) {
   
  int i;
  List<String> ourPortList;
   
  ourPortList = new ArrayList<String>();  // Create the array/list of strings.
  ourPortList.add("No port selected");    // Preload the first string as the "No Port selected" label.
  i = 0;                                  // Set i, our list index to 0;
  while(i<myPort.list().length) {          // While our listr index is less than the number of serial ports..
    ourPortList.add(myPort.list()[i++]);  // We pop in each serial port name and bump up i.
  }
  portList.setItems(ourPortList,0);        // When we show this, set it to show item 0. (The none selcted text)
}


public void setup(){
  size(320, 240, JAVA2D);
  createGUI();
  customGUI();
  
  // My stuff below this line.
  setupPortList(portList);
}

//Used to send a string to the remote hardware.
public void sendString(String outStr) {

  if (myPort!=null && outStr!=null) {                // If we have a port and we got a string..
    outScr.appendText(outStr);                        // Append the string into the output scrolling text window.
    outScr.appendText("\n");                          // Add a newline char to the screen. We're going to send one anyway.
    myPort.write(outStr);                            // Write the string out the serial port.
    myPort.write('\n');                              // Write out a newline as a end of text flag for the hardware.
  } else {                                          // Didn't get a string or maybe no port?
    outScr.appendText("Error, No port choosen?\n");  // Probably no port selcted, we'll go with that for now.
  }
}


// I guess this is where we loop around over and over. 
public void draw() {
  
  char inByte;
  
  background(200,200,200);              // Setup background color for redraws.
  if (myPort!=null) {                  // If we have a valid serial port..
    if (myPort.available() > 0) {      // If we have a char waiting for us..
      inByte = myPort.readChar();      // read out the byte and save it as inByte.
      switch(inByte) {
        case '\0' : break;                // We're going to ignore null chars for now..
        case '\n' :                       // Newline chars will be flags that the line is done, write it out.
          inBuff += inByte;               // Save off the inByte to our char buffer as before.
          outScr.appendText(inBuff);      // Add our char buffer to the end of the text in our scrolling output window.
          inBuff = "";                    // Dump the text buffer.
        break;
        case '\t' :                        // Tabs will be jumps to coloums of text.
          inBuff = inBuff + ' ';           // Adding a char because you need at least one.
          while(inBuff.length()%10!=0) {    // For now we'll jump to the next colum divisibal by 5.
            inBuff = inBuff + ' ';         // Adding a char.
          }
        break;
        default :                          // Everything else..
          inBuff += inByte;                // We just save it into our char buffer for later.
        break;
      }
    }
  }
}


// Use this method to add additional statements
// to customise the GUI controls
public void customGUI(){

}

Although its in Java its pretty similar to Arduino. Instead of loop() they have draw().

I used a GUI generator for this, it had issues that couldn't be fixed. So that kinda' put the skids on all this.

-jim lee