Intiate function as soon as button is pressed, not loop it over and over.

void loop(){
  readButton();
  switch (buttonPressed){
  case LEFT: 
        positionLauncher = positionLauncher - 1;
        delay(200);
        Serial.println(positionLauncher);
        break;
  case RIGHT:
        positionLauncher = positionLauncher + 1;
        delay(200);
        Serial.println(positionLauncher);
        break;
        case SELECT:
    if (encoderLine != positionLauncher){
        moveLauncher(positionLauncher);
        break;
         }
        break;
           solenoid(IN);
  default: 
        analogWrite(motorSpeedPin,0);

        break;
  }
  
  servoControl();
  
  }

Hello everyone,

This is a project for a class of mine and thus far works perfectly. However the issue is when I press the SELECT button (in the switch case statement in void loop) whatever follows constantly gets repeated and doesn't run once. I've tried an if statement to say if such and such position is not met then run the following, but it loops the if statement over and over without breaking as well. How would I go about it so that when I press the select button once, it will just run those functions?

If the rest of the code is necessary, I'll provide it. (Didn't include it for safety reasons).

Thank you in advance.

If the rest of the code is necessary

It probably is.

Oh alright, updated the original post.

Why does readButton() not return a value? Why does it, apparently, diddle a global variable?

whatever follows constantly gets repeated

Yes it will:-

case SELECT:
    //if (int){
      //  moveLauncher(positionMove);
     //   break;
    //}
            solenoid(IN);
  default:

Needs to be

case SELECT:
    //if (int){
      //  moveLauncher(positionMove);
     //   break;
    //} 
       break;
            solenoid(IN);
  default:

However the issue is when I press the SELECT button (in the switch case statement in void loop) whatever follows constantly gets repeated and doesn't run once.

All that is in the SELECT case is commented out. How can you tell that it executes more than once?

Where do you reset buttonPressed? Now, perhaps, you can see why readButton() should return a values, instead of diddling a global.

All that is in the SELECT case is commented out. How can you tell that it executes more than once?

Well that’s the thing, I’ll need it pressed for it to actually run the SELECT case.

Where do you reset buttonPressed? Now, perhaps, you can see why readButton() should return a values, instead of diddling a global.

I am working off a template. I understand globals can be avoided but I have to use the provided template.

EDIT: updated code in the main post

I am working off a template. I understand globals can be avoided but I have to use the provided template.

Why? If the template is flawed, fix it!

Alrighty will do :).

You should keep posting your latest code not updating that first post.

You have

           solenoid(IN);

That can never be reached because there is a break before and after it.