const int Dir_pin = 18;
const int step_pin = 19;
const float stepAngle = 1.8; // Step angle of the motor in degrees
int moveCount = 0; // Variable to count the number of movements
int secondPass = 0;
int thirdPass = 9;
int defaultAngle = 0;
void setup() {
pinMode(Dir_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Move the stepper motor clockwise 17 times, each time by 9°
defaultAngle += 153;
// start moving to number 23
moveStepperClockwise(defaultAngle);
delay(100); // Delay between movements
// After moving 17 times, move the motor counter-clockwise by 360°
defaultAngle -= 360+secondPass;
moveStepperCounterClockwise(360+secondPass);
delay(100); // Delay before stopping the motor
defaultAngle += thirdPass;
moveStepperClockwise(thirdPass);
delay(100);
//check if the password correct
Serial.println("-------------------------------");
Serial.println(" check if the password correct ");
Serial.println("-------------------------------");
delay(3000);
Serial.println(" ");
Serial.println(" Wrong Password!!! ");
Serial.println(" ");
//if(!password)
if(defaultAngle <= 0){
moveStepperClockwise( abs(defaultAngle) );
}else {
moveStepperClockwise( abs( 360- defaultAngle) );
}
Serial.print("defaultAngle: ");
Serial.println(defaultAngle);
Serial.print("SecondPassVar: ");
Serial.println(secondPass);
Serial.print("ThirdPassVar: ");
Serial.println(thirdPass);
if(thirdPass >= 360){
thirdPass = 9;
secondPass += 9;
} else {
thirdPass += 9;
}
// Stop the motor
/*while (true) {
}*/
defaultAngle = 0;
delay(1000);
}
// Function to move the stepper motor clockwise by a given angle
void moveStepperClockwise(float angle) {
int steps = angle / stepAngle;
digitalWrite(Dir_pin, HIGH); // Set direction to clockwise
for (int i = 0; i < steps; i++) {
digitalWrite(step_pin, HIGH);
delayMicroseconds(1000); // Adjust speed as needed
digitalWrite(step_pin, LOW);
delayMicroseconds(1000); // Adjust speed as needed
}
}
// Function to move the stepper motor counter-clockwise by a given angle
void moveStepperCounterClockwise(float angle) {
int steps = angle / stepAngle;
digitalWrite(Dir_pin, LOW); // Set direction to counter-clockwise
for (int i = 0; i < steps; i++) {
digitalWrite(step_pin, HIGH);
delayMicroseconds(1000); // Adjust speed as needed
digitalWrite(step_pin, LOW);
delayMicroseconds(1000); // Adjust speed as needed
}
}