Go Down

Topic: Library compilation problem (Read 1 time) previous topic - next topic

PaulS

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

AWOL

#16
Jun 14, 2011, 11:43 am Last Edit: Jun 14, 2011, 11:46 am by AWOL Reason: 1
...or for that matter, the private variables pval1 etc.

Edit: ..or "count" or "val"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

alkopop79

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);
   }
}




PaulS

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

AWOL

Code: [Select]
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)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

alkopop79

It should return the count and val variables. At this point the library prints these variables which is fine for my project but if I want to use it in the future, I might want the function to return val and count. Does that make sense?

AWOL

Quote
Does that make sense?
Returning "val" does, but "count" will always be 16.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

C and C++ pass all values, except arrays, by value. So, the arguments to a function are not two-way. Whatever the function does to the variables is lost when the function ends.

C/C++ does have a pass by reference operator (&) that will allow you to pass an address to the function where it can store a value. Pass two reference variables to the function, to get two values.

This is NOT good C++ practice, though. You should have a get function to get each value, and member variables to hold the values.

Go Up