You are on page 1of 2

//int num_paso = 1;

//int i;
int thisStep = 1;
//int DIR;
//void paso(int);
//int veloc;

int motor_pin_1 = 8;
int motor_pin_2 = 9;
int motor_pin_3 = 10;
int motor_pin_4 = 11;

void setup() {
// set the speed of the motor to 30 RPMs
Serial.begin(9600);
pinMode(motor_pin_1, OUTPUT);
pinMode(motor_pin_2, OUTPUT);
pinMode(motor_pin_3, OUTPUT);
pinMode(motor_pin_4, OUTPUT);
}
void loop() {
// get the sensor value
int val = analogRead(0);

if ((val > 380)&& (val < 410));


else if (val >= 410) paso_SIMPLE(2,1,(int(((780-val)/10))));
else if (val <= 380 ) paso_SIMPLE(2,0,(int(abs(-val)/10)));
/*
paso_SIMPLE(200, 1, 10);
delay (200);
paso_SIMPLE(400, 0, 10);
delay (200);
paso_SIMPLE(200, 1, 10);
delay (200);
paso_SIMPLE(30, 1, 10);
delay (200);
paso_SIMPLE(60, 0, 10);
delay (200);
paso_SIMPLE(30, 1, 10);
delay (200);
*/
Serial.println(val);
delay(10);
}
/*
FUNCION DE CONTROL DEL MOTOR PASO A PASO
Se entra con tres parámetros, pasos, que es el número de pasos que queremos
realizar,
DIR, que es la dirección en la que queremos dar los pasos y veloc, que es el
valor del delay entre pasos, es decir la velocidad a la que queremos que se mueva
el motor..

*/
void paso_SIMPLE(int pasos, int DIR, int veloc) {

int r;
for (r = 1 ; r < pasos; r++) {

if (DIR == 1 ) {
thisStep++;
if (thisStep > 3)thisStep = 0;
}
else if (DIR == 0) {
thisStep-- ;
if (thisStep < 0)thisStep = 3;
}

switch (thisStep) {
case 0: // 1010
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, LOW);
break;
case 1: // 0110
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, LOW);
break;
case 2: //0101
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
break;
case 3: //1001
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
break;
}
// Serial.println(thisStep);
delay(veloc);
}
//return;
}

You might also like