#include <Stepper.h> // include the Stepper library
int current_degree = 0;
const int stepsPerRevolution = 200; // change this value to match the number of steps per revolution of your motor
const float degreesPerStep = float(360) / float(stepsPerRevolution); // calculate the number of degrees per step
// initialize the stepper library with the number of steps per revolution and the relevant pins
Stepper myStepper(stepsPerRevolution, 15, 2, 4, 5);
void setup() {
Serial.begin(115200);
// set the speed of the motor in revolutions per minute
myStepper.setSpeed(10);
}
void loop() {
// generate a random number between 0 and 360
int randomDegree = random(360);
Serial.print("sudut baru : ");
Serial.println(randomDegree);
// if(){
if(randomDegree > current_degree){
int move_degree = randomDegree - current_degree;
int per = move_degree - 360;
Serial.print("step clock wise : ");
Serial.println(move_degree);
Serial.print("step invert clock wise : ");
Serial.println(per);
}else{
int move_degree = current_degree - randomDegree;
int per = move_degree - 360;
Serial.print("step clock wise : ");
Serial.println(move_degree);
Serial.print("step invert clock wise : ");
Serial.println(per);
}
// if(move_degree < (move_degree-360)){
// int direct = move_degree/degreesPerStep;
// myStepper.step(direct);
// }else{
// int direct = move_degree/degreesPerStep;
// myStepper.step(-direct);
// }
// }else{
// int move_degree = randomDegree - current_degree;
// if(move_degree < (move_degree-360)){
// int direct = move_degree/degreesPerStep;
// myStepper.step(-direct);
// }else{
// int direct = move_degree/degreesPerStep;
// myStepper.step(direct);
// }
// }
// move the motor to the random degree
//myStepper.step((float(stepsPerRevolution))/ float(360) * float(randomDegree));
//myStepper.step( randomDegree/ degreesPerStep);
// delay(500);
// move the stepper 10 degrees in the other direction
//myStepper.step(-randomDegree / degreesPerStep);
// delay(500);
current_degree = randomDegree;
// pause for 2 seconds
delay(2000);
}