using a button to complete two tasks

Hello, I was trying design a trap door with a limit switch, solenoid, servo, and button. I am trying to code a button to turn on a solenoid using a relay button and at the same time will turn on the servo to turn 90 degrees to close the trap door again. I am able to code turning on the solenoid by itself and the servo by its self But I am not sure how to integrate this together into the button. Any help would be greatly appreciated

my code is below

#include <Servo.h>

Servo myservo;
// constants
const int Switch = 2; // the number of the Switch pin
const int Sol = 4; // the number of the Sol pin
const int button = 3; // the number of the button pin
int pos = 0; // variable to store the servo position
// variables changing
int switchState = 0; // variable for reading the switch status
int state; // the current state of the Sol
int buttonState; // the current reading from the input pin

void setup(){
// inputs
myservo.attach(5); // attaches the servo on pin 9 to the servo object
pinMode(pos, OUTPUT);
digitalWrite (button, INPUT_PULLUP);
pinMode(button, INPUT_PULLUP); // button
pinMode(Switch, INPUT_PULLUP); // limit switch
// outputs
pinMode(Sol, OUTPUT); // solenoid

void loop(){
// reading state of both the button and the switch
switchState = digitalRead(Switch);
buttonState = digitalRead(button);

// loop for turning on/off solenoid
if (buttonState == HIGH && switchState == HIGH) // if limit switch is pressed down even though we press the button solenoid will not turn on
state = LOW;
else if (buttonState == LOW && switchState == LOW)
state = HIGH;

digitalWrite(Sol, state);

if (digitalRead(button) == LOW)

for(pos = 0; pos < 90; pos += 90) // goes from 0 degrees to 90 degrees
{ // in steps of degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
// waits 1s for the servo to reach the position
if (digitalRead(button) == HIGH)

for(pos = 90; pos>=90; pos-=90) // goes from 90 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 50ms for the servo to reach the position

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Please include power supplies and pin names, please not a fritzy image.

Thanks… Tom… :slight_smile: