// Stepper motor on Wokwi!

#include <AccelStepper.h>
#include <Stepper.h>

#define Step 47
#define Reset 49 
#define Clockwise 51 
#define Counterclockwise 53

const int stepPin = 3;
const int dirPin = 2; 

const int stepsPerRevolution = (995 / 1.8) * 4 ; 
const int stepsPerRevolution_counter = (1053 / 1.8) * 4;  

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, stepPin, dirPin);
Stepper myStepper_counter(stepsPerRevolution_counter,  stepPin, dirPin);

int stepCount = 0;      
int stepCount_reverse = 0; 

void(* resetFunc) (void) = 0;

void setup() {
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
  myStepper.step(13); // Add step para ma zero ang degree
}

void loop() {

    if( digitalRead(Clockwise) == LOW)
    {

      if( digitalRead(Step) == LOW && digitalRead(Reset) == HIGH)
      {        
             
        int sensorReading = analogRead(A0);
        int motorSpeed = map(sensorReading, 0, 1023, 0, 60);
        if (motorSpeed > 0 && stepCount <= stepsPerRevolution)
        {
            myStepper.setSpeed(motorSpeed);
            myStepper.step(1);
            stepCount += 1.8;
        }

      }
      if( digitalRead(Reset) == LOW && digitalRead(Step) == HIGH)
      {
        int sensorReading = analogRead(A0);
        int motorSpeed = map(sensorReading, 0, 1023, 0, 60);
        if (stepCount_reverse <= stepsPerRevolution)
        {
            myStepper.setSpeed(motorSpeed);
            myStepper.step(-1);
            stepCount_reverse += 1.8;
        } 

        // 3.6 degree ang answer

       // Add ang ubos nga if statement para mabalik nasab ug 0 degree

        if (stepCount_reverse == 2212)
        {
            myStepper.step(-10); 
            stepCount_reverse += 1;
        }

      }

      // if goods na minusan ang step nga 0 degree kay para ma -3.6 which inig reset e add siya sa 3.6 steps sa setup 
      if( digitalRead(Reset) == HIGH && digitalRead(Step) == HIGH && stepCount_reverse == 2213 )
      {
        myStepper.step(-13);
        resetFunc();
      }
    }

  if( digitalRead(Counterclockwise) == LOW)
  {
      if( digitalRead(Step) == LOW && digitalRead(Reset) == HIGH)
      {        
             
        int sensorReading = analogRead(A0);
        int motorSpeed = map(sensorReading, 0, 1023, 0, 60);
        if (motorSpeed > 0 && stepCount <= stepsPerRevolution_counter)
        {
            myStepper.setSpeed(motorSpeed);
            myStepper.step(-1);
            stepCount += 1.8;
        }

      }
      if( digitalRead(Reset) == LOW && digitalRead(Step) == HIGH)
      {
        int sensorReading = analogRead(A0);
        int motorSpeed = map(sensorReading, 0, 1023, 0, 60);
        if (stepCount_reverse <= stepsPerRevolution_counter)
        {
            myStepper.setSpeed(motorSpeed);
            myStepper.step(1);
            stepCount_reverse += 1.8;
        }
        if (stepCount_reverse == 2341)
        {
            myStepper.step(-23);
            stepCount_reverse += 1;
        }
      }

      if( digitalRead(Reset) == HIGH && digitalRead(Step) == HIGH && stepCount_reverse == 2342 )
      {
        myStepper.step(-17);
        resetFunc();
      }
  }

}


A4988