Introduction
I'm currently working on a complete setup for interfacing Arduino with C++ directly. I'm using it mostly as coding practice, and would like to share it with others for use, and more importantly, to critique or offer better methods.
Using the Code
Construct an ARDUINO object:
ARDUINO Mega("COM7");
Once the object is constructed, it includes several methods directly related to the stock Arduino commands.
Current methods:
void IsConnected()
Checks if the serial port currently connected.
void SetEcho(bool)
Data-sending methods can ask for verification. If set to 0
, all such functions will return 0
.
-
bool pinMode(int pin, bool mode)
Sets pin mode to input or output. 1
= input, 0
= output. Or use PIN_MODE_INPUT
or PIN_MODE_OUTPUT
. Returns 1
if successful.
bool analogWrite(int pin, int value)
Writes a value (0-255) to the selected pin. Returns 1
if successful.
bool digitalWrite(int pin, bool value)
Writes Low or High to the selected pin. Returns 1
if successful.
int analogRead(int pin)
Returns the value (0-1023) of the selected pin.
bool digitalRead(int pin)
Returns the state of the pin.
Points of Interest
This uses the C++ Serial Class supplied by Arduino.cc (included). It uses basic file-print methods and stringstreams
. I created a method in which different ASCII symbols represent different instructions:
[]
represents the start and end of an instruction "packet", and instructs the buffer when to start and stop loading. !
precedes a char that represents which command (a-z) will be run. For instance, !c
is analogWrite
. ()
represent numbers, and can be comma-seperated. Each value is stored in an array, to be used by the function.
So a full command to set pin 15 to half-brightness looks like this:
[ !c(15,128)]
I certainly doubt it's the fastest method out there, but it makes C++ programming and Arduino a piece of cake, and it was really fun to set up.
Issues
- Slow
- Sometimes gets stuck if it misses an echoed value (needs a timeout or sorts)
- Missing most of the commands thus far
Goals
- Faster speed and better reliability
- Complete list of commands
- Create subclasses which represent each Arduino model