Pages: [1]   Go Down
Author Topic: how to build classes  (Read 5113 times)
0 Members and 1 Guest are viewing this topic.
Lisbon . Portugal
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Lisbon . Portugal
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

thnx smiley-wink
Logged

Lisbon . Portugal
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: February 26, 2009, 05:10:24 am by guibot » Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Lisbon . Portugal
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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);
      servo1_control++;
    }
    else if (servo1_control == 1) {
      servoControl(servo1, 1500, 25);
      servo1_control++;
    }
    else {
      servoControl(servo1, 2200, 4);
      servo1_control = 0;
    }
  }

}

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

void servoControl (ServoTimer2 _servoName, int _dest, int _inc) {
  
  // activate the metro for position update
  servoIntervalReset(10);
  
  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(99999);
  }
  
  servoName.write(newPos);
}


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

void servoIntervalReset(int duration) {
  servoControl_ID.interval(duration);
  servoControl_ID.reset();
}
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.print(msg);
   Serial.println(value, DEC);
}
 
void setup() {
  Serial.begin(19200);
  
  for( int i =0; i < NBR_SERVOS; i++){
      Servos[i].attach(servoPins[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]);      
    }
    delay(10);
}
« Last Edit: February 26, 2009, 12:12:05 pm by mem » Logged

Pages: [1]   Go Up
Jump to: