pointer question [SOLVED]

I’m new to C and I’m sure this is a simple problem. I’ve been reading about pointers, but I still can’t figure out my issue. Perhaps I just need to sleep on it.
In my main program I have:

#include "LimnoPins.h"
byte pinsSet[2]; 
LimnoPins limnopins(pinsSet); // Instantiate class with empty byte array

in LimnoPins.cpp I have

include "LimnoPins.h"
LimnoPins::LimnoPins(byte pinsSet[]) {
  // Set up our input and output pins
  _ps = pinsSet; // Not sure I get this line
  pinsSet[0] = 0; // Wish I could do pinsSet[] = {0,0}
  pinsSet[1] = 0;
  for (int _thisPin = 0; _thisPin < modePinCount; _thisPin++) {
    Serial.print("Setting pin ");
    Serial.print(modeSelectPins[_thisPin]);
    Serial.println(" as OUTPUT set LOW");
    pinMode(modeSelectPins[_thisPin],OUTPUT);
    digitalWrite(modeSelectPins[_thisPin],LOW);
  }
  for (int _thisPin = 0; _thisPin < inputPinCount; _thisPin++) {
    Serial.print("Setting pin ");
    Serial.print(inputPins[_thisPin]);
    Serial.println(" as INPUT pulled HIGH");
    pinMode(inputPins[_thisPin],INPUT);
    // Pullup resistor
    digitalWrite(inputPins[_thisPin],HIGH);
  }
}

and in LimnoPins.h I have

  class LimnoPins
  {
    public:
	  LimnoPins(byte);
	  ~LimnoPins(); // un-necessary destructor
          void evalPins();
	  void readPins();
	  void waitForPinClear(byte inputPin);
	  int evaluateFunct();
	  int evaluateMeasurement();
	  boolean isMeasurement();
	  byte _ps[]; // Again, not sure I get this.
    private:
      // Any private global variables in this class?
  };

The error I get is

Arduino: 1.5.4 (Windows 7), Board: "Arduino Due (Programming Port)"
LimnoTerra:13: error: invalid conversion from 'byte*' to 'byte'
LimnoTerra:13: error: initializing argument 1 of 'LimnoPins::LimnoPins(byte)'

This worked great before I tried to put it in a library. I had all my constants in a header file, but the library has stumped me.

I probably just need an asterix somewhere or perhaps an ampersand.

Full code http://pastebin.com/NSuLjkSq
and http://pastebin.com/svbiYT9E
and http://pastebin.com/rzuDKpTK

The implementation of the constructor, in the source file, shows that it takes an array. The declaration of the constructor, in the header file, shows that it doesn't.

One of them is wrong.

thanks, but if I change the header file to

class LimnoPins
  {
    public:
	  LimnoPins(byte[]);
...

The error is

LimnoPins.cpp: In constructor 'LimnoPins::LimnoPins(byte*)':
LimnoPins.cpp:14: error: incompatible types in assignment of 'byte*' to 'byte [0]'

It seems like this has to do with pointers rather than mismatched data types (though I may be wrong).

The error is

What is line 14? Is it:

  _ps = pinsSet; // Not sure I get this line
	  byte _ps[]; // Again, not sure I get this.

You can't assign one array to another using one assignment statement.

Change this:

class LimnoPins
  {
    public:
	  LimnoPins(byte[]);

to

class LimnoPins
  {
    public:
	  LimnoPins(byte *);

Change

	  byte _ps[]; // Again, not sure I get this.

to

	  byte *_ps;

Then, the constructor implementation will match the declaration, and the assignment will work.

None of this:

    Serial.print("Setting pin ");
    Serial.print(modeSelectPins[_thisPin]);
    Serial.println(" as OUTPUT set LOW");
    pinMode(modeSelectPins[_thisPin],OUTPUT);
    digitalWrite(modeSelectPins[_thisPin],LOW);

belongs in the constructor. When your constructor is called, you don't know that the Serial instance has been created and you don't know that the hardware is ready to be diddled with. By the time setup() is called, and Serial.begin() is called, you do know. Notice that HardwareSerial, of which Serial is an instance, has a begin() method. Your class needs one, too.

Thanks,

I think I’m getting pointers now.
This page was helpful too Classes (I) - C++ Tutorials

So my code now compiles and runs, but doesn’t behave as expected.
It looks like this. LimnoTerra.ino

#include "Arduino.h" // Some sort of work around to define byte array in header files.
#include "LimnoPins.h"

const int LEDpin = 13;
byte pinsSet[2];
LimnoPins limnopins(pinsSet);

LimnoPins.h:

#ifndef LimnoPins_h
  #define LimnoPins_h
  
  #include "Arduino.h"
    class LimnoPins {
        byte *pinsSet[];
      public:
        LimnoPins(byte *);
	~LimnoPins(); // destructor
        void evalPins();
	void readPins();
	void waitForPinClear();
	int evaluateFunct();
	int evaluateMeasurement();
	boolean isMeasurement();
      private:
        // Any private global variables in this class?
        byte * _ps
  };
  
#endif

and LimnoPins.cpp:

#include "Arduino.h"
#include "LimnoPins.h"


//<<constructor>> 
LimnoPins::LimnoPins (byte _ps[]) {
  // This is what's done when this class is instantiated.
  // Set up our input and output pins
  *pinsSet = _ps;
  pinsSet[0] = 0;
  pinsSet[1] = 0;
  for (int _thisPin = 0; _thisPin < modePinCount; _thisPin++) {
    //Serial.print("Setting pin ");
    //Serial.print(modeSelectPins[_thisPin]);
    //Serial.println(" as OUTPUT set LOW");
    pinMode(modeSelectPins[_thisPin],OUTPUT);
    digitalWrite(modeSelectPins[_thisPin],LOW);
  }
  for (int _thisPin = 0; _thisPin < inputPinCount; _thisPin++) {
    //Serial.print("Setting pin ");
    //Serial.print(inputPins[_thisPin]);
    //Serial.println(" as INPUT pulled HIGH");
    pinMode(inputPins[_thisPin],INPUT);
    // Pullup resistor
    digitalWrite(inputPins[_thisPin],HIGH);
  }
}
void LimnoPins::readPins(){
  thisPin = 5
  *pinsSet[1] = thisPin;
  Serial.print("    FOUND PIN ");
  Serial.println(*pinsSet[1]);
  Serial.print(" == ");
  Serial.println(thisPin);
}

if I call readPins() I get
FOUND PIN 0 == 5

Doesn’t this code say to set the memory address pinsSet[1] to the value 5, but when I try to read *pinsSet[1] it is still 0.

To answer my own question, this

void LimnoPins::readPins(){
  thisPin = 5
  *pinsSet[1] = thisPin;
  Serial.print("    FOUND PIN ");
  Serial.println(*pinsSet[1]);
  Serial.print(" == ");
  Serial.println(thisPin);
}

Should be

void LimnoPins::readPins(){
  thisPin = 5
  _ps[1] = thisPin;
  Serial.print("    FOUND PIN ");
  Serial.println(_ps[1]);
  Serial.print(" == ");
  Serial.println(thisPin);
}

It looks like I need to use the private array _ps rather than a pointer to the public array pinsSet

I dont understand why you need it that complex,
but perhaps you don't understand it neither :wink:

For some reason, you have a global array
byte pinsSet[2];
which you supply to the constructor of a "LimnoPins".

I guess, this

class LimnoPins {
     byte *pinsSet;
public: LimnoPins(byte* _ps) { pinsSet = _ps;}
// ...
};

should do what you really (?) want.

void LimnoPins::readPins(){
  thisPin = 5;
  pinsSet[1] = thisPin;
  Serial.print("    FOUND PIN ");
  Serial.println(pinsSet[1]);
  Serial.print(" == ");
  Serial.println(thisPin);
}
        byte *pinsSet[];

This is an array of pointers, not a pointer to an array of values. Not the same thing at all. pinsSet should not be an array.