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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 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.