// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int enPin = 2;
const int homeSwitchPin = 12;
//from here are variables for change of values (0 and 1)
int speedState = 0;
int speedswitch = 13;
int buttonNew;
int buttonOld=1;
void setup() {
buttonNew=digitalRead(speedswitch);
Serial.begin(9600);
Serial.println(speedState);
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(homeSwitchPin , INPUT);
pinMode(enPin,OUTPUT);
pinMode(speedswitch,INPUT);
digitalWrite(enPin,LOW);
// Set Dir to Home switch
digitalWrite(dirPin,LOW); // sets pin 4 as ready to send AntiClockWise signal to stepper
}
void loop() {
int homeSw = digitalRead( homeSwitchPin); // checks if switch on pin 12 is pressed
if( homeSw == HIGH && (digitalRead(dirPin) == LOW) ){ //if limit switch is NOT pressed and pin 4 is set as Anticlockwise
motorStep(1); // rotates one step
}
else if( homeSw == LOW && (digitalRead(dirPin) == LOW) ){ //if limit switch IS pressed and pin 4 is set as Anticlockwise
digitalWrite(dirPin,HIGH); // sets pin 4 as ready to send Clockwise signal to stepper
}
if (homeSw == LOW && (digitalRead(dirPin) == HIGH)){ // If pin 4 is set as Clockwise
motorStep(200); //precise angle of rotation
buttonNew=digitalRead(speedswitch);
if(buttonOld==0 && buttonNew==1){
if (speedState==0){
motorStep(1000);
digitalWrite(dirPin,HIGH);
// sets pin 4 as ready to send Clockwise signal to stepper
speedState=1;
Serial.println(speedState);
}
else{
speedState=0;
}
}
}
}
void motorStep( int MAX){// start of speed of rotation code
for(int x = 0; x < MAX; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin,LOW);
delayMicroseconds(2000);
}// start of speed of rotation code
}