Triggering a servo to repeat an action

I am currently working on a project for a pill dispenser. The feeding mechanism works similarly to a gun in that a slide with a slot moves over, a pill drops in from a hopper, and then the slide moves to the dispensing area where the pill drops out of the device. The slide is actuated by a servo that spins to pull the slide to the dispensing position and then retracts back to the hopper to get another pill. I have written some code that allows the system to be triggered by a button, I've included the code below.

I am currently trying to rewrite my code to use a four-position rotary switch so that I can, with a single button press, dispense the desired number of pills. Help with the code and a simple diagram of how your method incorporates the rotary switch would be helpful.

I have used a rotary switch in series with a button in the past resulting in the Arduino effectively recognizing multiple buttons but I am open to new methods.

#include <Servo.h>

Servo myservo;  
#define servoPin 3 //~
#define pushButtonPin 2 

int angle =179;   
int angleStep = 10;
const int minAngle = 0;
const int maxAngle = 180;

const int type =2;

int buttonPushed =0;

void setup() {
  Serial.begin(9600);          //  setup serial
  myservo.attach(servoPin);  // attaches the servo on pin 3 to the servo object
  pinMode(pushButtonPin,INPUT_PULLUP);
   Serial.println("Robojax Servo Button ");
   myservo.write(angle);//initial position
}

void loop() {
  if(digitalRead(pushButtonPin) == LOW){
    buttonPushed = 1;
  }
   if( buttonPushed ){
  // change the angle for next time through the loop:
  angle = angle - angleStep;

    // reverse the direction of the moving at the ends of the angle:
    if (angle <= minAngle) {
      angleStep = -angleStep;
       if(type ==1)
        {
            buttonPushed =0;       
        }
    }

        if (angle >= maxAngle) {
      angleStep = -angleStep;
        if(type ==2)
        {
            buttonPushed =0;                   
        }
    }

    myservo.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");    
  delay(100); // waits for the servo to get there
   }

  
}

What does the Serial.print tell?
To make the best presentation use autoformat in the IDE and code tags here when pasting.

Thank you for the advice.

I modified another set of code and the serial.print is from that code. I am unsure what it is doing.

    Serial.print("Moved to: ");
    Serial.print(angle);   // print the angle
    Serial.println(" degree");

They print out the variable "angle" assuming You have activated serial monitor in the IDE.

Use a rotary encoder to increment/decrement the pill count variable.  Limit the high and low values with code.  Employ the encoder's built-in pushbutton, or a discrete button, to start the dispense cycle.

My current difficulty isn't necessarily incorporating a rotary switch. My main issue is how to repeat the dispensing action. The way I have coded it is simply including a <= or >= argument which moves it in the appropriate direction if the angle is between min and max angles.

Is there a way to get the servo to repeat the action easily?

A general outline: Gather everything which constitutes the actuation sequence and put it in a finite state machine.

Start with just one button push initiating one cycle.  To finish, put together a loop which counts how many cycles have been done.  If not done continue initiating cycles.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.