Library compilation problem

Foolishly I’ve been trying to write a multiplexer library but had problems when compiling. The original, working sketch:

int count;
int pinval;

int cpin1=6;
int cpin2=7;
int cpin3=8;
int cpin4=9;

int a;
int b;
int c;
int d;

void setup(){
Serial.begin(9600);
pinMode(cpin1, OUTPUT);
pinMode(cpin2, OUTPUT);
pinMode(cpin3, OUTPUT);
pinMode(cpin4, OUTPUT);
}
void loop(){
muxi();
}
void muxi(){
for(count=0; count<=15; count++){
a = count & 0x01;
b = (count>>1) & 0x01;
c = (count>>2) & 0x01;
d = (count>>3) & 0x01;
digitalWrite(cpin1, a);
digitalWrite(cpin2, b);
digitalWrite(cpin3, c);
digitalWrite(cpin4, d);
int pinval=analogRead(0);
Serial.print(count);
Serial.print("-");
Serial.println(pinval);
delay(300);
}
}

Then created a class from it. The header file:

#ifndef Multiplexer_h
#define Multiplexer_h

#include “WProgram.h”

class Multiplexer
{
public:
Multiplexer(int cp1,int cp2,int cp3,int cp4, int apin); // apin being the analog pin
void muxi();
private:
int _val; //value from analog input
int pval1;
int pval2;
int pval3;
int pval4;
int count;
};

#endif

… and the source file:

#include “multiplexer.h”
#include “WProgram.h”

Multiplexer::Multiplexer(int cp1,int cp2,int cp3,int cp4,int apin){
pinMode(cp1, OUTPUT);
pinMode(cp2, OUTPUT);
pinMode(cp3, OUTPUT);
pinMode(cp4, OUTPUT);
}

void Multiplexer::Muxi(){
for(int count=0; int count<=15; int count++){
pval1 = count & 0x01;
pval2 = (count>>1) & 0x01;
pval3 = (count>>2) & 0x01;
pval4 = (count>>3) & 0x01;
digitalWrite(cpin1, pval1);
digitalWrite(cpin2, pval2);
digitalWrite(cpin3, pval3);
digitalWrite(cpin4, pval4);
int val=analogRead(apin);
Serial.print(count);
Serial.print("-");
Serial.println(val);
delay(300);
}
}

Finally I tried to import the library into a new sketch:

#include <multiplexer.h>

Multiplexer multiplexer(6,7,8,9,0);

void setup(){
Serial.begin(9600);
}
void loop(){
multiplexer.muxi();
}

…and got the following error message:

/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp:20: error: no ‘void Multiplexer::Muxi()’ member function declared in class ‘Multiplexer’

Any ideas where did I go wrong? Thanks for any help in advance!

class Multiplexer
{
public:
  Multiplexer(int cp1,int cp2,int cp3,int cp4, int apin); // apin being the analog pin
    void muxi();
void Multiplexer::Muxi(){

If you define a method called muxi(), you must implement a method called muxi(), not Muxi().

Doesn't your class need to memorise the mux pin numbers?

AWOL: Doesn't your class need to memorise the mux pin numbers?

I guess if I have the mux pin numbers as variables in the constructor, I can define them in the sketch. What are you suggesting?

I'm suggesting you store them in your class, otherwise "Muxi" (or "muxi") is going to have a difficult time doing its "digitalRead"s.

Found the error...

AWOL: I'm suggesting you store them in your class, otherwise "Muxi" (or "muxi") is going to have a difficult time doing its "digitalRead"s.

Do you mean use hard coded values instead of variables for the pins?

I found an error and corrected it. Now the cpp looks like this:

#include “multiplexer.h”
#include “WProgram.h”

Multiplexer::Multiplexer(int cp1,int cp2,int cp3,int cp4,int apin){
pinMode(cp1, OUTPUT);
pinMode(cp2, OUTPUT);
pinMode(cp3, OUTPUT);
pinMode(cp4, OUTPUT);
}

void Multiplexer::muxi(){
for(int count=0; count<=15; count++){
pval1 = count & 0x01;
pval2 = (count>>1) & 0x01;
pval3 = (count>>2) & 0x01;
pval4 = (count>>3) & 0x01;
digitalWrite(int cp1, pval1);
digitalWrite(int cp2, pval2);
digitalWrite(int cp3, pval3);
digitalWrite(int cp4, pval4);
int val=analogRead(int apin);
Serial.print(count);
Serial.print("-");
Serial.println(val);
delay(300);
}
}

… and yet I got errors when compiling:

/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp: In member function ‘void Multiplexer::muxi()’:
/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp:27: error: expected primary-expression before ‘int’
/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp:28: error: expected primary-expression before ‘int’
/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp:29: error: expected primary-expression before ‘int’
/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp:30: error: expected primary-expression before ‘int’

…which means the trouble is here:

digitalWrite(int cp1, pval1);
digitalWrite(int cp2, pval2);
digitalWrite(int cp3, pval3);
digitalWrite(int cp4, pval4);

I have no clue what the problem is… Your valuable help is still appreciated!

digitalWrite(int cp1, pval1);

Your constructor uses the parameters cp1, etc to set the "pinMode"s, but you don't save the values of "cp1" etc, so they aren't visible inside "muxi". You can't declare "cp1" inside a call to "digitalWrite.

So, make some class member variables (they could be "byte" type), called _cp1, _cp2 etc, and assign them in your constructor.

You’re amazing! Thanks! Will try it!

In fact, you're almost there, because the "pval" variables don't need to be class members, because you only ever use them in "muxi", where you could make them automatics. Just rename (and change the data type to "byte" to save memory) them and assign in the constructor.

I changed it with not much success:

#include “multiplexer.h”
#include “WProgram.h”

Multiplexer::Multiplexer(int cp1,int cp2,int cp3,int cp4,int apin)
{
pinMode(cp1, OUTPUT);
pinMode(cp2, OUTPUT);
pinMode(cp3, OUTPUT);
pinMode(cp4, OUTPUT);
_cp1=cp1;
_cp2=cp2;
_cp3=cp3;
_cp4=cp4;
}

void Multiplexer::muxi(){
for(int count=0; count<=15; count++){
byte pval1 = count & 0x01;
byte pval2 = (count>>1) & 0x01;
byte pval3 = (count>>2) & 0x01;
byte pval4 = (count>>3) & 0x01;
digitalWrite(_cp1, pval1);
digitalWrite(_cp2, pval2);
digitalWrite(_cp3, pval3);
digitalWrite(_cp4, pval4);
int val=analogRead(int apin);
Serial.print(count);
Serial.print("-");
Serial.println(val);
delay(300);
}
}

Error:

/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp: In constructor ‘Multiplexer::Multiplexer(int, int, int, int, int)’:
/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp:19: error: ‘_cp1’ was not declared in this scope
/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp:20: error: ‘_cp2’ was not declared in this scope
/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp:21: error: ‘_cp3’ was not declared in this scope
/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp:22: error: ‘_cp4’ was not declared in this scope
/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp: In member function ‘void Multiplexer::muxi()’:
/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp:31: error: ‘_cp1’ was not declared in this scope
/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp:32: error: ‘_cp2’ was not declared in this scope
/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp:33: error: ‘_cp3’ was not declared in this scope
/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp:34: error: ‘_cp4’ was not declared in this scope
/Applications/Arduino.app/Contents/Resources/Java/libraries/multiplexer/multiplexer.cpp:35: error: expected primary-expression before ‘int’

I’m beyond confused.

You didn't show us the declarations of "_cp1" etc in "multiplexer.h"

Hah, sorted! Thanks for all your valuable help! I haven’t had time to test it with the board but it compiles with no probs:

header:

#ifndef Multiplexer_h
#define Multiplexer_h

#include “WProgram.h”

class Multiplexer
{
public:
Multiplexer(int cp1,int cp2,int cp3,int cp4, int apin); // apin being the analog pin
void muxi();
int cp1;
int cp2;
int cp3;
int cp4;
private:
int _val; //value from analog input
int pval1;
int pval2;
int pval3;
int pval4;
int count;
int _cp1;
int _cp2;
int _cp3;
int _cp4;
int _apin;

};

#endif

cpp:

#include “multiplexer.h”
#include “WProgram.h”

Multiplexer::Multiplexer(int cp1,int cp2,int cp3,int cp4,int apin)
{
pinMode(cp1, OUTPUT);
pinMode(cp2, OUTPUT);
pinMode(cp3, OUTPUT);
pinMode(cp4, OUTPUT);
_cp1=cp1;
_cp2=cp2;
_cp3=cp3;
_cp4=cp4;
_apin=apin;
}

void Multiplexer::muxi(){
for(int count=0; count<=15; count++){
byte pval1 = count & 0x01;
byte pval2 = (count>>1) & 0x01;
byte pval3 = (count>>2) & 0x01;
byte pval4 = (count>>3) & 0x01;
digitalWrite(_cp1, pval1);
digitalWrite(_cp2, pval2);
digitalWrite(_cp3, pval3);
digitalWrite(_cp4, pval4);
int val=analogRead(_apin);
Serial.print(count);
Serial.print("-");
Serial.println(val);
delay(300);
}
}

Again, thanks for all the help! I have learnt a lot about classes in C++ in the last few days!

Why are cp1, cp2, cp3 and cp4 public members? You don't appear to ever use them.

...or for that matter, the private variables pval1 etc.

Edit: ..or "count" or "val"

Corrected! I wonder what if I want the function to return two variables: the pin index and the analog value? Can I do this:

cpp

#include “multiplexer2.h”
#include “WProgram.h”

Multiplexer::Multiplexer(int cp1,int cp2,int cp3,int cp4,int apin)
{
pinMode(cp1, OUTPUT);
pinMode(cp2, OUTPUT);
pinMode(cp3, OUTPUT);
pinMode(cp4, OUTPUT);
_cp1=cp1;
_cp2=cp2;
_cp3=cp3;
_cp4=cp4;
_apin=apin;
}

void Multiplexer::muxi(int count, int val){
for(int count=0; count<=15; count++){
byte pval1 = count & 0x01;
byte pval2 = (count>>1) & 0x01;
byte pval3 = (count>>2) & 0x01;
byte pval4 = (count>>3) & 0x01;
digitalWrite(_cp1, pval1);
digitalWrite(_cp2, pval2);
digitalWrite(_cp3, pval3);
digitalWrite(_cp4, pval4);
int val=analogRead(_apin);
Serial.print(count);
Serial.print("-");
Serial.println(val);
delay(300);
}
}

I wonder what if I want the function to return two variables: the pin index and the analog value?

To accept two arguments or to return two values? Your one function doesn’t currently return any values.

void Multiplexer::muxi(int count, int val){
   for(int count=0; count<=15; count++){

Two "count"s?

What is it you’re trying to return?
(unless you return a structure, you can normally only return one value. Or you could use references)