Skip to content

Instantly share code, notes, and snippets.

@johan-bjareholt
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johan-bjareholt/9455959 to your computer and use it in GitHub Desktop.
Save johan-bjareholt/9455959 to your computer and use it in GitHub Desktop.
int directionPin = 0;
int activationPin = 1;
boolean activationState = LOW;
boolean directionState = LOW;
int currentPosition = 0;
int maxPosition = 1;
void setup(){
pinMode(directionPin, OUTPUT);
pinMode(activationPin, OUTPUT);
}
void loop(){
if (currentPosition >= maxPosition) {
activationState = HIGH;
}
else if (currentPosition <= 0) {
activationState = LOW;
}
if (activationState == HIGH){
currentPosition--;
}
else {
currentPosition++;
}
digitalWrite(directionPin,activationState);
delay(10);
}
void tick() {
//Switch directions if end has been reached
if (currentPosition >= maxPosition) {
activationState = HIGH;
}
else if (currentPosition <= 0) {
activationState = LOW;
}
digitalWrite(directionPin,activationState);
//Update currentPosition
if (activationState == HIGH){
currentPosition--;
}
else {
currentPosition++;
}
//Pulse the control pin
digitalWrite(activationPin, activationState);
activationState = ~activationState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment