const int STEP_PIN = 3;
const int DIR_PIN = 4;
const int limitSwitch = 7;
const int buttonPlay = 6;
const int buttonStop = 5;
const int DELAY_STEP = 1000;
bool stringStatus = false;
bool isPlay = false;
const int buzz = 2;
void setup() {
Serial.begin(9600);
pinMode(limitSwitch, INPUT);
pinMode(buttonPlay, INPUT);
pinMode(buttonStop, INPUT);
pinMode(buzz, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
}
void maju() {
digitalWrite(DIR_PIN, HIGH);
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(DELAY_STEP);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(DELAY_STEP);
}
void mundur() {
digitalWrite(DIR_PIN, LOW);
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(DELAY_STEP);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(DELAY_STEP);
}
void stopMotor() {
digitalWrite(STEP_PIN, LOW);
}
void play() {
if (digitalRead(buttonPlay) == HIGH) {
isPlay = true;
Serial.println("play");
delay(250);
}
}
void stop() {
if (digitalRead(buttonStop) == HIGH) {
isPlay = false;
Serial.println("stop");
delay(250);
}
}
void status() {
if (digitalRead(limitSwitch) == HIGH) {
stringStatus = true;
Serial.println("safe");
}
else {
stringStatus = false;
}
}
void running() {
play();
stop();
status();
if (isPlay == true && stringStatus == true) {
maju();
}
else if(isPlay == true && stringStatus == false){
digitalWrite(buzz, HIGH);
Serial.println("putus");
}
else {
digitalWrite(buzz, LOW);
stopMotor();
}
}
void loop() {
running();
}