The x value of the joystick controls how big of an arc that the sweeper covers.
The y value of the joystick controls the speed of the sweeper.
When I change the joystick position, the sweeper changes its tempo and movement correspondingly.
int pos;
int speed;
#include <Servo.h>
#define SERVO_PIN 9
Servo servo;
//copied from servo sweeper sketch
void linear_move(int start, int end, float speed = 60.0)
{
// Specify the number of milliseconds to wait between updates.
const int interval = 20;
// Compute the size of each step in degrees. Note the use of float to capture
// fractional precision. The constant converts speed units from milliseconds
// to seconds: deg/step = (deg/sec) * (sec/msec) * (msec/step)
float step = speed * 0.001 * interval;
// Declare a float variable to hold the current servo angle.
float angle = start;
// Begin a do-loop. This always executes the body at least once, and then
// iterates if the while condition is met.
do {
servo.write(angle); // update the servo output
delay(interval); // pause for the sampling interval
if (end >= start) {
angle += step; // movement in the positive direction
if (angle > end) angle = end;
} else {
angle -= step; // movement in the negative direction
if (angle < end) angle = end;
}
} while (angle != end);
// Update the servo with the exact endpoint before returning.
servo.write(end);
}
void setup() {
Serial.begin(9600);
pos = 90;
speed = 60;
servo.attach(SERVO_PIN);
}
void loop() {
int x = analogRead(A0);
int y = analogRead(A1);
Serial.print(x);
Serial.print(":");
Serial.print(y);
Serial.print("\n");
if (x < 300) {
speed = 30;
} else if (x > 700) {
speed = 120;
} else {
}
if (y < 300) {
pos = 15;
} else if (y > 700) {
pos = 90;
} else {
}
Serial.print(speed);
Serial.print("(s):(p)");
Serial.print(pos);
Serial.print("\n");
linear_move(90 - pos, 90 + pos, speed);
linear_move(90 + pos, 90 - pos, speed);
}
Leave a Reply
You must be logged in to post a comment.