Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C++
Tip/Trick

C++ Arduino Class

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
26 Jul 2013CPOL2 min read 24.4K   333   4   5
An incomplete C++ class for Arduino

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:

C++
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionRather a Tip/Trick Pin
Marco Bertschi25-Jul-13 22:09
protectorMarco Bertschi25-Jul-13 22:09 
GeneralRe: Rather a Tip/Trick Pin
DaveAuld27-Jul-13 2:00
professionalDaveAuld27-Jul-13 2:00 
GeneralComments Pin
User 5924125-Jul-13 20:10
User 5924125-Jul-13 20:10 
GeneralRe: Comments Pin
Jonathan Weber26-Jul-13 6:20
Jonathan Weber26-Jul-13 6:20 
GeneralRe: Comments Pin
Marco Bertschi27-Jul-13 7:40
protectorMarco Bertschi27-Jul-13 7:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.