Process commands from the usb port

Hi
I have connected up an HX711 and load cell to my Arduino uno and have it working, however I would like to send commands to the Arduino from my PC to invoke the tare and other functions as needed.

So my question is how do I recieve the command and then use it to invoke the functions which will also need parameters passing to some?

Many thanks for any help.

Well done :sunglasses:

The Arduino Uno creates a (virtual) serial port via the usb. That build-in serial monitor of the Arduino IDE can be used, but also any other serial terminal program, like this one : http://qst.sourceforge.net/
It is even possible to send messages from the command line in Windows.

Start here : http://arduino.cc/en/reference/serial
Try a few examples.

You need to think about how to send data, the "protocol". Just a single line with number and commas or with a special start byte, perhaps a checksum, readable text or binary, and so on.

The examples in serial input basics may be helpful.

If you have an Uno and are using the USB connection to communicate with a PC you will need to communicate with the Bluetooth device using SoftwareSerial - but you may already be doing that.

...R

Hi Peter_n, I think I didn't communicate my issue very well! Sorry.

I can receive in a windows application the text being sent via serial.print() function and I can send text from windows to the Arduino. What I can't see is how I write an event handler for the Arduino to read the data/ serial text and use it to call the hx711 functions.

Hi Robin2, I don't have bluetooth (yet :slight_smile: ) it's serial over usb...

Thank you both for your replies.

Start with this : http://arduino.cc/en/Tutorial/SerialCallResponse

To read an incoming line of text, it can be read into a buffer, or use for example http://arduino.cc/en/Serial/ReadBytesUntil
Some use a sscanf() on that line, or solve the commands with normal code.

Perhaps you can use http://arduino.cc/en/Reference/ParseInt

It is possible to create an Event Handler for Serial data, but you might just as well check Serial.available() in the loop() function.

Please tell us what data is sent to the Arduino, and make a start with the code to read it.

Thanks Peter,
It's understanding the coding approach in my new Arduino, I'm writing a c# windows application to talk to it, so I'm in control of both ends.

I think the links you have given me will help me design what I need.

Many thanks

I_Need_Help2:
Hi Robin2, I don't have bluetooth (yet :slight_smile: ) it's serial over usb...

Sorry. I have no idea why I thought there was Bluetooth involved.

...R

So my question is how do I recieve the command and then use it to invoke the functions which will also need parameters passing to some?

You need to capture the data character string and then do something with the captured data. The below shows a simple capture of sent characters into a String variable, which is then converted to a number for use with servo control.

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    int n = readString.toInt();  //convert readString into a number
    myservo.write(n);
    readString="";
  } 
}

I_Need_Help2:
Hi Robin2, I don't have bluetooth (yet :slight_smile: ) it's serial over usb...

I probably should also have made it clear that the examples in serial input basics apply to any serial input - not just Bluetooth. They were all developed using serial over USB.

...R

Thank you all for your help and examples, I think I have what I need now and will be having a play Monday :slight_smile: