Click here to Skip to main content
Click here to Skip to main content
Go to top

C++ Arduino Class

, 26 Jul 2013
Rate this:
Please Sign up or sign in to vote.
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:

 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)

Share

About the Author

Jonathan Weber

United States United States
No Biography provided

Comments and Discussions

 
SuggestionRather a Tip/Trick PinprofessionalLeroy Gibbs25-Jul-13 22:09 
GeneralRe: Rather a Tip/Trick PinprotectorDaveAuld27-Jul-13 2:00 
GeneralComments Pinmemberpwasser25-Jul-13 20:10 
GeneralRe: Comments [modified] PinmemberJonathan Weber26-Jul-13 6:20 
GeneralRe: Comments PinprofessionalLeroy Gibbs27-Jul-13 7:40 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 26 Jul 2013
Article Copyright 2013 by Jonathan Weber
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid