// Define pin connections & motor's steps per revolution
#define dirPin 2
#define stepPin 3
#define LIMIT_SWITCH_PIN1 5
#define LIMIT_SWITCH_PIN2 6
#define select_pin_1 8
#define select_pin_2 9
#define select_pin_3 10
int LIMIT_SWITCH_1 = 0;
int LIMIT_SWITCH_2 = 0;
int select_1 = 0;
int select_2 = 0;
int select_3 = 0;
int wantedPos = 0;
int currentPos = 0;
int steps_to_go = 0;
void setup()
{
Serial.begin(9600);
// set limit switches as input
pinMode(LIMIT_SWITCH_PIN1, INPUT);
pinMode(LIMIT_SWITCH_PIN2, INPUT);
// set input buttons as input
pinMode(select_pin_1, INPUT);
pinMode(select_pin_2, INPUT);
pinMode(select_pin_3, INPUT);
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
//homing ();
}
void loop()
{
update_and_print_Sensors();
while (LIMIT_SWITCH_1 == 0 ) {
Serial.print("Moving towards LS1 ");
digitalWrite(dirPin, HIGH);//direction right
movestep(5000);
update_and_print_Sensors();
} // end while
while (LIMIT_SWITCH_2 == 0 ) {
Serial.print("Moving towards LS2 ");
digitalWrite(dirPin, LOW);//direction right
movestep(5000);
update_and_print_Sensors();
} // end while
} // end void loop
/*--------------- MY FUNCTIONS ------------------*/
/*--------------- MY FUNCTIONS ------------------*/
/*--------------- MY FUNCTIONS ------------------*/
void movestep(int mehirut) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(mehirut);
digitalWrite(stepPin, LOW);
delayMicroseconds(mehirut);
}
void update_and_print_Sensors() {
LIMIT_SWITCH_1 = digitalRead(LIMIT_SWITCH_PIN1);
LIMIT_SWITCH_2 = digitalRead(LIMIT_SWITCH_PIN2);
Serial.print("LIMIT_1: ");
Serial.print(LIMIT_SWITCH_1);
Serial.print("||");
Serial.print("LIMIT_2: ");
Serial.print(LIMIT_SWITCH_2);
Serial.println();
}
void homing (){
update_and_print_Sensors();
while (LIMIT_SWITCH_1 == 0 ) {
Serial.print("Moving to home position while LS1 is 0 || ");
digitalWrite(dirPin, HIGH);//direction right
movestep(5000);
update_and_print_Sensors();
} // end while
Serial.println("homing position Aquired ");
currentPos = 0;
Serial.print("thge Current position is: ");
Serial.print(currentPos);
Serial.println();
} // end homing
void Go_between_two_positions() {
update_and_print_Sensors();
while (LIMIT_SWITCH_1 == 0 ) {
Serial.print("Moving towards LS1 ");
digitalWrite(dirPin, HIGH);//direction right
movestep(5000);
update_and_print_Sensors();
} // end while
while (LIMIT_SWITCH_2 == 0 ) {
Serial.print("Moving towards LS2 ");
digitalWrite(dirPin, LOW);//direction right
movestep(5000);
update_and_print_Sensors();
} // end while
} // end Go_between_two_positions()
/*
if (digitalRead(select_pin_1) == 1){
wantedPos = 100;
Serial.println("YOU HAVE CHOSEN TO GO TO POSITION 100 ");
}
if (digitalRead(select_pin_2) == 1){
wantedPos = 200;
Serial.println("YOU HAVE CHOSEN TO GO TO POSITION 200 ");
}
if (digitalRead(select_pin_3) == 1){
wantedPos = 300;
Serial.println("YOU HAVE CHOSEN TO GO TO POSITION 300 ");
}
delay (1500);
if (wantedPos != currentPos){ // if we need to move
Serial.print("LETS GO!");
if ((wantedPos - currentPos) > 0 ){ // if to move forward
Serial.println("forward");
steps_to_go = wantedPos -currentPos;
for (int i = 0; i< steps_to_go ; i++){
digitalWrite(dirPin, HIGH);
movestep(5000);
currentPos ++;
Serial.print("current pos is: ");
Serial.println(currentPos);
}
} // end if positive direction
if ((wantedPos - currentPos) < 0 ){ // if to move backwards
Serial.println("backward");
steps_to_go = abs( wantedPos -currentPos);
for (int i =steps_to_go; i> 0; i--){
digitalWrite(dirPin, LOW);
movestep(5000);
currentPos --;
Serial.print("current pos is: ");
Serial.println(currentPos);
}
} // end if positive direction
}// end if
delay (1500);
Serial.println("PLEASE ENTER NEXT POSITION ");
delay (1500);
*/