
#define IN1 3  //Connected to Pin 3
#define IN2 9  //Connected to Pin 9
#define IN3 10 //Connected to Pin 10
#define IN4 11 //Connected to Pin 11

#define Switch 6 //Connected to Pin 6
#define DEL 10 //Delay of 10 milliseconds

int current_state = HIGH; //Variable used to toggle device. Assigned to be toggled "off" initally.
int switch_value; // Stores current value of the Switch
int previous_value = LOW; //Stores previous value of the switch

long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(Switch, INPUT); 
}

void loop()
{
    switch_value=digitalRead(Switch); //Read the value of the switch
    
    if (switch_value == HIGH && previous_value == LOW && millis() - time > debounce) // If the button is pressed, the device will either toggle on or off
    {
       if (current_state == HIGH) //If the device is off   
      
           current_state = LOW; // Toggle On the device
       else
           current_state = HIGH; //Toggle off the device

           time = millis();    
    }
      if (current_state==LOW){ // If the button is toggled "On", the stepper motor will move. 
          digitalWrite(IN2, HIGH); //IN2 and IN4 are set to HIGH
          digitalWrite(IN4, HIGH);
          delay(DEL);
          digitalWrite(IN2, LOW);//IN4 and IN1 are set to HIGH 
          digitalWrite(IN1, HIGH);
          delay(DEL);
          digitalWrite(IN4, LOW);//IN1 and IN3 are set to HIGH
          digitalWrite(IN3, HIGH);
          delay(DEL);
          digitalWrite(IN1, LOW);//IN3 and IN2 are set to HIGH
          digitalWrite(IN2, HIGH);
          delay(DEL);
          digitalWrite(IN3, LOW);//IN2 is HIGH
      }

  previous_value = switch_value; //Stores the current value of the switch
}
    
 
