PeterH:
I'd use a variable to hold the actual speed being output, and another variable to hold the target speed, and an unsigned long recording the value of millis() last time you changed the actual speed.
Use the technique demonstrated in 'blink without delay' to adjust the output speed periodically. At each adjustment, move the output speed towards the target speed. It is up to you to decide what what interval you want to make these changes, and whether you're going to make a constant small adjustment (resulting in a linear change in speed) or a proportional adjustment (resulting in an exponential change in speed).
If you are interested in more sophisticated behaviour then there are various control algorithms you can use which would enable you to simulate inertia and control the acceleration/decelleration etc.
If you do this right, your code will not include any calls to delay().
Thanks Peter,
I'll start a new sketch and try what you said.
In my current sketch I was finally able to stop motion.
Inputting angles in the throttle works great but when I try inputting angles for the steering, it takes several seconds to start the motors and several seconds to turn them off. The steering is differential.
Not sure if it has to do with the 'pulseIn' command for the auto/manual toggle or something else...
Here's the full sketch I was working on...(disregard the PING sensor data)
/*Main program on the Arduino Mega2560 with auto/manual toggle switch to control robot
with ultrasonic PING sensors, PIR sensors...
*/
//
//
//
#include <Servo.h>
#include <Stepper.h>
#include <KitchenSink.h>
#define N_PINGS 4
const byte pingPins [N_PINGS] = {22, 23, 24, 25}; //These are the pins for the 4 Ping ultrasonic sensors
const int minRanges [N_PINGS] = {16, 16, 24, 18}; // for later. These are the thresholds in inches for the ping sensors (when to stop when object detected)
unsigned int duration;
Servo myservoThrottle; // create servo object to control throttle
Servo myservoSteering; // create servo object to control steering
int autManToggle = 50; // auto manual toggle switch on pin 50
int angleThrottle; // variable to store the servo throttle angle position
int angleSteering; // variable to store the servo steering angle position
void setup()
{
Serial.begin(9600);
//Setup toggle auto/man Switch as an Input:
pinMode(autManToggle, INPUT);
myservoThrottle.attach(52, 1000, 2000); // attaches throttle servo on pin 52 to servo object (min. 1000uS and max. is 2000uS).
myservoSteering.attach(53, 1000, 2000); // attaches steering servo on pin 53 to servo object (min. 1000uS and max. is 2000uS).
}
void loop()
{
duration = pulseIn(autManToggle, HIGH);
{
if(duration > 1700)
{
servoStop();
// had to add these below to stop looping on and off
angleThrottle = 90;
angleSteering = 90;
}
else
{
calcThrottle();
calcSteering();
}
}
}
int calcThrottle()
{
myservoThrottle.write(angleThrottle);
angleThrottle = 130;
//...
//...
}
int calcSteering()
{
myservoSteering.write(angleSteering);
angleSteering = 90;
//...
//...
}
int servoStop()
{
int angle;
for (angle = angleThrottle; angle > 90; angle -= 1)
{
delay(100);
myservoThrottle.write(angle);
}
}
t