Please HELP!

I’m trying to add a code to turn led1 off the only way I figure how to do it was to
make.

if (digitalRead(2) == HIGH){

digitalWrite(11, LOW);
}

but when I do this the led1 keep flashing at a very low density I need it to stop the flash

Thanks in advance.

    #include <Servo.h> 
    
     
    class Flasher
    {
      // Class Member Variables
      // These are initialized at startup
      int ledPin;      // the number of the LED pin
      long OnTime;     // milliseconds of on-time
      long OffTime;    // milliseconds of off-time
     
      // These maintain the current state
      int ledState;                 // ledState used to set the LED
      unsigned long previousMillis;   // will store last time LED was updated
     
      // Constructor - creates a Flasher 
      // and initializes the member variables and state
      public:
      Flasher(int pin, long on, long off)
      {
      ledPin = pin;
      pinMode(ledPin, OUTPUT);     
        
      OnTime = on;
      OffTime = off;
      
      ledState = LOW; 
      previousMillis = 0;
      }
     
      void Update()
      {
        // check to see if it's time to change the state of the LED
        unsigned long currentMillis = millis();
         
        if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
        {
          ledState = LOW;  // Turn it off
          previousMillis = currentMillis;  // Remember the time
          digitalWrite(ledPin, ledState);  // Update the actual LED
        }
        else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
        {
          ledState = HIGH;  // turn it on
          previousMillis = currentMillis;   // Remember the time
          digitalWrite(ledPin, ledState);   // Update the actual LED
        }
      }
    };
     
    class Sweeper
    {
      Servo servo;              // the servo
      int pos;              // current servo position 
      int increment;        // increment to move for each interval
      int  updateInterval;      // interval between updates
      unsigned long lastUpdate; // last update of position
     
    public: 
      Sweeper(int interval)
      {
        updateInterval = interval;
        increment = 1;
      }
      
      void Attach(int pin)
      {
        servo.attach(pin);
      }
      
      void Detach()
      {
        servo.detach();
      }
      
      void Update()
      {
        if((millis() - lastUpdate) > updateInterval)  // time to update
        {
          lastUpdate = millis();
          pos += increment;
          servo.write(pos);
          Serial.println(pos);
          if ((pos >= 180) || (pos <= 0)) // end of sweep
          {
            // reverse direction
            increment = -increment;
          }
        }
      }
    };
     
     
    Flasher led1(11, 250, 250);
    Flasher led2(12, 500, 500);
    Flasher led3(13, 125, 125);
     
    Sweeper sweeper1(15);
    Sweeper sweeper2(25);
     
    void setup() 
    { 
      Serial.begin(9600);
      sweeper1.Attach(9);
      sweeper2.Attach(10);
    
    } 
     
     
    void loop() 
    { 
      sweeper1.Update();
      
      if(digitalRead(2) == HIGH){
   
       sweeper2.Update();
       led1.Update();
       }
         
      led2.Update();
      led3.Update();
      
    }

How is the switch wired?

.

!(http://<a target=)">

|500x281

Use INPUT_PULLUP on pin 2 to see what happens. Use diagonal pins on the switch.

.

Thanks for your help.

this is what I did and it is much better than before but I still see led1 flashing at very low density but much lower than before is almost unnoticeable

any ideas on how can I stop the flashing?

    #include <Servo.h> 
    
     
    class Flasher
    {
      // Class Member Variables
      // These are initialized at startup
      int ledPin;      // the number of the LED pin
      long OnTime;     // milliseconds of on-time
      long OffTime;    // milliseconds of off-time
     
      // These maintain the current state
      int ledState;                 // ledState used to set the LED
      unsigned long previousMillis;   // will store last time LED was updated
     
      // Constructor - creates a Flasher 
      // and initializes the member variables and state
      public:
      Flasher(int pin, long on, long off)
      {
      ledPin = pin;
      pinMode(ledPin, OUTPUT);     
        
      OnTime = on;
      OffTime = off;
      
      ledState = LOW; 
      previousMillis = 0;
      }
     
      void Update()
      {
        // check to see if it's time to change the state of the LED
        unsigned long currentMillis = millis();
         
        if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
        {
          ledState = LOW;  // Turn it off
          previousMillis = currentMillis;  // Remember the time
          digitalWrite(ledPin, ledState);  // Update the actual LED
        }
        else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
        {
          ledState = HIGH;  // turn it on
          previousMillis = currentMillis;   // Remember the time
          digitalWrite(ledPin, ledState);   // Update the actual LED
        }
      }
    };
     
    class Sweeper
    {
      Servo servo;              // the servo
      int pos;              // current servo position 
      int increment;        // increment to move for each interval
      int  updateInterval;      // interval between updates
      unsigned long lastUpdate; // last update of position
     
    public: 
      Sweeper(int interval)
      {
        updateInterval = interval;
        increment = 1;
      }
      
      void Attach(int pin)
      {
        servo.attach(pin);
      }
      
      void Detach()
      {
        servo.detach();
      }
      
      void Update()
      {
        if((millis() - lastUpdate) > updateInterval)  // time to update
        {
          lastUpdate = millis();
          pos += increment;
          servo.write(pos);
          Serial.println(pos);
          if ((pos >= 180) || (pos <= 0)) // end of sweep
          {
            // reverse direction
            increment = -increment;
          }
        }
      }
    };
     
     
    Flasher led1(11, 250, 250);
    Flasher led2(12, 500, 500);
    Flasher led3(13, 125, 125);
     
    Sweeper sweeper1(15);
    Sweeper sweeper2(25);
     
    void setup() 
    { 
      Serial.begin(9600);
      sweeper1.Attach(9);
      sweeper2.Attach(10);
      pinMode(2, INPUT_PULLUP);
    
    } 
     
     
    void loop() 
    { 
      sweeper1.Update();
      
      if(digitalRead(2) == HIGH){
   
       digitalWrite(11, LOW);
       
       
       }
     
      else if (digitalRead(2) == LOW);
      
      led1.Update();
      led2.Update();
      led3.Update();
      
    }

One thing to do is to stop diddling with the hardware in your constructors. The hardware is not ready to be diddled with when the constructors are called. Add a begin() method to each class, and diddle with the hardware in that method.

else if (digitalRead(2) == LOW) ; I don't think you want the ;

Where do you want the { } to go for the else if ?

else if (digitalRead(2) == LOW) {

}

You could leave the 'else if' just a 'else' .

EDIT: digitalWrite(11, LOW); this controls pin 11 And so does led1.Update(); Not wise ;)

.