Stepper motor velocidade variavel por potenciometro

test_ULN2003_speed1

Rotação num sentido com velocidade variavel definida por um potenciometro.

Este exemplo não faz uso de nenhuma biblioteca.

arduino D8 / Blue – ULN2003 pin 1
arduino D9 / Pink – ULN2003 pin 2
arduino D10 / Yellow – ULN2003 pin 3
arduino D11 / Orange – ULN2003 pin 4

O potenciometro tem os pinos de entrada ligados aos +5V e a GND e o de saida ligado no arduino ao pino A0

 

int motorPins[] = {8, 9, 10, 11};
int count = 0;
int count2 = 0;
int delayTime = 500;
int val = 0;

void setup() {
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);
if (val > 540) {
// move faster the higher the value from the potentiometer
delayTime = 2048 - 1024 * val / 512 + 1;
moveForward();
} else if (val < 480) {
// move faster the lower the value from the potentiometer
delayTime = 1024 * val / 512 + 1;
moveBackward();
} else {
delayTime = 1024;
}
}