u/JustABoy75

Looking for a servo tech assist...

Looking for a servo tech assist...

So, I have a bunch of servos I am needing to wire for a project. I am currently up to 10 in TinkerCad as I am waiting for hardware to be delivered. Most of the code is working properly, but there is strange behavior at power on. I'm hoping someone can tell me how to prevent Group A from moving at power on.

Thanks in advance!

Details:

  • Servos
    • Group A
      • Bottom four servos
    • Group B
      • Middle two servos
    • Group C
      • Top four servos
  • Button
    • Single press = Toggle Group A 90-degrees
    • Double Press = Toggle Group B 90-degrees
    • Triple press = Toggle Group C 90-degrees

Problem:

  • When power is applied, all servos "twitch" to starting position.
    • Then Group A servos move 90-degrees, while Groups B & C stay in place.

Code:

#include <Servo.h>

// Pin assignments

const int BUTTON_PIN = 2;

// Servo Pins (Assigned 1 servo per group)

const int SERVO_A1_PIN = 5;

const int SERVO_B1_PIN = 9;

const int SERVO_C1_PIN = 11;

// Target angles

const int POS_MIN = 0;

const int POS_MAX = 90;

// Timing configuration

const unsigned long DEBOUNCE_PERIOD = 50; // Debounce time (ms)

const unsigned long TIMEOUT_PERIOD = 500; // Time (ms) allowed between clicks

// Servo objects

Servo servoA1;

Servo servoB1;

Servo servoC1;

// Servo position tracking

int targetA = POS_MIN;

int targetB = POS_MIN;

int targetC = POS_MIN;

// Click tracking variables

int clickCount = 0;

bool lastButtonState = HIGH;

bool stableButtonState = HIGH;

unsigned long lastDebounceTime = 0;

unsigned long lastClickTime = 0;

void setup() {

pinMode(BUTTON_PIN, INPUT_PULLUP);

// Attach all 3 servos

servoA1.attach(SERVO_A1_PIN);

servoB1.attach(SERVO_B1_PIN);

servoC1.attach(SERVO_C1_PIN);

// Initialize all to POS_MIN (90 degrees)

servoA1.write(targetA);

servoB1.write(targetB);

servoC1.write(targetC);

}

void loop() {

bool currentButtonState = digitalRead(BUTTON_PIN);

unsigned long currentMillis = millis();

// Check if the physical button state changed (due to noise or pressing)

if (currentButtonState != lastButtonState) {

lastDebounceTime = currentMillis; // Reset debounce timer

}

lastButtonState = currentButtonState;

// Check if the state has been stable longer than the debounce period

if ((currentMillis - lastDebounceTime) >= DEBOUNCE_PERIOD) {

// If the stable state changed, process the transition

if (currentButtonState != stableButtonState) {

stableButtonState = currentButtonState;

// Detect press event (falling edge when using INPUT_PULLUP)

if (stableButtonState == LOW) {

clickCount++;

lastClickTime = currentMillis; // Reset the timeout clock on every valid click

}

}

}

// Process the click count only after the user stops clicking (timeout expires)

if (clickCount > 0 && (currentMillis - lastClickTime >= TIMEOUT_PERIOD)) {

switch (clickCount) {

case 1: // Single Press -> Toggle Servo A1

if (targetA == POS_MIN) { targetA = POS_MAX; }

else { targetA = POS_MIN; }

servoA1.write(targetA);

break;

case 2: // Double Press -> Toggle Servo B1

if (targetB == POS_MIN) { targetB = POS_MAX; }

else { targetB = POS_MIN; }

servoB1.write(targetB);

break;

case 3: // Triple Press -> Toggle Servo C1

if (targetC == POS_MIN) { targetC = POS_MAX; }

else { targetC = POS_MIN; }

servoC1.write(targetC);

break;

default: // Ignore accidental extra clicks

break;

}

clickCount = 0; // Reset counter for the next routine

}

}

Power Off State

Power On State

reddit.com
u/JustABoy75 — 18 hours ago