Pages: 1 [2]   Go Down
Author Topic: Library compilation problem  (Read 1322 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26312
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Edit: ..or "count" or "val"
« Last Edit: June 14, 2011, 04:46:00 am by AWOL » Logged

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

London
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Yes, we can (solder)!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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



Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26312
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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)
Logged

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

London
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Yes, we can (solder)!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26312
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Does that make sense?
Returning "val" does, but "count" will always be 16.
Logged

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: 1 [2]   Go Up
Jump to: