using a L298N within a TaskScheduler

Hi There.
I chose this area of the forum because it is motor related… if not the right spot please move.

I’ve managed to create a sample sketch that reads temp sensors, displays temps on an LCD, randomly moves a servo (for now I am in the “how does it work” stage as opposed to the “look what I made” stage) by following Don’t Delay() Use an Arduino Task Scheduler Today! © GPL3+

My next addition was to be a potentiometer controlled pwm fan (using an L298N).
I’ve written a simple sketch that will achieve this control but when I try to transfer it into a task the fan will not run (although it will occasionally “twitch”)

I’ve changed the frequency that the RunFan task is called (from 5ms up to 5000) with no change in behaviour

the “Simple Sketch”

// Motor A connections
int enA = 10;
int in1 = 11;
int in2 = 12;
int SpdMA = 8;


void setup() {
  // put your setup code here, to run once:

  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(SpdMA, INPUT);

  // Turn off motors - Initial state
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);

}

void loop() {
  // put your main code here, to run repeatedly:

 int potValue = analogRead(A0); // Read potentiometer value
  int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value from 0 to 255
  analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin

}

And some portions of the much larger Task Sheduler sketch (file attached below):

/////LARGE CODE GAP HERE


//Class RunFan

class RunFan: public TimedTask
{
  public:
    RunFan(uint8_t _pin, uint32_t fanFreq, Debugger *_ptrDebugger = NULL);
    virtual void run(uint32_t now);
  private:
    uint8_t pin;      // LED pin.
    uint32_t  fanFreq;   //How oftern to move servo
    Debugger *ptrDebugger;  // Pointer to debugger
};

// ***
// *** RunFan Constructor(s)
// ***

RunFan::RunFan(uint8_t _pin, uint32_t _fanFreq, Debugger *_ptrDebugger)
  : TimedTask(millis()),
    pin(_pin),
    fanFreq(_fanFreq),
    ptrDebugger(_ptrDebugger)
{
  pinMode(pin, OUTPUT);     // Set pin for output.
}


// ***
// *** RunFan::run()
// ***
void RunFan::run(uint32_t now)
{

  int potValue = analogRead(A0); // Read potentiometer value
  int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value from 0 to 255
  analogWrite(pin, pwmOutput); // Send PWM signal to L298N Enable
  int pinval = analogRead(pin);
  String MyStr = "Fan set to : " +String(pinval) + "  pin "  + String(potValue) + " in " + fanFreq + " milliseconds.";
  ptrDebugger->debugWrite(MyStr);
  // Run again in the specified number of milliseconds.
  incRunTime(fanFreq);

}

////////////////// LARGE CODE GAP HERE

void loop() {
  // put your main code here, to run repeatedly:


  // ***
  // *** Instantiate the task objects for use by the TaskScheduler
  // ***
  // *** New: If DEBUG is commented, don't instantiate the debugger object.
  // ***
#ifdef DEBUG
  Debugger debugger;
  RunFan RunFan(enA,5, &debugger);
  TempProbes  TempProbes(ONE_WIRE_BUS, RATE_BLINKER_BLINK, deviceCount, &debugger);
  MoveServo MoveServo(9, servoMove, &debugger);
  InterruptHandler interruptHandler(LED_INTERRUPT_RECEIVED, &debugger);

#else

  InterruptHandler interruptHandler(LED_INTERRUPT_RECEIVED);
RunFan RunFan(enA,5, &debugger);
  TempProbes  TempProbes(ONE_WIRE_BUS, RATE_BLINKER_BLINK, deviceCount, &debugger);
  MoveServo MoveServo(9, servoMove, &debugger);
#endif

  // ***
  // *** Create an array of reference addresses to the task objects we just instantiated.
  // ***
  // *** The order matters here.  When the TaskScheduler is running it finds the first task it can
  // *** run--canRun(), runs the task--run(), then returns to the top of the list and starts the
  // *** process again.
  // ***
  // *** New: If DEBUG is commented, don't instantiate the debugger object.
#ifdef DEBUG
  Task *tasks[] = {

    &debugger,
    &interruptHandler,
   
    &TempProbes,
    &MoveServo,
     &RunFan
  };
#else
  Task *tasks[] = {
    &interruptHandler,

    &TempProbes,
    &MoveServo,
    &RunFan
  };
#endif

  //***
  //*** Instantiate the TaskScheduler and fill it with task references.
  //***
  TaskScheduler scheduler(tasks, NUM_TASKS(tasks));

  //GO! Run the scheduler - it never returns.
  scheduler.runTasks();

}

I’ve included a pfd with my wiring and the sketch in full plus the libraries that need to be in the same directory as the sketch.

All help appreciated

Thanks

Scheduler_Testing.2.ino (15.5 KB)

Task.cpp (295 Bytes)

Task.h (2.54 KB)

TaskScheduler.cpp (653 Bytes)

TaskScheduler.h (985 Bytes)

Hi to all.
I have read dragynn; me too, I started using L298N to move a step by step motor, but...
my problem is that the output n. 9 (the 5 V dc I thought to use to turn Arduino on) never puts out the 5 V dc.
So I read the write of dragynn and I shall be very happy to write some help, but now I have the L298N stopped.
I just read that "A non inductive capacitor, usually of 100 nF, must be foreseen between both Vs and Vss, to ground" (quote from a web site).
My question: no voltage comes out from the Vss, just because I didn't mount the capacitors?
For dragynn: certainly I shall try to write some help to you, but now my L298N is completely looked.
As everyone understood, my english is poor,
All help appreciated from me too.
Thanks

dariosermasi,

thanks for looking.

If I run the first sketch using the wiring diagram I attached, the potentiometer (pot) works as expected and the motor speed changes - the pot may be acting as the resistor you mentioned? but I'm not sure.

when running this way the fan (12v DC fan) is noisy "chirps" at low speed but I've seen solutions on other posts I can use to correct

Some additional info:

long term I plan to write a new sketch as part of the "carputer" I am building that will include climate control (thus the temp sensors, servo and fan parts) plus power seat and window controls, interior lighting scenes and a connection to a CAN BUS from my EFI system to display operating parameters like temp, fuel pressure, MAP position etc. all tied to a Rasberry PI (not too ambitious huh?)

lots to learn before that though.

Step one is figure out how things work...

Thanks

dragynn:
dariosermasi,

thanks for looking.

If I run the first sketch using the wiring diagram I attached, the potentiometer (pot) works as expected and the motor speed changes - the pot may be acting as the resistor you mentioned? but I'm not sure.

when running this way the fan (12v DC fan) is noisy "chirps" at low speed but I've seen solutions on other posts I can use to correct

The "chirping" can be caused by the frequency expected by the motor not being correct.
This causes some motors to struggle at low speeds.
The same condition can also apply if there is not enough voltage AND current available

This is why we prefer to see a schematic of your particular setup and the code you are using.

Also see the sticky posts at the top of each section for additional information that may be useful.

Hey ballscrewbob.
Thanks for having a look. I attached a pdf of my schematic that I use to run both sketches (I had to attach the larger one as a file as well).

As I said the first code I posted works fine other than the chirping which I can figure out… the second won’t run the fan at all. (I had to attach it as a file due to the char limit in the forum)

I’m wondering if it is be related to the scheduler code not pulsing the L298N correctly/enough.

I read the entire long “how to post”… from your comment I have to assume I’ve missed something… guess I’ll have to try harder…
Thanks