problam trying oop in arduno

Your source for all things Atmel.

Moderator: phalanx

Post Reply
derrickadean
Posts: 1
Joined: Thu Jan 11, 2018 6:05 am

problam trying oop in arduno

Post by derrickadean » Thu Jan 11, 2018 6:21 am

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


}

Valen
Support Volunteer
Posts: 1628
Joined: Wed Dec 22, 2010 2:05 am

Re: problam trying oop in arduno

Post by Valen » Fri Jan 26, 2018 12:38 pm

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.

Valen
Support Volunteer
Posts: 1628
Joined: Wed Dec 22, 2010 2:05 am

Re: problam trying oop in arduno

Post by Valen » Fri Jan 26, 2018 1:16 pm

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.

Post Reply