Cannibalising the sketch from Adafruit’s multitasking tutorial (A classy solution | Multi-tasking the Arduino - Part 1 | Adafruit Learning System) I have been trying to write a sketch where I can control the speed of a servo using the same technique. But there are a few things I still don’t understand
// Using one pot to independently control the speed of one servo - no longer in the loop
// Next step - add another servo with independent speed control
#include <Servo.h>
const int potPin = A0;
int potVal;
int servoInterval;
int pos; // current servo position
int increment = 1 ; // increment to move for each interval
unsigned long lastUpdate; // last update of position
Servo servo; // the servo
class Sweeper
{
public:
Sweeper(int pointless) { // WHAT DO I DO HERE???
}
void Attach(int servoPin) {
servo.attach(servoPin);
}
void Detach() {
servo.detach();
}
void Update() {
potVal = analogRead(potPin);
//map analog values to milliseconds
servoInterval = map(potVal, 0, 1023, 100, 3);
if ((millis() - lastUpdate) > servoInterval) // time to update
{ lastUpdate = millis();
pos += increment; // pos = pos + increment
servo.write(pos); // tell the servo to move to pos
if ((pos >= 171) || (pos <= 0)) //end of sweep. "||" means OR
{
// reverse direction
increment = -increment; // assign negative value to variable increment
}
}
}
};
Sweeper servo1(0); // What is being addressed here??? I am declaring the servos but do I need the ()???
Sweeper servo2(0);
void setup()
{
// assign pins to servos
servo1.Attach(9); //servo1 isn't attaching...
servo2.Attach(10);
Serial.begin(250000); //High baud rate due to low interval value on servo.
}
void loop() {
servo1.Update();
servo2.Update(); //if this is commented out it makes no difference
Serial.print("potVal: ");
Serial.print(potVal);
Serial.print(", servoInterval: ");
Serial.println(servoInterval);
}
Before I can go onto my next task I obviously need to get this working, but more importantly understand what’s going on. The Arduino library tutorial (Arduino - LibraryTutorial) is pretty good but it doesn’t really answer my queries.
There are two problems I am having:
A) I cannot get servo1 and servo2 to run at the same time - no matter what PWM pins I assign them and what number I give them (could be servox & servoy even) it only ever runs one servo. Not only that but I can comment out "servo2.Update() and it will still run a servo off pin10 albeit slightly slower (is that useful to know…? Is the Update function running twice through one pin?) I cannot source the problem for this. As the Adafruit sketch works fine on the same circuit!
B) Class & Constructor syntax - you may notice at the top of the class:
public:
Sweeper(int pointless) { // WHAT DO I DO HERE???
}
and then later on:
Sweeper servo1(0); // What is being addressed here??? I am declaring the servos but do I need the ()???
The only reason these are here is to get everything else to work. This is clearly wrong but I do not know how to create the constructor without creating a useless int variable after the class - can someone help me out here?
Much appreciated - I am somewhat at a loss.