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
}
}