United Kingdom
Asked

Push Button Servo Control Advice

Hello everyone,

I am currently searching for a push button solution to control a servo motor. Specifically, I need the servo to move to a designated position when the button is pressed and then return to its original position once the button is released. I've looked extensively but have not found a suitable option.

Any advice or guidance on this setup would be greatly appreciated. Thank you in advance!

Kind regards.


Related Controls Read Digital Vertical Servo

ARC Pro

Upgrade to ARC Pro

Unleash your creativity with the power of easy robot programming using Synthiam ARC Pro

PRO
Canada
#1  

Nice setup - you can do this two ways in ARC. Below I’ll give the recommended, reliable approach (a small JavaScript robot skill that monitors the digital input and moves the servo), plus notes on wiring, debounce, and a quick alternative using the Read Digital control for debugging.

Useful docs:

Recommended method - JavaScript robot skill (poll + edge detection)

  1. Configure your Vertical servo skill first (Project Add robot skill servo Vertical Servo).
    • Open its kebab menu (three lines / gear) set Board/Port and Min/Max positions to match your servo and travel limits.
  2. Add a JavaScript robot skill:
    • Project Add robot skill Scripting JavaScript.
    • Open its kebab menu Edit Script paste the script below.
  3. Change the top config constants for your actual ports and target positions.
  4. Run the script. Pressing the button will move the servo to the press position; releasing returns it.

Example JavaScript (edit the ports/positions at top):

// === CONFIGURE THESE ===
var digitalPort = d2;     // digital input port with the push button (e.g. d2)
var servoPort   = d4;     // servo port for the vertical servo (e.g. d4)
var pressPosition   = 140; // position when button pressed
var releasePosition = 90;  // position when button released (original)
var pollMs = 40;          // poll interval in ms (40ms is fine)
// ========================

setVar("$buttonLast","0"); // store last state to detect edges

while(true) {
  try {
    // Read digital input (returns true/false)
    var isHigh = Digital.get(digitalPort) ? 1 : 0;
    var last   = parseInt(getVar("$buttonLast","0"));

    if (isHigh !== last) {
      // edge detected
      setVar("$buttonLast", isHigh.toString());
      if (isHigh === 1) {
        print("Button pressed -> moving servo to " + pressPosition);
        Servo.setPosition(servoPort, pressPosition);
      } else {
        print("Button released -> moving servo to " + releasePosition);
        Servo.setPosition(servoPort, releasePosition);
      }
    }
  } catch(e) {
    print("Error: " + e);
  }

  sleep(pollMs);
}

Notes on the script

  • Uses edge detection (compares last state) so servo moves only once on press or release.
  • pollMs controls responsiveness; 20-50 ms is typical.
  • You can change pressPosition/releasePosition to required values.
  • If you want the servo to move smoothly or asynchronously, you can set Servo.setSpeed or use servo driver features that support velocity/acceleration.

Wiring and input tips

  • Most EZB digital inputs expect a TTL-level signal (0V = low, 3.3/5V = high). Typical push-button wiring:
    • Option A (recommended): Button between input and GND, with internal pull-up enabled on controller or use an external pull-up resistor to Vcc. This way unpressed = HIGH, pressed = LOW - adapt logic to that wiring.
    • Option B: Button between input and Vcc, use pull-down resistor to GND. Choose based on your I/O board capability.
  • Use Read Digital skill to monitor the pin visually while you press the button to confirm wiring and the polarity (High or Low when pressed).
  • Add debounce: software is handled by polling and edge detection; if you see chatter, add a short delay or require the state to be stable for N ms before taking action.

Alternative: Use Read Digital skill for debug + controlCommand

  • Add Read Digital to watch the input and verify wiring (Project Add robot skill Digital Read Digital).
  • Once verified you can either keep the JavaScript approach (recommended) or use ControlCommand() calls from other skills if you want a skill-to-skill link. (If you go that route, rename the Read Digital title to something unique and use a small script that listens for it - but the JS approach above is simpler and more robust.)

Would you like:

  • A version that uses the Read Digital skill’s title / ControlCommand() events instead of polling? (I can show exact ControlCommand syntax if you tell me the skill titles in your project.)
  • The script adapted for an inverted input (pressed = LOW) or for using a virtual servo vX instead of dX?

Tell me the EZB board and the ports you're using (digital port number and servo port), and whether the button reads HIGH when pressed or LOW, and I’ll adapt the script for you.


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.