2

Question completely rewritten

Trying to assess an Arduino project feasibility. New to Arduino... but did a lot of controller programming (assembly), Roboteq (C) and Phidgets (C++) , using various kinds of motors and sensors ; and reading the Arduino documentation makes me feel confident interfacing and programming shouldn't be an issue.

However, before investing time (and some money) in the project, I thought it'd be better to ask here if Arduino is the right choice, considering

  • a non-latching Hall sensor is to be connected to Analog 0, analogRead() to return a [0,1023] value, used as a proximity sensor. This should be straight forward.

Question →

  • while the Arduino runs my program, and is still connected to the PC (USB), is it possible to synchronously or asynchronously send commands from the PC to the Arduino. I.e. can the Arduino poll if there is something coming from USB (synchronous), or is there a way for the PC via USB to have Arduino do something (asynchronously)?

Thank you.

asked Aug 27, 2016 at 7:57
6
  • Why a downvote, very new to Arduino are the questions too obvious? Commented Aug 27, 2016 at 9:13
  • 1
    Because your question is too broad. If you are a beginner, start with simpler projects (blinking LED, control the LED via serial, now try it with a motor, read a sensor and print it to serial, try to combine those projects, ...). After you know how to use do simpler stuff, you can use them together to make more complex projects. And learn to structure your program as a state machine and use millis() instead delay() or else doing multiple stuff at the same time won't work or be buggy. With that, you would be able to ask more specific questions. Commented Aug 27, 2016 at 15:50
  • Completely rewrote the question to simplify the text and go to the point. Commented Aug 29, 2016 at 1:36
  • Yes, this is possible, but you will need to learn about doing non-blocking serial communication on the Arduino, and perhaps on the PC side as well (though you may just be able to use a terminal program there). Getting non-blocking serial right is unlikely to be either easier or harder on Arduino than on most other embedded platforms - it is entirely do-able, but tricky, and there are a lot of mistakes that can make for a system that "works sometimes". The key will be to make few assumptions - don't assume when characters will arrive, or how much time will be between them, etc. Commented Aug 29, 2016 at 4:29
  • 2
    Your classic Arduino is conceptually a serial device. On a board such as the Uno the USB port goes through a USB-serial converter (though implemented in a second MCU). On one such as the Leonardo it actually runs into the application processor, but the default handling still treats it as a serial device. It is true that USB is different - it both has greater latency, but also if used as USB all the way through gives you some framing possibilities missing in legacy serial. These tend to be advanced topics however. Commented Aug 29, 2016 at 4:58

1 Answer 1

3

It's perfectly doable. As Chris Stratton puts it, it's all about implementing non-blocking serial communication:

  • whenever you receive a character, you store it in a buffer
  • whenever you have a complete command in the buffer, you process it and reset the buffer

For a line-oriented protocol, it may look something like:

/* Process incoming characters if any. */
while (Serial.available()) {
 static char buffer[BUF_LENGTH]; // incoming command
 static int length = 0; // command length
 int data = Serial.read();
 if (data == '\r') { // complete command
 buffer[length] = '0円'; // terminate the string
 if (length)
 exec_command(buffer); // process
 length = 0; // reset buffer
 }
 else if (length < BUF_LENGTH - 1)
 buffer[length++] = data; // buffer incoming character
}

For a more complete example, you may want to take a look at this simple Arduino command line interpreter. You connect a terminal emulator to the Arduino at 9600/8N1, type the "help" command and it displays:

mode <pin> <mode>: pinMode()
read <pin>: digitalRead()
aread <pin>: analogRead()
write <pin> <value>: digitalWrite()
awrite <pin> <value>: analogWrite()
echo <value>: set echo off (0) or on (1)

You can use it as a template, replace existing commands or add new ones (the interpreter is just a long if ... else if ...) and – since it's all non-blocking – add whatever you want to loop().

answered Aug 29, 2016 at 7:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.