Baby Steps

I'm going to enter the "holy war" for a minute. C++ builds on C, but they are different languages. Arduino code is nominally "C/C++" which means you can use either or a mix of both. Most of the code I see written for Arduino is C-style with use of Structs and nary a Class to be seen. But, tellingly, most of the "libraries" you can get for the hardware are written in C++. I'm going to declare that my allegiance is to the C++ camp. Why? Simply because I want to write OO code. You need classes for that and therefore C is out. Simple. Feel free to take me to task in comments.
>
It's important to start out right. Get into the habit of writing the basic outline of your Arduino code, verifying, uploading and running your code. To that end I've created a couple of projects that are just a skeleton of code from which to begin. This one is a basic one-file project.
void setup()
{
Serial.begin(115200);
Serial.println("setup()");
}

void loop()
{
Serial.println("loop()");
delay(1000);
}
Hardware Required: Arduino board (I'm using a Duemilanove)

So just to prove my point I'm having trouble. I want to upload this skeleton sketch and run it and it's not working. Something's up with my set-up. COM3 (the Arduino) keeps resetting to COM1 and the baud rate resets to 9,600 every time and I can't get the sketch to run! The Arduino editor keeps crashing. Great. But it kinda proves the point - get the basics sorted out before you do anything complex in your code. Nope I'm still getting garbage out of the serial monitor.

After a reboot (good 'ol Windows!) it's fine. You'd think by Windows 7 64-bit they'd have stuff sorted. Oh well.

My second skeleton includes a class so it has three files.

ClassSketch.pde:
#include "TestClass.h"

void setup()
{
Serial.begin(115200);
Serial.println("setup()");
}

void loop()
{
}

TestClass.h
#ifndef TestClass_h
#define TestClass_h

class TestClass
{
public:
private:
};

#endif

TestClass.cpp
#include "TestClass.h"


It compiles. It uploads. It does nothing. But it's a start. If I wanted to eke out a basic function I would probably do this:


ClassSketchPlus.pde
#include "TestClass.h"

TestClass thing(6);

void setup()
{
Serial.begin(115200);
Serial.println("setup()");
}

void loop()
{
Serial.print("Object's Number: ");
Serial.print(thing.Number());
Serial.println();
delay(1000);
}

TestClass.h
#ifndef TestClass_h
#define TestClass_h

class TestClass
{
public:
TestClass(int newNumber);
int Number();
private:
int itsNumber;
};

#endif

TestClass.cpp
#include "TestClass.h"
TestClass::TestClass(int newNumber)
{
itsNumber = newNumber;
}

int TestClass::Number()
{
return itsNumber;
}


So now I just have to get this class to be something useful. I want a GPS class that can read serial data from the GPS device, validate the incoming text and interpret it into meaningful data; numbers and such.

TODO: GPS Class.

That is all.

Comments