// The Servo library comes with the IDE #include // Joystick: two axes and its push button const int JOY_X = A2; const int JOY_Y = A1; const int JOY_BUTTON = A0; // The servos' orange (signal) wires const int PAN_PIN = 10; const int TILT_PIN = 9; // Angle limits: narrow them if the head // catches on something or strains const int PAN_MIN = 0; const int PAN_MAX = 180; const int TILT_MIN = 20; const int TILT_MAX = 160; const int CENTER = 90; // Wobble this small at the middle is // ignored const int DEAD_ZONE = 60; // Fastest turn (degrees per second) const float MAX_SPEED = 90.0; // If it turns against the way you push, // set the matching one to true const bool INVERT_PAN = false; const bool INVERT_TILT = false; Servo pan; Servo tilt; float panAngle = CENTER; float tiltAngle = CENTER; // Middle readings with the stick let go int centerX = 512; int centerY = 512; // A press sends it back to the middle bool homing = false; unsigned long lastStep = 0; unsigned long lastPrint = 0; int shownPan = -1; int shownTilt = -1; int printedPan = -1; int printedTilt = -1; // How far the stick is pushed: -1 to 1, // 0 inside the dead zone float stick(int pin, int center) { int offset = analogRead(pin) - center; if (abs(offset) <= DEAD_ZONE) return 0; float range = 512 - DEAD_ZONE; float amount = (abs(offset) - DEAD_ZONE) / range; if (amount > 1) amount = 1; return offset > 0 ? amount : -amount; } // Moves the angle at most "step" degrees // towards the target float approach(float angle, float target, float step) { if (angle < target) return min(angle + step, target); return max(angle - step, target); } void setup() { Serial.begin(9600); pinMode(JOY_BUTTON, INPUT_PULLUP); // Each stick's middle is a little off: // measured at start (hands off) long sumX = 0; long sumY = 0; for (int i = 0; i < 16; i++) { sumX += analogRead(JOY_X); sumY += analogRead(JOY_Y); delay(5); } centerX = sumX / 16; centerY = sumY / 16; // A held stick would read wrong if (abs(centerX - 512) > 150) centerX = 512; if (abs(centerY - 512) > 150) centerY = 512; pan.attach(PAN_PIN); tilt.attach(TILT_PIN); pan.write(CENTER); tilt.write(CENTER); Serial.println("Ready: push the stick, press it to go back to the middle."); lastStep = millis(); } void loop() { unsigned long now = millis(); // 50 steps a second if (now - lastStep < 20) return; float seconds = (now - lastStep) / 1000.0; lastStep = now; float x = stick(JOY_X, centerX); float y = stick(JOY_Y, centerY); if (INVERT_PAN) x = -x; if (INVERT_TILT) y = -y; if (digitalRead(JOY_BUTTON) == LOW) homing = true; // Pushing the stick stops the way home if (x != 0 || y != 0) homing = false; float step = MAX_SPEED * seconds; if (homing) { panAngle = approach(panAngle, CENTER, step); tiltAngle = approach(tiltAngle, CENTER, step); if (panAngle == CENTER && tiltAngle == CENTER) homing = false; } else { // Pushed further, it turns faster panAngle = constrain(panAngle + x * step, PAN_MIN, PAN_MAX); tiltAngle = constrain(tiltAngle + y * step, TILT_MIN, TILT_MAX); } // Servos are written only on a change int panNow = round(panAngle); int tiltNow = round(tiltAngle); if (panNow != shownPan || tiltNow != shownTilt) { pan.write(panNow); tilt.write(tiltNow); shownPan = panNow; shownTilt = tiltNow; } // Angles at most every 0.2 s, and the // last one always bool changed = shownPan != printedPan || shownTilt != printedTilt; if (changed && now - lastPrint >= 200) { lastPrint = now; printedPan = shownPan; printedTilt = shownTilt; Serial.print("Pan: "); Serial.print(shownPan); Serial.print("° Tilt: "); Serial.print(shownTilt); Serial.println("°"); } }