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)