Organizing growing sketch

As my sketch is growing the overview is getting lost.
So i had the idea to move some subroutines to a seperate file and use this as a #include.

As i dit some search on the subject i found a lot about subroutines but not what i am looking fore.
Could someone point me to where to find more information about this.

So i had the idea to move some subroutines to a seperate file and use this as a #include.

Partially right. You would want to create two files - a header file where the function prototypes are stored and a source file where the functions are implemented. You then include the header file.

i found a lot about subroutines but not what i am looking fore.

Too bad.

Could someone point me to where to find more information about this.

Google. Perhaps if you defined what "this" is, we could offer more specific advice.

]
Google. Perhaps if you defined what "this" is, we could offer more specific advice.

As i dont know how these are called i searched for subroutines and header files.
If i look at the libraries there's a lot of extra information in them and i am not aware what to do with that.

Or could i simply place the subroutines in a *h file and include them on top of the sketch?

Or could i simply place the subroutines in a *h file and include them on top of the sketch?

You could, but it isn't recommended, because then everywhere you include the header, you'll get a copy of the function.

Put them in a separate .ccp, and put the function prototype in a header.

AWOL:

Or could i simply place the subroutines in a *h file and include them on top of the sketch?

You could, but it isn't recommended, because then everywhere you include the header, you'll get a copy of the function.

Put them in a separate .ccp, and put the function prototype in a header.

As i am not a C++ programmer is there a section on the arduino website with information how to do this.

Maybe start here?

Ok, as i a complet newbie in this i give it a try as i dont compleet understand the example as i am not using any pin's.
This is the header file:

#ifndef RotorSteering_h
#define RotorSteering_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

class RotorSteering
{
    public:
      void stopAzimuth()
      void stopElevation()
}

#endif

and this is the cpp file:

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#define RotorSteering_h

RotorSteering::RotorSteering()
{
}

void RotorSteering::stopAzimuth()
{
  analogWrite(_azimuthPWM, 0);
  digitalWrite(_azimuthDIR, LOW);
  _azimuthMove = false;  
}

void RotorSteering::stopElevation()
{
  analogWrite(_elevationPWM, 0);
  digitalWrite(_elevationDIR, LOW);
  _elevationMove = false;  
}

My setup:

void setup()
{
  // initialize rotor control pins as outputs
  pinMode(_elevationDIR, OUTPUT);
  pinMode(_azimuthDIR, OUTPUT);

  // set all the rotor control outputs low
  digitalWrite(_elevationDIR, LOW);
  digitalWrite(_azimuthDIR, LOW);

If i run this i get a error '_elevationMove' was not declared in this scope
As i am nog a C++ programmer i realy dont know how to solve it

i dont compleet understand the example as i am not using any pin's.

Yes, you are:

  analogWrite(_azimuthPWM, 0);
  digitalWrite(_azimuthDIR, LOW);

The first argument is the pin number to affect.

If i run this i get a error '_elevationMove' was not declared in this scope

So, where is it defined? I don't see it declared in the class definition (there are many other variables missing, too).

These are the pin def's
// pins
const byte _azimuthPWM = 10; // azimuth rotor left control line
const byte _azimuthDIR = 12; // azimuth rotor right control line
const byte _elevationPWM = 11; // elevation rotor up control line
const byte _elevationDIR = 13; // elevation rotor down control line

These are the pin def's

Where are those statements located? How do you expect the class to know about them? You need to pass values to the constructor, and have it store them in class fields. Class members need to reference those fields, not stuff defined in the sketch that it knows nothing about.

As i stated before i am a complete noob on this, these delclarations are just before setup() in the sketch, as they would normaly be.
As my mother language is dutch, maybe i am not that clear in telling what or how i would say, i.e. translating this into englisch.
Sorry for that.

Your English is fine. Your C++ skills are not.

Your class definition needs to look like:

#ifndef RotorSteering_h
#define RotorSteering_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

class RotorSteering
{
    public:
      RotorSteering(int azPwmPin, int azDirPin, int elPwmPin, int elDirPin);

      void stopAzimuth();
      void stopElevation();

   private:
      int azPWM;
      int azDIR;

      int elPWM;
      int elDir;
};

#endif

Note that I added a bunch of semicolons, too.

Then, you need to add the constructor to the source file:

RotorSteering::RotorSteering(int azPwmPin, int azDirPin, int elPwmPin, int elDirPin)
{
   azPWM = azPwmPin;
   azDIR = azDirPin;
   elPWM = elPwmPin;
   elDIR = elDirPin;
}

Then, you need to change the other methods to use azPWM, azDIR, elPWM, and elDir, instead of _azimuthPWM, _azimuthDIR, _elevationPWM, and _elevationDIR.

[quote author=PaulS link=topic=151402.msg1136955#msg1136955 date=1362060917]
Your C++ skills are not.[/quote] Thats why i am seeking help here.
I now have constructed a file called RotorSteering.cpp:
[codfe]#include <RotorSterring.h>

RotorSteering::RotorSteering(int azPwmPin, int azDirPin, int elPwmPin, int elDirPin)
{
   azPWM = azPwmPin;
   azDIR = azDirPin;
   elPWM = elPwmPin;
   elDIR = elDirPin;
}

void RotorSteering::stopAzimuth()
{
  analogWrite(azPWM, 0);
  digitalWrite(azDIR, LOW);
  _azimuthMove = false;  
}

void RotorSteering::stopElevation()
{
  analogWrite(elPWM, 0);
  digitalWrite(elDIR, LOW);
  _elevationMove = false;  
}

I must be missing something becourse i still get the error:

  • RotorSteering.cpp:1:27: warning: RotorSterring.h: No such file or directory
    RotorSteering.cpp:3: error: 'RotorSteering' has not been declared
    RotorSteering.cpp:3: error: ISO C++ forbids declaration of 'RotorSteering' with no type
    RotorSteering.cpp: In function 'int RotorSteering(int, int, int, int)':
    RotorSteering.cpp:5: error: 'azPWM' was not declared in this scope
    RotorSteering.cpp:6: error: 'azDIR' was not declared in this scope
    RotorSteering.cpp:7: error: 'elPWM' was not declared in this scope
    RotorSteering.cpp:8: error: 'elDIR' was not declared in this scope
    RotorSteering.cpp:9: warning: no return statement in function returning non-void
    RotorSteering.cpp: At global scope:
    RotorSteering.cpp:11: error: 'RotorSteering' is not a class or namespace
    RotorSteering.cpp: In function 'void stopAzimuth()':
    RotorSteering.cpp:13: error: 'azPWM' was not declared in this scope
    RotorSteering.cpp:13: error: 'analogWrite' was not declared in this scope
    RotorSteering.cpp:14: error: 'azDIR' was not declared in this scope
    RotorSteering.cpp:14: error: 'LOW' was not declared in this scope
    RotorSteering.cpp:14: error: 'digitalWrite' was not declared in this scope
    RotorSteering.cpp:15: error: '_azimuthMove' was not declared in this scope
    RotorSteering.cpp: At global scope:
    RotorSteering.cpp:18: error: 'RotorSteering' is not a class or namespace
    RotorSteering.cpp: In function 'void stopElevation()':
    RotorSteering.cpp:20: error: 'elPWM' was not declared in this scope
    RotorSteering.cpp:20: error: 'analogWrite' was not declared in this scope
    RotorSteering.cpp:21: error: 'elDIR' was not declared in this scope
    RotorSteering.cpp:21: error: 'LOW' was not declared in this scope
    RotorSteering.cpp:21: error: 'digitalWrite' was not declared in this scope
    RotorSteering.cpp:22: error: '_elevationMove' was not declared in this scope

But the file RotorSteering.h is in the same directory as the sketch and the cpp file.

RotorSteering != RotorSterring.h

Going back to the original question: When a sketch grows too large there are several things one can do. You already seem to have divided up a potential huge loop() into several functions (which is the C/C++ name for a "subroutine" in other languages).

A simpler "split up" is to place one or more functions in seperate files with the ".pde", ".ino" extension in the same directory as your main file. You need not do anything else - all files are compiled together as one large file. In the IDE each file gets its own tab.

Then there is putting functions in ".cpp" files, they are compiled seperatly, and you need to have common prototypes and defines in ".h" files and #include them if you have them in folders under the library folder

Lastly you can take a bunch of related functions (that use the same data) and put them in a class. You can do that if you spilt the main file or not. It is then usual that such a class is placed in its own ".cpp" file, with the prototype/header in the ".h". This is the most "complicated" way

(Edited an ommission after #include)

Shame on me.
Found a little error in the header file but corrected that.
My cpp file now looks like this:

#include "RotorSteering.h"

RotorSteering::RotorSteering(int azPwmPin, int azDirPin, int elPwmPin, int elDirPin)
{
   azPWM = azPwmPin;
   azDIR = azDirPin;
   elPWM = elPwmPin;
   elDIR = elDirPin;
}

void RotorSteering::stopAzimuth()
{
  analogWrite(azPWM, 0);
  digitalWrite(azDIR, LOW);  
}

void RotorSteering::stopElevation()
{
  analogWrite(elPWM, 0);
  digitalWrite(elDIR, LOW);
}

In the skecth i added this line: RotorSteering Rotor = RotorSteering(_azimuthPWM, _azimuthDIR, _elevationPWM, _elevationDIR);
But i get the following error.

  • GS232Controller180.ino:18:88: warning: RotorSteering.h: No such file or directory
    GS232Controller180:138: error: 'RotorSteering' does not name a type[/i]