Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Indented OP's code
Source Link
JRobert
  • 15.4k
  • 3
  • 25
  • 53
#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);  
      }

   }
  }
#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);  
  }

  }
  }
#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);  
      }

   }
}
Source Link

Servo control using DIP switch

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.