How to add a toggle switch to a servo

Hello, hope you are all doing well.
I am really new to arduino programming so bare with me! What I want to do is to program a toggle on/off switch into a code I have for a servo and a magnet. The idea is the magnet is attached to the servo and is on when the servo is moving upwards and off when the servo is moving downwards. Therefore what I want is when I press the button this code starts and when I press it again it stops. The servo code is:

int magnet = 8;
#include <Servo.h>
Servo servo;
int pos = 0;
void setup() {
  pinMode(magnet, OUTPUT);
  servo.attach(11);
  


}

void loop() {
  for(pos = 0; pos <= 180; pos +=1){
    servo.write(pos);
    delay(50);
    digitalWrite(magnet, HIGH);
    
  }
  for (pos = 180; pos >= 0; pos -=1) {
    servo.write(pos);
    delay(50);
    digitalWrite(magnet, LOW);
    
  }
  

}

Following a Tutorial, I have used this code for a toggle switch where when I press a button, a led light turns on. The code is here:

//  Sketch: SwitchingThings_03
//
//  An  example of using a button switch as a toggle switch to turn an LED on or off
//
//  Pins
//  D10 to resister and LED
//  D2 to push button switch
//  
 
// Define the pins being used
int pin_LED = 10;
int pin_switch = 2;
 
// variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
 
boolean LEDstatus = LOW;
 
void setup() 
{
    Serial.begin(9600);
    Serial.print("Sketch:   ");   Serial.println(__FILE__);
    Serial.print("Uploaded: ");   Serial.println(__DATE__);
    Serial.println(" ");
 
    pinMode(pin_LED, OUTPUT);  
    digitalWrite(pin_LED,LOW); 
 
    pinMode(pin_switch, INPUT); 
}
 
void loop()
{
    newSwitchState = digitalRead(pin_switch);
 
    if ( newSwitchState != oldSwitchState ) 
    {
       // has the button switch been closed?
       if ( newSwitchState == HIGH )
       {
           if ( LEDstatus == LOW ) { digitalWrite(pin_LED, HIGH);  LEDstatus = HIGH; }
           else                    { digitalWrite(pin_LED, LOW);   LEDstatus = LOW;  }
       }
       oldSwitchState = newSwitchState;
    }   

}

so what I have been trying to do, is use the for loop from the servo code but starting it with something along the lines of “while(pin_LED == HIGH)”. Although I keep on running into problems, probably because I am very much out of my depth.

I have been having some serious problems with this and any help would be greatly appreciated

Thank you very much

"while(pin_LED == HIGH)".

pin_LED has the value 10.
You gave it that value.
Arduino gave “HIGH” the value 1.

They are never going to be equal.

so bare with me!

Not on a first date.

@patrickstratton - your duplicate topic on teh same subject has been deleted and this one has been moved to this forum section as the original location was not appropriate

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.