1

I need to use a servo library in my class.
I read this question - Use object of other class within class and it works fine for my LCD application, but now I need to use a 2D servo array in my class. I made one approach, it worked, but I think it is bad programming approach, what is right way to solve my problem?

  1. My main file

    #include <SoftwareServo.h> 
    SoftwareServo servo[5][5];
    Servolib Servoarray;
    
    void setup(){
      blah blah blah
    
  2. My cpp file

    #include "Vending.h"
    #include "Arduino.h"
    #include <SoftwareServo.h>
    
    Servolib::TurnSevo(){
      for(int row=0;row<5;row++){
        for(int collum=0;collum<5;collum++){
          servo[row][collum].attach(Object[row][collum]);
          servo[row][collum].write(0);
        }
      }
    }
    
  3. my .h file

    #ifndef Vending_h
    #define Vending_h
    #include "Arduino.h"
    #include <SoftwareServo.h>
    
    class Servolib:SoftwareServo{
    public:
      void TurnServo();
    
    private:
      SoftwareServo servo[5][5];
      int Object[5][5]={
        {42,21,20,19,18},
        {37,41,36,38,35},
        {31,33,30,34,39},
        {67,28,66,29,65},
        {56,63,55,64,54}
      };
    };
    #endif
    

So this code worked, but please give me better solution.

2
  • What do you feel is wrong with that, please? I could suggest you use a #define or a const int for your array dimensions, rather than having so-called "magic numbers" in the code, but apart from that it looks fine to me. (Also you spelled column wrong, but really this is nit-picking.) Commented Nov 28, 2016 at 18:00
  • I though i could pass servo object address and work with pointers, similar to this example -> arduino.stackexchange.com/questions/9296/… , but i don't know how to pass 2D array using pointers. Commented Nov 28, 2016 at 18:24

1 Answer 1

0

You can indeed use pointers, but passing multi-dimensional arrays is a bit of a pain in C(++).

There's a pretty good explanation here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.