Skip to content

Instantly share code, notes, and snippets.

@matteocaberlotto
Created September 14, 2014 20:48
Show Gist options
  • Save matteocaberlotto/662d3b7ffa87b16570e2 to your computer and use it in GitHub Desktop.
Save matteocaberlotto/662d3b7ffa87b16570e2 to your computer and use it in GitHub Desktop.
Arduino sketch for unipolar stepper motors (to be used with uln2803)
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