unexpected results possibly from vibration

The code below works perfectly when the motorcycle is not running but is powered on, as it powers the Arduino.

The throttle, rotary encoder works perfectly and the switches, “clutchReleaseToThrottle” and “breakReleaseToThrottle” work perfectly at speed real world riding.

The buttons, “cruiseUp” and “cruiseDn” however are not working correctly once the bike speed increases. The buttons will set the throttle position as they should but then they release on their own, at various different times or durations.

The servo should hold its position once either of these buttons are pushed until “clutchReleaseToThrottle” or"breakReleaseToThrottle" switch has been closed. This is the only way they should be releasing.

#include <Bounce.h>
#include <Servo.h>

void setup(){
  buttonSetup();
  throttleSetup();
}

void loop(){
  throttleLoop();
  cruiseButtonLoop();
  
}
 
Servo throttleServo; // Define throttle Servo
int servoPos = 30;    // Initial power up “servo position”
int moveServoAmount = 2;    // move the servo 2 degrees per 1/125 pulse
const int pin_ThrottleUp = 4;
const int pin_ThrottleDn = A2;  // pin 14
unsigned char encoder_ThrottleUp;
unsigned char encoder_ThrottleDn;
unsigned char encoder_ThrottleUp_prev=0;

// set pin numbers:
const int buttonPin = 11; // the number of the button pin
const int buttonPin2 = 10;
const int buttonPin3 = 9;
const int buttonPin4 = 8;

long debounceDelay = 50;    // the debounce time; increase if the output flickers

//Debounce objects
// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce cruiseUp = Bounce(buttonPin, debounceDelay);
Bounce cruiseDn = Bounce(buttonPin2, debounceDelay);
Bounce brakeReleaseToThrottle = Bounce(buttonPin4, debounceDelay);
Bounce clutchReleaseToThrottle = Bounce(buttonPin3, debounceDelay);

void throttleSetup()  {
  throttleServo.attach(12); // servo on digital pin 12
  throttleServo.write(30);
  pinMode(pin_ThrottleUp, INPUT);
  digitalWrite(pin_ThrottleUp, HIGH);// turn on pullup resistor
  pinMode(pin_ThrottleDn, INPUT);
  digitalWrite(pin_ThrottleDn, HIGH);// turn on pullup resistor
} 

void throttleLoop()  {

  encoder_ThrottleUp = digitalRead(pin_ThrottleUp); // Read encoder pins
  encoder_ThrottleDn = digitalRead(pin_ThrottleDn); // Read encoder pins
  if((!encoder_ThrottleUp) && (encoder_ThrottleUp_prev)){
    // A has gone from high to low
    if(encoder_ThrottleDn) {
      // B is high so clockwise
      // increase the throttle, dont go over 220
      if(servoPos + moveServoAmount <= 220) servoPos += moveServoAmount;
    }
    else {
      // B is low so counter-clockwise
      // decrease the throttle, dont go below 20
      if(servoPos - moveServoAmount >= 20) servoPos -= moveServoAmount;
    }
  }
  encoder_ThrottleUp_prev = encoder_ThrottleUp;     // StAof ThrottleUp for next time
  // set the new location of servo:
  throttleServo.write(servoPos);
}

void buttonSetup(){ 
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 
  pinMode(buttonPin2, INPUT); 
  pinMode(buttonPin3, INPUT); 
  pinMode(buttonPin4, INPUT);
}

void cruiseButtonLoop(){ 
  //Update debounce tool
  cruiseUp.update();
  cruiseDn.update();
  brakeReleaseToThrottle.update();
  clutchReleaseToThrottle.update();
  //Do not need to update these here are they are not used
  //bouncer3.update();
  //bouncer4.update(); 
  if (cruiseUp.read() == HIGH)
  {
    cruiseLoop(); //Enters button control loop
  } 
  if (cruiseDn.read() == HIGH)
  {
    cruiseLoop(); //Enters button control loop
  }
      else if (brakeReleaseToThrottle.read() == HIGH)
    {
      servoPos = 30;
    }
} 

/**
 * If button control is enabled, loop and handle control buttons
 * If exit buttons (To return to pot control) are pressed, exit loop and return
 * to pot control
 */
void cruiseLoop(){
  servoPos = throttleServo.read(); //reads current servo location 
  int btnControlEnabled = 1; //If button control is enabled this equals 1, else it equals 0 
  while(btnControlEnabled == 1)
  {
    //Update all debounce tools
    cruiseUp.update();
    cruiseDn.update();
    brakeReleaseToThrottle.update();
    clutchReleaseToThrottle.update(); 
    if (cruiseUp.read() == HIGH)
    {
      throttleServo.write(servoPos + 1); //Add 2 degrees to servo position for increased motor rpm
      servoPos = throttleServo.read();
      delay(40); //allows time for switch ro reset
    }
    //If first button not pressed, check the next...
    else if (cruiseDn.read() == HIGH)
    {
      throttleServo.write(servoPos - 1); //Subtract 2 degrees to servo position for decreased motor rpm
      servoPos = throttleServo.read(); //Read new servo position
      delay(40); //allows time for switch ro reset
    }
    else if (clutchReleaseToThrottle.read() == HIGH)
    {
      servoPos = 30;
      btnControlEnabled = 0; //Set loop exit condition
      throttleLoop();
    }
    else if (brakeReleaseToThrottle.read() == HIGH)
    {
      servoPos = 30;
      btnControlEnabled = 0; //Set loop exit condition
      throttleLoop();
    }
  }
}

Cars and bikes are notoriously 'noisy' electrical environments which can affect electronic equipment attached to them. You could try running the Arduino on a separate power supply but my best advice would be to abandon the project on safety grounds. You are not in a position to safely test your cruise control without being a hazard to yourself or others and the thought of

they release on their own, at various different times or durations.

is very worrying. What if the system made non commanded throttle increases in the same way ?

I don't know where you live but it is quite possible that using such a system on public roads would be illegal without proper testing and certification.

I agree it's potentially dangerous and may even be illegal in some places. At least you do always have the option to dip the clutch, so it's not positively reckless, but just a brief unexpected burst of throttle at the wrong time could be dangerous and I wouldn't be comfortable with that.

If you decide to go ahead with full knowledge of the risks then I suggest you start by finding out whether the problem is mechanical or electrical. Is mechanical vibration triggering the switches, or are you getting electrical noise causing spurious readings?