1

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.

3 Answers 3

1

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

0

If you put the sweep-code in the setup function, it would execute once (because setup() is only called once) and you would see what you expected.

But, as it is in the loop() function, it executes each time the loop() function executes - that is the point of the loop() function. As another alternative, you could put an infinite- or forever loop

for(;;)
   ;    // program hangs here forever

at the end of the loop() function to prevent it returning and getting called again. Either way, you would see a single pass over the steps you coded.

0

Your switch 2 code includes a myservo.detach() at the end which prevents any further server code from operating.

If you want the switch 1 code to execute only once, you could add a myservo.detach() to the block.

You also seem to want to recycle for the appropriate number of times if you clear the switch. For this, you may want to myservo.attach(9) when all the switches are open.

Alternately, you could avoide the detach() and attach() by adding a one-shot flag to the conditionals, set it inside the conditional, and clear it when the switches are clear.

For example:

   if (!s1state & !s2state & !s3state & !s4state) armed = 1;
   ...

   if (armed & s1state == LOW) {     // read the stored input pin value
       armed = 0;
       ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.