#include <AccelStepper.h>
#include <Servo.h>
#define dirPin 3
#define stepPin 5
#define motorInterfaceType 1
int limitpin = 2;
int coinpin = 4;
int toppin = 7;
Servo beyazservo;
int coinval;
int topval;
long initial_homing=-1;
int servopos = 0;
int pos = 0;
//1/16 microstep
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
Serial.begin(9600); // Start the Serial monitor with speed of 9600 Bauds
pinMode(limitpin, INPUT_PULLUP);
beyazservo.attach(9);
delay(5); // Wait for EasyDriver wake up
stepper.setMaxSpeed(5.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(5.0); // Set Acceleration of Stepper
Serial.print("Stepper is Homing . . . . . . . . . . . ");
while (digitalRead(limitpin)) { // Make the Stepper move CCW until the switch is activated
stepper.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepper.run(); // Start moving the stepper
delay(5);
}
stepper.setCurrentPosition(0); // Set the current position as zero for now
stepper.setMaxSpeed(5.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(5.0); // Set Acceleration of Stepper
initial_homing=1;
while (!digitalRead(limitpin)) { // Make the Stepper move CW until the switch is deactivated
stepper.moveTo(initial_homing);
stepper.run();
initial_homing++;
delay(5);
}
stepper.setCurrentPosition(0);
Serial.println("Homing Completed");
Serial.println("");
stepper.setMaxSpeed(1000.0); // Set Max Speed of Stepper (Faster for regular movements)
stepper.setAcceleration(1000.0); // Set Acceleration of Stepper
}
void loop() {
coinval = digitalRead(coinpin);
topval = digitalRead(toppin);
Serial.println(coinval);
Serial.println(topval);
if (coinval>0)
{
start();
}
else if (topval>0)
{
beyazkapat();
}
else{
Serial.println("wait");
delay(5);
return;
}
}
void start(){
Serial.println("start");
stepper.moveTo(-2400);
stepper.runToPosition();
servoacik();
delay (5000);
stepper.moveTo(0);
stepper.runToPosition();
delay(500);
return;
}
void servoacik(){
for (servopos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
beyazservo.write(servopos); // tell servo to go to position in variable 'pos'
delay(15);
return ;
}
}
void beyazkapat(){
for (servopos = 90; pos <= 0; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
beyazservo.write(servopos); // tell servo to go to position in variable 'pos'
delay(15);
}
}