OOP Beginner: Implementing classes into separate files

01 Mar 2013

Hello, I am working on a project utilizing the mbed and I am new to C++ and OOP. I think you'll understand what I am trying to do by looking at the code. I think my issue is a lack of understanding regarding the proper initialization of classes and their members.

On a side note I would eventually like to incorporate Threads to handle the timing of certain functions so any advice in that direction would be very helpful.

Any assistance, even in the way of tutorials, etc would be very much appreciated.

Edit: I was able to fix my code below... not too sure what the issue was. - I moved the constructor up to be first under public. - I removed the ()'s from the AnalogIn declaration. - I also changed the define statement of the file to match the actual case of the letters in the file name.

I would still be very interested in ideas regarding Threads or other ways to incorporate timing and a real-time clock (for timestamps) into this system. The rough idea I have is - sample every 5 seconds and store, average again after 5 minutes and log (will be to external SD).

Here is the code:


#include "mbed.h"

#define maxInput 3.3
#define totalSamples 50
#define samplesPerMinute 12

class PowerLogger

    AnalogIn voltageIn(), currentIn();

    typedef float pData_t; 

    struct pData {
        pData_t voltage;
        pData_t current;
        pData_t voltageSamples[totalSamples];
        pData_t currentSamples[totalSamples];

    struct pData pMinute[samplesPerMinute];

    PowerLogger(PinName voltagePin, PinName currentPin);  
    void collectSamples(unsigned char logNumber);



The starred spots below are where I am having trouble.

#include "PowerLogger.h"
#include "mbed.h"

PowerLogger::PowerLogger(PinName voltagePin, PinName currentPin) **: voltageIn(voltagePin), currentIn(currentPin)**


void PowerLogger::collectSamples(unsigned char logNumber)
    for (int i = 0; i < totalSamples; i++) {
        pMinute[logNumber].voltageSamples[i] = **voltageIn.read();**
        pMinute[logNumber].currentSamples[i] = **currentIn.read();**

    for (int i = 0; i < totalSamples; i++) {
        pMinute[logNumber].voltage += pMinute[logNumber].voltageSamples[i];
        pMinute[logNumber].current += pMinute[logNumber].currentSamples[i];

    pMinute[logNumber].voltage /= totalSamples;
    pMinute[logNumber].current /= totalSamples;

Thank you.

01 Mar 2013

What the best way for your case is is hard to tell, it also depends on personal preferences. Personally I am not a big fan of RTOS, because it isn't as clear what the device is doing with RTOS, but I am probably biased.

I assume you use the LPC1768 mbed? That has an RTC built in, so you can just read the time from the RTC. If you want to do something every 5 seconds you can create a ticker object that generates an interrupt every 5 seconds. The RTC can also create interrupts, although they are probably a bit more limitted. They arent included in the standard mbed interrupts, but a bit ago I wrote a library to use them:

Import libraryRTC

Allows easy usage of the LPC1768 RTC interrupts

02 Mar 2013

most small micro applications don't need an RTOS. A cooperative scheduler does fine, usually. And that's like 50 lines of code.