So you want to adjust the start angle (0 now) and the finish angle (180 now) and specify the delay? And once started sweeps continuously? The changes made through the Serial port?
Here is a sketch that is a modification of the sketch in reply #5.
The syntax for this sketch is <delay time, start position, end position>.
#include <Servo.h>
Servo servo;
const byte numChars = 12;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
unsigned long delayTime = 15; // servo sweep delay time
int servoStartPosition = 0;
int servoEndPosition = 180;
boolean newData = false;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
servo.write(90);
servo.attach(2);
}
void loop()
{ // run over and over
recvWithStartEndMarkers();
if (newData == true)
{
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
servoSweep(delayTime, servoStartPosition, servoEndPosition);
}
void servoSweep(int delayT, int startPos, int endPos)
{
for (int pos = startPos; pos <= endPos; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(delayT); // waits 15ms for the servo to reach the position
}
for (int pos = endPos; pos >= startPos; pos -= 1) { // goes from 180 degrees to 0 degrees
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(delayT); // waits 15ms for the servo to reach the position
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false)
{
rc = Serial.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker)
{
recvInProgress = true;
}
}
}
//============
void parseData()
{ // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // get the first part - the delay
delayTime = atol(strtokIndx);
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
servoStartPosition = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
servoEndPosition = atoi(strtokIndx);
}
//============
void showParsedData()
{
Serial.print("delay time ");
Serial.println(delayTime);
Serial.print("servo starting position ");
Serial.println(servoStartPosition);
Serial.print("servo ending position ");
Serial.println(servoEndPosition);
}