book collections email follower instructable user

Step 4: Coding: Setup Monster Class

Picture of Coding: Setup Monster Class

In order to program our Monster in a way that can easily be built upon by other developers we will be implementing it as a class/library. A class consists of a header file (.h) and a source file (.cpp). The header file defines everything that will be inside of the class, while the source file consists of the actual code implementation.

We will start with the header file:

#ifndef Monstro_h
#define Monstro_h

#include "Arduino.h"

class Monstro {

  public:
    Monstro(int leftForward, int leftBackward, int leftSpeedPin,
        int rightForward, int rightBackward, int rightSpeedPin,
        int trigA, int echoA, int trigB, int echoB);

    // Behavior
    bool Update();
    void Initialize();
    
  private:

};
#endif

All we have done here is set up the header file with a constructor that takes in the pins we will use for interacting with our sensors and drivers. We will be adding to this later on as we introduce each of the constituent components.
The #include Arduino.h statement just ensures that we have access to the constants and types provided by the Arduino language.
We will be using the Update() function to call certain behaviors during the main loop, and the Initialize() function to make sure our sensors and motors are ready for action. More on this in later steps.

Our source file will reflect this header file:

#include "Arduino.h"
#include "monstro.h"

Monstro::Monstro(int leftForward, int leftBackward, int leftSpeedPin,
         int rightForward, int rightBackward, int rightSpeedPin,
         int trigA, int echoA, int trigB, int echoB)
{

}

// Behavior
void Monstro::Initialize() {
}
bool Monstro::Update() {
}


Again, all we have done here is set up the bare bones of the source file, while making sure we also include the Arduino.h reference here, and a reference to our header file so we have access to its definitions as well.