Variable to serve as "holding contact" for while loop

I am having an issue. Simply, I want to have a push button that when pressed writes a value (sequenceswitch) to HIGH. While this condition is HIGH, a while loop positions a servo relative to a potentiometer. At the end of the while loop it writes the value of (sequenceswitch) back to zero.

This is the code I have

/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott http://people.interaction-ivrea.it/m.rinott

modified on 8 Nov 2013
by Scott Fitzgerald

*/

#include <Servo.h>
Servo myservo; // create servo object to control a servo

int switchState = 0;
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int sequenceswitch = 0;

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(2,OUTPUT); //attaches the button to pin 2
}

void loop() {
switchState = digitalRead(2);
sequenceswitch = LOW

if (switchState == HIGH){
sequenceswitch = HIGH; //writes sequenceswitch to HIGH
}

while (sequenceswitch == HIGH){
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); //lets stepper get into position.
sequenceswitch = LOW; // negates while loop condition
}
else {
digitalWrite(9,LOW);
}
}

Without reading all of your code. Use two variables that switch to HIGH. Make the first variable switch the second, and when the operation is complete, switch one or both variables back to LOW. I hope this makes sense. Its like a "dumb" debounce

Int val1 = low; (button)
Int val2 = low;

If val1 == high{
Val2 == high
}
While.......
Xxxxxx then finish with
Val2 == low

wkdkris:
Without reading all of your code. Use two variables that switch to HIGH. Make the first variable switch the second, and when the operation is complete, switch one or both variables back to LOW. I hope this makes sense. Its like a "dumb" debounce

Int val1 = low; (button)
Int val2 = low;

If val1 == high{
Val2 == high
}
While.......
Xxxxxx then finish with
Val2 == low

if you read the above that is what I have tried to accomplish. Basically it just writes the hypothetical variable (sequenceswitch) to HIGH if the push button is pressed. Then, the only way the condition is nullified is at the end of the WHILE loop.

Sorry so you have. I THINK it's the while which is causing your problem. I have written something similar. But have not used while. Only if and else if.

Unfortunately I'm using a mobile phone and find it difficult to work with any code because of the multiple lines.

Remove the while. Stick with two variable. And use if. You do not need while IMO because the value cannot change, until its executed and changes the variable at the end.

I have compiled this, please test it, but i notice your button is set to a OUTPUT??? Which seems a little odd???

#include <Servo.h>
Servo myservo;  // create servo object to control a servo

int switchState = 0;
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
int sequenceswitch = 0;

void setup() {
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 pinMode(2,OUTPUT);  //attaches the button to pin 2
}

void loop() {
 switchState = digitalRead(2);
 sequenceswitch = LOW;

 if(switchState == HIGH){
   sequenceswitch = HIGH;
   while(sequenceswitch == HIGH){
   val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
   val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
   myservo.write(val);                  // sets the servo position according to the scaled value
   delay(15);                                   //lets stepper get into position.
   sequenceswitch = LOW;                // negates while loop condition
   }
 }
 else {
   digitalWrite(9,LOW); 
 }
}

I suspect the WHILE should be changed to an IF

...R
PS ... To make it easy for people to read the code please use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Having now read your code, I think what Ive posted below, will behave how you expect.
There was a number of mistakes in the original you posted.
Switch was set to OUTPUT.
No need for the WHILE to do the switch action you require
WHILE was wrongly placed in alignment of the code.
Not sure it matters but your analog wasnt named A0;

Pin 2 needs to be put to ground with no other hardware (resistors etc) to simulate pushing a button. Logic is reversed because of internal pullup resistors

#include <Servo.h>
Servo myservo;  // create servo object to control a servo

int switchState = 1;
int potpin = A0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

int pin2in = 2; //setting the input switch pin

void setup() {
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 pinMode(pin2in, INPUT_PULLUP);  //attaches the button to pin 2
}

void loop() {
 switchState = digitalRead(pin2in); //

 if(switchState == LOW){
   val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
   val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
   myservo.write(val);                  // sets the servo position according to the scaled value
   delay(15);                                   //lets stepper get into position.
 }
 else {
   digitalWrite(9,LOW); 
 }
}

@wkdkris, please use the code button </> when posting code.

...R