Last active
August 29, 2015 13:57
-
-
Save johan-bjareholt/9455959 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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