Navigation Menu

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/9455081 to your computer and use it in GitHub Desktop.
Save johan-bjareholt/9455081 to your computer and use it in GitHub Desktop.
int directionPin = 0;
int activationPin = 1;
int btnPin = 2;
int directionState = 0;
int btnState = 0;
int currentState = LOW;
int currentPosition = 0;
int maxPosition = 158;
void setup(){
pinMode(directionPin, OUTPUT);
pinMode(activationPin, OUTPUT);
pinMode(btnPin, INPUT);
//Serial.begin(9600);
}
void loop(){
int tonearray[] = {30578, 28861, 27242, 25713, 24270, 22909, 21622, 20409, 19263, 18182, 17161, 16198};
for (int i; i<sizeof(tonearray); i++){
int starttime = millis();
while (millis() < starttime+1000){
updateDirection(activationPin, directionPin);
delay(10*40*tonearray[i]);
}
delay(1000);
}
}
void updateDirection(int pin, int direction_pin){
//Switch directions if end has been reached
if (currentPosition >= maxPosition) {
currentState = HIGH;
digitalWrite(direction_pin,HIGH);
}
else if (currentPosition <= 0) {
currentState = LOW;
digitalWrite(direction_pin,LOW);
}
//Update currentPosition
if (currentState == HIGH){
currentPosition--;
}
else {
currentPosition++;
}
//Pulse the control pin
digitalWrite(pin, currentState);
currentState = ~currentState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment