const int stepPin = 3;
const int dirPin = 2;
const int enPin = 4;
const int LimitSwitch_LEFT_Pin = 40;
const int LimitSwitch_RIGHT_Pin = 41;
const int led = 13;
const int led1 = 12;
const int ON_BUTTON = 12;
void setup() {
Serial.begin(9600);
pinMode(LimitSwitch_LEFT_Pin , INPUT_PULLUP);
pinMode(LimitSwitch_RIGHT_Pin , INPUT_PULLUP);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
// Set Dir to Home switch
pinMode(ON_BUTTON, INPUT_PULLUP);
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
}
void loop(){
int leftSw = digitalRead( LimitSwitch_LEFT_Pin);
int rightSw = digitalRead( LimitSwitch_RIGHT_Pin);
if( (leftSw == HIGH && (digitalRead(dirPin) == HIGH)) || (rightSw == HIGH && (digitalRead(dirPin) == LOW)) ){
motorStep(10);
}
else if( leftSw == LOW && (digitalRead(dirPin) == HIGH) ){
digitalWrite(dirPin,LOW);
// mandek();
delay(2000);
digitalWrite(led, LOW);
Serial.println("kanan");
}
else if( rightSw == LOW && (digitalRead(dirPin) == LOW ) ){
digitalWrite(dirPin,HIGH);
// mandek();
delay(2000);
digitalWrite(led, LOW);
Serial.println("kiri");
}
else{
Serial.println("mandek");
digitalWrite(led1, HIGH);
}
}
void motorStep(int MAX){
for(int x = 0; x < MAX; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
digitalWrite(led1, HIGH);
delayMicroseconds(500);
}
void mandek(){
digitalWrite(stepPin, LOW);
digitalWrite(dirPin, LOW);
}