Created
September 14, 2014 20:48
-
-
Save matteocaberlotto/662d3b7ffa87b16570e2 to your computer and use it in GitHub Desktop.
Arduino sketch for unipolar stepper motors (to be used with uln2803)
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 motorPins[] = {11, 10, 9, 8}; | |
int count = 0; | |
int count2 = 0; | |
int delayTime = 500; | |
int val = 0; | |
int ledPin = 13; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(ledPin, OUTPUT); | |
for (count = 0; count < 4; count++) { | |
pinMode(motorPins[count], OUTPUT); | |
} | |
} | |
void moveForward() { | |
if ((count2 == 0) || (count2 == 1)) { | |
count2 = 16; | |
} | |
count2>>=1; | |
for (count = 3; count >= 0; count--) { | |
digitalWrite(motorPins[count], count2>>count&0x01); | |
} | |
delay(delayTime); | |
} | |
void moveBackward() { | |
if ((count2 == 0) || (count2 == 1)) { | |
count2 = 16; | |
} | |
count2>>=1; | |
for (count = 3; count >= 0; count--) { | |
digitalWrite(motorPins[3 - count], count2>>count&0x01); | |
} | |
delay(delayTime); | |
} | |
void loop() { | |
val = analogRead(0); | |
Serial.println(val); | |
if (val > 540) { | |
// move faster the higher the value from the potentiometer | |
delayTime = 50 * (1023 - val) / 483; | |
moveForward(); | |
} else if (val < 484) { | |
// move faster the lower the value from the potentiometer | |
delayTime = val * 50 / 484; | |
moveBackward(); | |
} else { | |
delayTime = 1024; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment