analogWrite not working in function call from another class?

Guessing this is obvious to C++ peeps but it’s baffling me.

I have a class “Bbq” that instantiates another class “Auger” and a function of class “Auger” is called from a function of class “Bbq”. The Auger function calls analogWrite to turn on a motor. When I call bbq.ignite, i know auger.turn is being called and working because Serial1.print will print from it. However, analogWrite will not. I can instantiate Auger by itself and call turn and analogWrite works fine there.

I tried just using the pin code and a value to write instead of passing values for each in and still no results (analogWrite(A6,200) instead of analogWrite(pin,rate)

Any ideas?

Some code:

class Bbq {

  public:
  Auger auger;
  Fan fan;
  
  Bbq(int augerPin, int fanPin);
  
  void ignite();

};
#include "auger.h"
#include "fan.h"
#include "bbq.h"
#include "application.h"

Bbq::Bbq(int augerPin, int fanPin) : auger(augerPin), fan(fanPin){}

void Bbq::ignite() {
    auger.turn(200);
    fan.spin(200);
}
class Auger{
  int pin;

  public:
  Auger(int p);
  void turn(int rate);
};
#include "auger.h"
#include "application.h"

Auger::Auger(int p) { 
    pin = p;
    }

void Auger::turn(int rate = 0){
    // Serial1.print(pin);
    //analogWrite(pin,rate);
    analogWrite(A6,200);
}
int fan_pin     = A5;
int auger_pin   = A6;




Bbq bbq1 (auger_pin,fan_pin);

void setup()
{
    pinMode(fan_pin, OUTPUT);
    pinMode(auger_pin, OUTPUT);
}

void loop()
{
    // analogWrite(16,230);

    bbq1.ignite();

}

Don’t you have to use the “other” syntax for auger and fan in Bbq?

void Bbq::ignite() {
    auger->turn(200);
    fan->spin(200);
}
1 Like

Hi @bko, thanks so much for responding! I changed the code to -> instead of dot notation but i get the following compile error:

error: base operand of '->' has non-pointer type 'Auger'
auger->turn(200);
^

why would the code work to the point where it calls Serial1.print in the same function but not the analogWrite in the next line?

OK sorry I see that the initializers don’t create pointers to the objects but the objects themselves–my bad!

How do you know it is not working? I copied your code and change the Auger::turn() method to do digitalWrite(pin,HIGH) and change the Auger to pin D7 and the little blue LED lights up.

AnalogWrite should create a PWM waveform out so you need a load capacitor to see a voltage value and it can be tricky to measure.

1 Like

The motor will run if i use analogWrite from the tinker app. I have it connected to 12v power with a transistor, diode and rectifier.

you know what, scratch that, now i seem to have an electrical problem. i have two motors connected and one running was dependent on the other starting up. hmmmm. sorry, it was working but maybe i did something weird to one of the transistors.

2 Likes