Rotating servo motor with push button

Hello
I am Creating an Arduino sketch that rotates the servo motor from 0 to 90 degrees if the pushbutton is pressed an even number of times; otherwise, it rotates from 180 degrees to 90 degrees. With a 10o step size and a 0.5sec delay between successive angles, the rotation will be gradual.

if I don't use while loop it works but rotates only once, and if I add while loop it doesn't work
can anyone tell me what I am doing wrong here

// C++ code
//
#include<Servo.h>

int button = 5;
int pbs; //previous button state
int cbs; //current button state
int count=0;

Servo myservo;
int pos;

void setup()
{
    myservo.attach(10);
    pinMode(button, INPUT);
    cbs = digitalRead(button);

}

void loop()
{
    pbs = cbs;
    cbs = digitalRead(button);
    if (pbs == HIGH && cbs == LOW)
    {
        count++;
        if ((count%2)!= 0)
        {
          while(pbs == LOW && cbs == HIGH){
            for(pos=0; pos<=90; pos+=10)
            {
                delay(500);
                myservo.write(pos);
            }
          }
        }
        else
        {
          while(pbs == HIGH && cbs == LOW){
            for(pos=180; pos>=90; pos-=10)
            {
                delay(500);
                myservo.write(pos);
            }
          }
        }
    }
}

  pinMode(button, OUTPUT);

Really ?

is there an issue?

edit:
nvm just saw it

Hello
This is a typical school assignment.
Take some time and check the forum for a solution.
Have a nice day and enjoy coding in C++.

sorry for bothering but at least could you tell me if I am in the right direction?

how can the values of pbs and cbs change within your while loops?

what do you recommend?
should I add PBS and CBS again inside the while loop?
or should I make the

while(cbs!=HIGH)

I am really confused in this

answering the question

is that what you meant by reading the value inside the while loop

// C++ code
//
#include<Servo.h>

int button = 5;
int pbs; //previous button state
int cbs; //current button state
int count=0;

Servo myservo;
int pos;

void setup()
{
    myservo.attach(10);
    pinMode(button, INPUT);
    cbs = digitalRead(button);

}

void loop()
{
    pbs = cbs;
    cbs = digitalRead(button);
    if (pbs == HIGH && cbs == LOW)
    {
        count++;
        if ((count%2)!= 0)
        {
          while(digitalRead(button) != HIGH){
            for(pos=0; pos<=90; pos+=10)
            {
                delay(500);
                myservo.write(pos);
            }
          }
        }
        else
        {
          while(digitalRead(button) != HIGH){
            for(pos=180; pos>=90; pos-=10)
            {
                delay(500);
                myservo.write(pos);
            }
          }
        }
    }
}

you still haven't answered the question. you're jumping to a solution. understanding the code will help you figure out what you're trying to do

when you answer that question, what is the next question

sorry about that

I think it can't since it is in a while loop

it's not because it's in a while loop. what does the while loop do (or not do) that make the logic inappropriate?

understand what the code is doing

make that function run till it stops if there is a condition in a while

what it does not do is accept any variable change while the loop is running

correct?

This is what I have done so far, kinda works
I was hoping to make one tap instead of holding push button

// C++ code
//
#include<Servo.h>

int button = 5;
int pbs; //previous button state
int cbs; //current button state
int count=0;

Servo myservo;
int pos;

void setup()
{
    myservo.attach(10);
    pinMode(button, INPUT);
    cbs = digitalRead(button);

}

void loop()
{
    pbs = cbs;
    cbs = digitalRead(button);
    if (pbs == HIGH && cbs == LOW)
    {
        count++;
        if ((count%2)!= 0)
        {
          while(digitalRead(button) != HIGH){
            for(pos=0; pos<=90; pos+=10)
            {
                delay(500);
                myservo.write(pos);
            }
            if (digitalRead(button) == HIGH){
              break;
            }
          }
        }
        else
        {
          while(digitalRead(button) != HIGH){
            for(pos=180; pos>=90; pos-=10)
            {
                delay(500);
                myservo.write(pos);
            }
            if (digitalRead(button) == HIGH){
              break;
            }
          }
        }
    }
}

You will need to detect when the button becomes pressed instead of when it is pressed

See the StateChangeDetection example in the IDE

This is what I understood, for this code I have to double-tap the push button to go next stage, and it works when the servo is in the middle of the action.

but still can't figure it out single tap

// C++ code
//
#include<Servo.h>

int button = 5;
int pbs; //previous button state
int cbs; //current button state
int count=0;

Servo myservo;
int pos;

void setup()
{
    myservo.attach(10);
    pinMode(button, INPUT);
    cbs = digitalRead(button);

}

void loop()
{
    pbs = cbs;
    cbs = digitalRead(button);
    if (pbs == HIGH && cbs == LOW)
    {
        count++;
        if ((count%2)!= 0)
        {
          while(digitalRead(button) != pbs && digitalRead(button) == cbs){
            for(pos=0; pos<=90; pos+=10)
            {
                delay(500);
                myservo.write(pos);
              
            if (digitalRead(button) == pbs && digitalRead(button) != cbs){
              break;
            }
            }
          }
        }
        else
        {
          while(digitalRead(button) != pbs && digitalRead(button) == cbs){
            for(pos=180; pos>=90; pos-=10)
            {
                delay(500);
                myservo.write(pos);
              if (digitalRead(button) == pbs && digitalRead(button) != cbs){
              break;
            }
            }
            
          }
        }
    }
}

in the original code, where was there an attempt to change the value?

there's a for loop nested within the while loop. why is there a need for the while loop? what do you expect the while loop to do?

no

if I had only for loop, it will only rotates once.
but with a while loop, it won't stop rotating till I push the button. that what I was aiming for

do you want to be able to stop the rotation at any time or only after it completes a cycle?

I am planning for any time, but I am also interested how to stop after the completing cyle
if you don't mind