Go Down

Topic: how to build classes (Read 5792 times) previous topic - next topic


I need to learn how to build classes, can someone give me a very basic example of a class implementation, something like "blink an led class". .. something to have as a reference.


Arduino libraries are classes, so you already know how to use them. There is a tutorial on creating an Arduino  class here: http://www.arduino.cc/en/Hacking/LibraryTutorial


Feb 26, 2009, 11:09 am Last Edit: Feb 26, 2009, 11:10 am by guibot Reason: 1
I´ve been following the SOS Morse class, and it doesn´t seem complicated at all.

I am devoloping myClass and I need to call another class wich I will use inside myClass, and I have a bunch of errors.. could you post an example please?


How to do it can depend on the context. Why not post a cut down version of the code


I´m finding hard to post code of what I´m doing, or even ask quentions about it, I´m a bit confused..

So, basically what I would like to do is, to use the function servoControl(), to control all the servos in my sketch.  The way it is now, I can control just one servo.
I can make it control more servos but it will take a lot of code, and I know that if this could become class based it would be easier and very straightforward to use.

I´m using the Metro library to update the servo destination values while they need to be updated.. If you think I should use another library please advice.

Code: [Select]

// this sketch cycles three servos at different rates

// ===========================  Servo library

#include <ServoTimer2.h>  // the servo library

// define the pins for the servos
#define servo1Pin  2

ServoTimer2 servo1;    // declare variables for up to eight servos  
ServoTimer2 servoName;

int actualPos;
int dest;
int inc;

// =========================  Metro library
// http://www.arduino.cc/playground/Code/Metro

#include <Metro.h>
int servo1_duration = 4000;
Metro servo1_metro = Metro(servo1_duration);
int servo1_control = 0;

int servoControl_duration = 99999;
Metro servoControl_ID = Metro(servoControl_duration);

// ============================== Setup()

void setup() {
 servo1.attach(servo1Pin);     // attach a pin to the servos and they will start pulsing

// =============================== Loop()

void loop() {
 if (servoControl_ID.check() == 1) {
   servoControl(servoName, dest, inc);  
 if (servo1_metro.check() == 1) {
   if (servo1_control == 0) {
     servoControl(servo1, 750, 50);
   else if (servo1_control == 1) {
     servoControl(servo1, 1500, 25);
   else {
     servoControl(servo1, 2200, 4);
     servo1_control = 0;


// =============== servoControl()

void servoControl (ServoTimer2 _servoName, int _dest, int _inc) {
 // activate the metro for position update
 servoName = _servoName;
 int val = servoName.read();
 dest = _dest;
 inc = _inc;
 int newPos;
 if (val > dest) {
   newPos = val - inc;
 } else if (val < dest) {
   newPos = val + inc;
 } else {

// ============== servoIntervalReset()
// metro reset function

void servoIntervalReset(int duration) {


Feb 26, 2009, 06:10 pm Last Edit: Feb 26, 2009, 06:12 pm by mem Reason: 1
I am not familiar with metro but it sounds like an array of servos would help so you can control any particular servo by indexing into the array. If this is what you want to do , the sketch below shows how to make an array of servo instances
Code: [Select]
#include <ServoTimer2.h>

#define NBR_SERVOS 8   // this must be between 1 and 8
byte servoPins[NBR_SERVOS] = {2,3,4,5,6,7,8,9};  // pins our servos are attached to
ServoTimer2 Servos[NBR_SERVOS]  ;

int direction[NBR_SERVOS];  

void debug(char * msg, int value){
  Serial.println(value, DEC);

void setup() {
 for( int i =0; i < NBR_SERVOS; i++){
     Servos[i].write( 800 + (i * 200));  // write some test data
     direction[i] = 2;  // start all the servos going

void loop() {
 int pulseWidth;

   //sweep all the servos back and forth
   for( int i=0; i <  NBR_SERVOS; i++){
       pulseWidth = Servos[i].read();        
       if(pulseWidth >= MAX_PULSE_WIDTH){
           direction[i] = -1;
       if (pulseWidth <= MIN_PULSE_WIDTH) {
            direction[i] = 1;    
      Servos[i].write(pulseWidth + direction[i]);      

Go Up