user19230: I am pretty new to Arduino and programming too. 
I want to sweep a servo using DIP switch. At the moment, I use 4 position DIP switch. To each of the switches, I want to set the number of times the servo has to run. Eg., Pos 1 of DIP switch says, sweep for 5 times. Pos 2 says, sweeps for 10 times etc. 
I wrote the following code:
#include <Servo.h>

Servo myservo;  
int pos = 0; 
int count = 0;   // might need later
int runXTimes = 0;

#define S1 2
#define S2 3
#define S3 4
#define S4 5

int s1state = HIGH;
int s2state = HIGH;
int s3state = HIGH;
int s4state = HIGH;


void setup() {
   myservo.attach(9);
   pinMode(S1, INPUT_PULLUP);
   pinMode(S2, INPUT_PULLUP);
   pinMode(S3, INPUT_PULLUP);
   pinMode(S4, INPUT_PULLUP);
   pinMode(9, OUTPUT);
}

void loop() {

   s1state = digitalRead(S1);
   s2state = digitalRead(S2);
   s3state = digitalRead(S3);
   s4state = digitalRead(S4);


   // Servo control through Switch 1
   if (s1state == LOW) {     // read the input pin
      for (int runXTimes = 0; runXTimes < 5; runXTimes++) {    // Servo to     sweep for 5 times
         for (pos = 0; pos <= 80; pos += 1)  // goes from 0 degrees to 80 degrees in steps of  degree

         {                                   
            myservo.write(pos);              // tell servo to go to position in variable 'pos'
            delay(5);
         }

         for(pos = 80; pos>=0; pos-=1)     // goes from 80 degrees to 0 degrees
         {                               
            myservo.write(pos);              // tell servo to go to position in variable 'pos'
            delay(5);
         }

      }

   }

   // Servo control through Switch 2. Used servo detach here
   else if (s2state == LOW) {  // read the input pin
      for (int runXTimes = 0; runXTimes < 10; runXTimes++) {  // Servo to sweep for 10 times
         for (pos = 0; pos <= 80; pos += 1)  // goes from 0 degrees to 80 degrees in steps of  degree
         {                                  
            myservo.write(pos);              // tell servo to go to position in variable 'pos'
            delay(5); 
         }

         for(pos = 80; pos>=0; pos-=1)     // goes from 80 degrees to 0 degrees
         {                               
            myservo.write(pos);              // tell servo to go to position in variable 'pos'
            delay(5);  
         }

      }
      myservo.detach();
   }

   // Servo control through Switch 3
   else if (s3state == LOW) {  // read the input pin
      for (pos = 0; pos <= 80; pos += 1)  // goes from 0 degrees to 80 degrees in steps of  degree
      {                                  // 
         myservo.write(pos);              // tell servo to go to position in variable 'pos'
         delay(5); 
      }

      for(pos = 80; pos>=0; pos-=1)     // goes from 80 degrees to 0 degrees
      {                               
         myservo.write(pos);              // tell servo to go to position in variable 'pos'
         delay(5);  
      }
   }

   // Servo control through Switch 4
   else if (s4state == LOW) {  // read the input pin
      for (pos = 0; pos <= 80; pos += 1)  // goes from 0 degrees to 80 degrees  in steps of  degree
      {                                 
         myservo.write(pos);              // tell servo to go to position in variable 'pos'

         delay(5); 
      }

      for(pos = 80; pos>=0; pos-=1)     // goes from 80 degrees to 0 degrees
      {                               
         myservo.write(pos);              // tell servo to go to position in variable 'pos'
         delay(5);  
      }

   }
}

The actions that happen with the above code are:
1) Servo control through Switch 1 - If I switch ON (0) and then OFF (1) immediately, the servo sweeps for 5 times. Works more like a button switch. If I switch ON and OFF (immediately) again, I am able to sweep 5 times again. But, if I leave the switch in 1, the sweep does not stop after 5 times.
2) Servo control through Switch 2: If I switch ON, the servo sweeps for 10 times and stops as per my interest. But, if I change the switch to 0 and 1 again, the sequence does not start. In fact, no other switch works. I need to power OFF the Arduino to run the program again. 
3) Servo control through Switch 3 (= switch 4): If my switch is 1, the servo sweeps. If my switch is 0, the servo stops. 
Could you please suggest:
1) How to write the code so that my servo sweeps only for the set number of times even when the switch is in 1 always or whenever I toggle between 0 and 1?
2) I do not want to use serial communication (or typically a PC). Is it possible to interface a 7-segment  or LCD to display the number of sweeps completed or still left. 
Thank you in advance. 

user28033: This is imagination-based since I can't test it myself at this moment, but feel free to ask if something is not right.
int readswitch()
{
 int S1,S2,S3,S4;
 uint8_t data = 255;
 data |= digitalRead(S4)<<3;
 data |= digitalRead(S3)<<2;
 data |= digitalRead(S2)<<1;
 data |= digitalRead(S1)<<0;

  if (data==0b11111110) return 1; else
  if (data==0b11111101) return 2; else
  if (data==0b11111011) return 3; else
  if (data==0b11110111) return 4; else
  //add another condition here if you want
  return 0;
}

int laststate = 0;
int state = 0;
int rotation;

void setup()
{
  //setup here, including LCD initialization
  //..
  //..
  //..
}

void updateLCD(int x,int y)
{
  lcd.clear();
  //displaying pin status
  lcd.print("dip : ");
  lcd.print(x,DEC);
  lcd.print(" rot : ");
  lcd.print(y,DEC); 
}

void loop()
{
  state = readswitch();
  if (state!=laststate)
  {
    if (state == 1) 
    {
      rotation = 5;
      for (int i = 0; i< rotation; i++)
        {
         //update lcd
         updateLCD(state,(rotation-i));

         //.. servo rotation
         //..
        }
      //...
    } else

    if (state == 2) 
    {
      //do what you want here if switch 2 == LOW
      //...
    } else

    if (state == 3) //switch 1 == LOW
    {
      //do what you want here if switch 3 == LOW
      //...
    } else

    if (state == 4) //switch 1 == LOW
    {
      //do what you want here if switch 4 == LOW
      //...
    } 
  }
  laststate = state;

}

For more information about LCD, you can refer to this documentation

19230: <p>I am pretty new to Arduino and programming too. 
I want to sweep a servo using DIP switch. At the moment, I use 4 position DIP switch. To each of the switches, I want to set the number of times the servo has to run. Eg., Pos 1 of DIP switch says, sweep for 5 times. Pos 2 says, sweeps for 10 times etc. </p>

<p>I wrote the following code:</p>

<pre><code>#include &lt;Servo.h&gt;

Servo myservo;  
int pos = 0; 
int count = 0;   // might need later
int runXTimes = 0;

#define S1 2
#define S2 3
#define S3 4
#define S4 5

int s1state = HIGH;
int s2state = HIGH;
int s3state = HIGH;
int s4state = HIGH;


void setup() {
   myservo.attach(9);
   pinMode(S1, INPUT_PULLUP);
   pinMode(S2, INPUT_PULLUP);
   pinMode(S3, INPUT_PULLUP);
   pinMode(S4, INPUT_PULLUP);
   pinMode(9, OUTPUT);
}

void loop() {

   s1state = digitalRead(S1);
   s2state = digitalRead(S2);
   s3state = digitalRead(S3);
   s4state = digitalRead(S4);


   // Servo control through Switch 1
   if (s1state == LOW) {     // read the input pin
      for (int runXTimes = 0; runXTimes &lt; 5; runXTimes++) {    // Servo to     sweep for 5 times
         for (pos = 0; pos &lt;= 80; pos += 1)  // goes from 0 degrees to 80 degrees in steps of  degree

         {                                   
            myservo.write(pos);              // tell servo to go to position in variable 'pos'
            delay(5);
         }

         for(pos = 80; pos&gt;=0; pos-=1)     // goes from 80 degrees to 0 degrees
         {                               
            myservo.write(pos);              // tell servo to go to position in variable 'pos'
            delay(5);
         }

      }

   }

   // Servo control through Switch 2. Used servo detach here
   else if (s2state == LOW) {  // read the input pin
      for (int runXTimes = 0; runXTimes &lt; 10; runXTimes++) {  // Servo to sweep for 10 times
         for (pos = 0; pos &lt;= 80; pos += 1)  // goes from 0 degrees to 80 degrees in steps of  degree
         {                                  
            myservo.write(pos);              // tell servo to go to position in variable 'pos'
            delay(5); 
         }

         for(pos = 80; pos&gt;=0; pos-=1)     // goes from 80 degrees to 0 degrees
         {                               
            myservo.write(pos);              // tell servo to go to position in variable 'pos'
            delay(5);  
         }

      }
      myservo.detach();
   }

   // Servo control through Switch 3
   else if (s3state == LOW) {  // read the input pin
      for (pos = 0; pos &lt;= 80; pos += 1)  // goes from 0 degrees to 80 degrees in steps of  degree
      {                                  // 
         myservo.write(pos);              // tell servo to go to position in variable 'pos'
         delay(5); 
      }

      for(pos = 80; pos&gt;=0; pos-=1)     // goes from 80 degrees to 0 degrees
      {                               
         myservo.write(pos);              // tell servo to go to position in variable 'pos'
         delay(5);  
      }
   }

   // Servo control through Switch 4
   else if (s4state == LOW) {  // read the input pin
      for (pos = 0; pos &lt;= 80; pos += 1)  // goes from 0 degrees to 80 degrees  in steps of  degree
      {                                 
         myservo.write(pos);              // tell servo to go to position in variable 'pos'

         delay(5); 
      }

      for(pos = 80; pos&gt;=0; pos-=1)     // goes from 80 degrees to 0 degrees
      {                               
         myservo.write(pos);              // tell servo to go to position in variable 'pos'
         delay(5);  
      }

   }
}
</code></pre>

<p>The actions that happen with the above code are:
1) Servo control through Switch 1 - If I switch ON (0) and then OFF (1) immediately, the servo sweeps for 5 times. Works more like a button switch. If I switch ON and OFF (immediately) again, I am able to sweep 5 times again. But, if I leave the switch in 1, the sweep does not stop after 5 times.<br>
2) Servo control through Switch 2: If I switch ON, the servo sweeps for 10 times and stops as per my interest. But, if I change the switch to 0 and 1 again, the sequence does not start. In fact, no other switch works. I need to power OFF the Arduino to run the program again. 
3) Servo control through Switch 3 (= switch 4): If my switch is 1, the servo sweeps. If my switch is 0, the servo stops. </p>

<p>Could you please suggest:
1) How to write the code so that my servo sweeps only for the set number of times even when the switch is in 1 always or whenever I toggle between 0 and 1?
2) I do not want to use serial communication (or typically a PC). Is it possible to interface a 7-segment  or LCD to display the number of sweeps completed or still left. </p>

<p>Thank you in advance. </p>
 28033: <p>This is imagination-based since I can't test it myself at this moment, but feel free to ask if something is not right.</p>

<pre><code>int readswitch()
{
 int S1,S2,S3,S4;
 uint8_t data = 255;
 data |= digitalRead(S4)&lt;&lt;3;
 data |= digitalRead(S3)&lt;&lt;2;
 data |= digitalRead(S2)&lt;&lt;1;
 data |= digitalRead(S1)&lt;&lt;0;

  if (data==0b11111110) return 1; else
  if (data==0b11111101) return 2; else
  if (data==0b11111011) return 3; else
  if (data==0b11110111) return 4; else
  //add another condition here if you want
  return 0;
}

int laststate = 0;
int state = 0;
int rotation;

void setup()
{
  //setup here, including LCD initialization
  //..
  //..
  //..
}

void updateLCD(int x,int y)
{
  lcd.clear();
  //displaying pin status
  lcd.print("dip : ");
  lcd.print(x,DEC);
  lcd.print(" rot : ");
  lcd.print(y,DEC); 
}

void loop()
{
  state = readswitch();
  if (state!=laststate)
  {
    if (state == 1) 
    {
      rotation = 5;
      for (int i = 0; i&lt; rotation; i++)
        {
         //update lcd
         updateLCD(state,(rotation-i));

         //.. servo rotation
         //..
        }
      //...
    } else

    if (state == 2) 
    {
      //do what you want here if switch 2 == LOW
      //...
    } else

    if (state == 3) //switch 1 == LOW
    {
      //do what you want here if switch 3 == LOW
      //...
    } else

    if (state == 4) //switch 1 == LOW
    {
      //do what you want here if switch 4 == LOW
      //...
    } 
  }
  laststate = state;

}
</code></pre>

<p>For more information about LCD, you can refer <a href="https://www.arduino.cc/en/Tutorial/LiquidCrystalScroll" rel="nofollow noreferrer">to this documentation</a></p>
