Skip to content

Instantly share code, notes, and snippets.

@syukronrm
Last active December 13, 2019 23:02
Show Gist options
  • Save syukronrm/860b791b27414555bc827564528e7d5d to your computer and use it in GitHub Desktop.
Save syukronrm/860b791b27414555bc827564528e7d5d to your computer and use it in GitHub Desktop.
int pin1, pin2, pin3;
int currentPos = 0; // posisi derajat aervo
int currentState = NULL;
int SPIN = 1;
int STOP = 2;
int RESET = 3;
loop() {
pin1 = digitalRead(PIN1);
pin2 = digitalRead(PIN2);
pin3 = digitalRead(PIN3);
if (pin1 == HIGH) {
currentState = SPIN;
} else if (pin2 == HIGH) {
currentState = STOP;
} else if (pin3 == HIGH) {
currentState = RESET;
}
Serial.print("State: ");
Serial.println(currentState);
if (currentState == SPIN) {
// putar terus sampe 180°
if (currentPos + 1 <= 180) {
currentPos += 1;
myservo.write(currentPos);
}
} else if (currentState == STOP) {
// do nothing
} else if (currentState == RESET) {
// balikin ke 0°
if (currentPos - 1 >= 0) {
currentPos -= 1;
myservo.write(currentPos);
}
}
delay(15);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment