Servo control using DIP switch

Hi,
I am not an electronics guy, but I am trying to play around using Arduino with some ideas that strike my mind. I am trying to run a servo motor using Arduino Uno and control the number of sweeps through DIP switch. Could somebody please advise how the circuit has to be? What are the components?
Are there sample projects on DIP Switch? I was able to see only button switch projects.
Thanks in advance.

What are the components?

A DIP switch, a servo, and an Arduino.

Could somebody please advise how the circuit has to be?

A DIP switch is just a collection of switches in a compact package. Wire each switch independently.

Thank you Paul.
But, I will need resistors too, right?
It is a pity I am not able to find projects with Servo-DIP switch on internet.

v_amateur:
But, I will need resistors too, right?

No. Each switch connects between an Arduino pin and ground, You set each pin mode to INPUT_PULLUP and the internal resistor will pull the pin HIGH if the switch is open, it will be LOW if it is closed.

v_amateur:
It is a pity I am not able to find projects with Servo-DIP switch on internet.

Wonder why? :roll_eyes:

How many people you you imagine want to control a servo with a DIP switch. Why do you for that matter?

v_amateur:
It is a pity I am not able to find projects with Servo-DIP switch on internet.

The nice thing about the Arduino system is that there are millions of things you can do with it so it is unlikely that you will find someone with the exact same requirement as you.

However if you break your project into its parts you find an enormous amount of advice about switches and about servos. You just have to meld the two to meet your requirement.

…R
Planning and Implementing a Program

You’ll probably find this useful in the near-future.

Thank you Paul__B, Robin2 and AWOL. I am using an Arduino simulator to practice. Hope it saves my board from wrong connections.

Hi,
I managed to run the 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.

The re-run of the 5 sweeps with switch #1 is because of the nature of loop(), which, well, loops. So once the 5 sweeps are done, loop() goes again, sees the switch is still closed, 5 more sweeps.

To prevent that, you should consider what's usually known as a flag, a variable whose value can be set to show that something has happened or not. Let's say you call your flag a boolean "done5sweeps", initialise it as false to indicate you have not done 5 sweeps, at the top of the sketch. (Booleans have only 2 states, true and false)

bool done5sweeps=false;

Then when you read switch 1 and it's closed, before you dive off into the sweep, check in an if to see if you have done5sweeps yet. If not, do the sweeps, but also set done5sweeps true. That's so that next time thru loop(), it will see you have done5sweeps.

I saved this snippet some time ago when PaulS answered a similar question thus:

bool beenThereDoneThat = false;

if(!beenThereDoneThat)
{
    GoThereDoThat();
    beenThereDoneThat = true;
}

PS..... when you read switch #1 and it's open, you would probably want to set that flag back to false, to enable the 5 sweeps to work next time it finds the switch closed.