SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By derrickadean
#197831
started using this tutorial as a guide

http://paulmurraycbr.github.io/ArduinoT ... nheritance

in my own code cant for the life of me figure out what the problem is

read comments in code thank you for your help

const int fieldCoilPin = 6;


class FieldCoil{
public:

int pwm;
int setPwm() { return pwm; }
void getPwm(int pwmvalue) { pwm = pwmvalue; }

void setup() {


pinMode(fieldCoilPin, OUTPUT);

}
void loop() {

Serial.println(pwm);
analogWrite(fieldCoilPin, pwm);

}
};



class Throtle {
FieldCoil field;
int x;
public:

void setup() {
x = 255;

}

void loop() {

Serial.println();

field.getPwm(x); //trying to pass x back to FieldCoil class
} // but i get nothing back


};



Throtle throtle;
FieldCoil fieldCoil;


void setup(){
Serial.begin(9600);

fieldCoil.setup();
throtle.setup();

}

void loop()
{

//fieldCoil.getPwm(255); //uncomment this and it works
// proves the function does what it suppose to
// but my goal is to do this through the Throtle class
fieldCoil.loop();
throtle.loop();


}
By Valen
#198070
You declare the instance field of class FieldCoil inside the Throtle class. It only exists there. Since you don't do anything with the member-value pwm other than setting it you won't notice the difference.

Besides you got the name of setPwm and getPwm mixed up. 'get' is to return the value of the member variable, 'set' is to assign a value to the member variable.
By Valen
#198071
Also, since x or pwm is always 255 (default in Throtle.setup; and assigned through fieldCoil.getPwm(255) how can you tell that it works or not works. It only works if you can see the code responds properly to changing values.