Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35483
Seattle, WA USA
|
 |
« Reply #15 on: June 14, 2011, 04:38:07 am » |
Why are cp1, cp2, cp3 and cp4 public members? You don't appear to ever use them.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19035
I don't think you connected the grounds, Dave.
|
 |
« Reply #16 on: June 14, 2011, 04:43:24 am » |
...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.
|
|
|
|
London
Offline
Full Member
Karma: 0
Posts: 192
Yes, we can (solder)!
|
 |
« Reply #17 on: June 14, 2011, 09:07:09 am » |
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
Brattain Member
Karma: 313
Posts: 35483
Seattle, WA USA
|
 |
« Reply #18 on: June 14, 2011, 09:09:37 am » |
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
Online
Brattain Member
Karma: 137
Posts: 19035
I don't think you connected the grounds, Dave.
|
 |
« Reply #19 on: June 14, 2011, 09:11:45 am » |
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.
|
|
|
|
London
Offline
Full Member
Karma: 0
Posts: 192
Yes, we can (solder)!
|
 |
« Reply #20 on: June 14, 2011, 09:15:52 am » |
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
Online
Brattain Member
Karma: 137
Posts: 19035
I don't think you connected the grounds, Dave.
|
 |
« Reply #21 on: June 14, 2011, 09:27:16 am » |
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.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35483
Seattle, WA USA
|
 |
« Reply #22 on: June 14, 2011, 09:30:20 am » |
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
|
|
|
|
|
|