Arduino Servo Sweep with speed, left and right angle control

Thread Starter

OldGeeza

Joined Dec 15, 2023
6
OK I'm a total noob at Arduino programming and have this project I would love to see working, I have the basic servo sweep code from the Arduino examples (below) but need to add three potentiometers to adjust the left angle sweep, the right angle sweep and the sweep speed, can anyone help this 64 year old newb at his new hobby?

MODERATOR NOTE: Welcome to AAC. Please use [CODE] tags when posting code. There is an editor in the ••• dropdown of the toolbar, choose the </> Code option. I‘ve done it for you this time, and you can see why we prefer to have posters use them.

Arduino Servo Sweep Demo:
#include <Servo.h>
Servo myservo;
int pos = 90;

void setup()
{
myservo.attach(9);
}

void loop()
{
// *** low number left angle - high number right angle I need a pot to adjust the left angle between 77 and 90 degrees and adjust the right angle between 90 and 112 degrees ***
for(pos = 77; pos <= 112; pos += 1)
{
myservo.write(pos);

// *** sweep speed I need a pot to change the delay between 20 and 40 ***
delay(20);
}

for(pos = 112; pos>=77; pos-=1)
{
myservo.write(pos);
delay(20);
}
}
Thanks in advance for any help
 
Last edited by a moderator:

ericgibbs

Joined Jan 29, 2010
18,997
hi OG,
Just in case you have not worked with the Analogue inputs, set up this demo program, use the IDE to display the pot values.

E
C-like:
// OG Servo1 15 Dec 23
//  Analog Input

int LeftPin = A0;    // the potentiometers
int RightPin = A1;
int SpeedPin = A2;

int LeftValue = 0;  // variable to store the value coming from the pot
int RightValue = 0;
int SpeedValue = 0;

void setup() {
  Serial.begin(9600); // for test Only
  Serial.println("Ready");
}

void loop() {
  // read the value from the potentiometers
  LeftValue = analogRead(LeftPin);
  RightValue = analogRead(RightPin);
  SpeedValue = analogRead(SpeedPin);

  Serial.println(LeftValue);
  Serial.println(RightValue);
  Serial.println(SpeedValue);

  byte mapLeft = map(LeftValue, 0, 1023, 0, 112);
  byte mapRight = map(RightValue, 0, 1023, 0, 112);
  byte mapSpeed = map(SpeedValue, 0, 1023, 0, 100);

  Serial.println(mapLeft);
  Serial.println(mapRight);
  Serial.println(mapSpeed);
  delay(1000);
}
 

Thread Starter

OldGeeza

Joined Dec 15, 2023
6
hi OG,
Just in case you have not worked with the Analogue inputs, set up this demo program, use the IDE to display the pot values.

E
C-like:
// OG Servo1 15 Dec 23
//  Analog Input

int LeftPin = A0;    // the potentiometers
int RightPin = A1;
int SpeedPin = A2;

int LeftValue = 0;  // variable to store the value coming from the pot
int RightValue = 0;
int SpeedValue = 0;

void setup() {
  Serial.begin(9600); // for test Only
  Serial.println("Ready");
}

void loop() {
  // read the value from the potentiometers
  LeftValue = analogRead(LeftPin);
  RightValue = analogRead(RightPin);
  SpeedValue = analogRead(SpeedPin);

  Serial.println(LeftValue);
  Serial.println(RightValue);
  Serial.println(SpeedValue);

  byte mapLeft = map(LeftValue, 0, 1023, 0, 112);
  byte mapRight = map(RightValue, 0, 1023, 0, 112);
  byte mapSpeed = map(SpeedValue, 0, 1023, 0, 100);

  Serial.println(mapLeft);
  Serial.println(mapRight);
  Serial.println(mapSpeed);
  delay(1000);
}
 

Thread Starter

OldGeeza

Joined Dec 15, 2023
6
Yeah I have added all the pots and they all seem to be working OK, I'm getting a read out in the serial monitor, pretty cool
 

ericgibbs

Joined Jan 29, 2010
18,997
Hi OG,
Do you know how to integrate parts of that code into your original Sketch?

You may have noted the 'map' function limits, you can choose 0 through 255 as a limit,
Post your code when ready and I will give it a try on my bench.
E

At 64 you are still a young buck, wait until you are 91 years old like me.:oops:
 

Thread Starter

OldGeeza

Joined Dec 15, 2023
6
91 Blimey! Same age as my Dad, he's 92 next August, wow and I thought I was pushing it with this stuff at 64 haha. As for integrating the code, to be honest I really don't have a clue, I only worked out this afternoon through YouTube videos how to connect the pots up.
 

Thread Starter

OldGeeza

Joined Dec 15, 2023
6
Chap! I done it, I added all the code and now it works exactly as I wanted, you my man are a genius!!!

Code:
// OG Servo1 15 Dec 23
//  Analog Input
#include <Servo.h>
Servo myservo;
int pos = 90;
int LeftPin = A0;    // the potentiometers
int RightPin = A1;
int SpeedPin = A2;

int LeftValue = 0;  // variable to store the value coming from the pot
int RightValue = 0;
int SpeedValue = 0;

void setup() {

  myservo.attach(9);
}

void loop()
{
  // read the value from the potentiometers
  LeftValue = analogRead(LeftPin);
  RightValue = analogRead(RightPin);
  SpeedValue = analogRead(SpeedPin);

  byte mapLeft = map(LeftValue, 0, 1023, 77, 90);
  byte mapRight = map(RightValue, 0, 1023, 90, 112);
  byte mapSpeed = map(SpeedValue, 0, 1023, 20, 40);


for(pos = mapLeft; pos <= mapRight; pos += 1)
{
myservo.write(pos);

// *** sweep speed I need a pot to change the delay between 20 and 40 ***
delay(mapSpeed);

}

for(pos = mapRight; pos>=mapLeft; pos-=1)
{
myservo.write(pos);
delay(mapSpeed);
}


}
Thank you so much for your help and have a brilliant Christmas!!
 

ericgibbs

Joined Jan 29, 2010
18,997
Hi OG,
I will run your code, thanks for posting back.
At the moment I have small problem with my Comm ports, will try it soon [ I hope]

E

Have a Happy Xmas too.!;)
 
Top