SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By derrickadean
started using this tutorial as a guide ... 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{

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

void setup() {

pinMode(fieldCoilPin, OUTPUT);

void loop() {

analogWrite(fieldCoilPin, pwm);


class Throtle {
FieldCoil field;
int x;

void setup() {
x = 255;


void loop() {


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


Throtle throtle;
FieldCoil fieldCoil;

void 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

By Valen
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
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.