#include "RotaryEncoder.h"
int Counter = 0, LastCount = 0; //uneeded just for test
void RotaryChanged(); //we need to declare the func above so Rotary goes to the one below
RotaryEncoder Rotary(&RotaryChanged, 2, 3, 4); // Pins 2 (DT), 3 (CLK), 4 (SW)
int coil[4] = {11,10,9,8}; //Definir pines utilizados en el arduino para controlar el motor
int states[8][4] = {{1,0,0,0},
{1,0,1,0},
{0,0,1,0},
{0,1,1,0},
{0,1,0,0},
{0,1,0,1},
{0,0,0,1},
{1,0,0,1}};
int states2[4][4] = {{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}};
int timer = 10;
int estados = 0;
void RotaryChanged()
{
const unsigned int state = Rotary.GetState();
if (state & DIR_CW)
{ Counter--; }
if (state & DIR_CCW)
{Counter++;
MoverMotorHorario();
}
}
void setup()
{
Rotary.setup();
Serial.begin(9600);
Serial.println("Rotary Encoder Tests");
//Definir los pines del arduino como salidas
for (int i=0; i<4;i++){
pinMode(coil[i],OUTPUT);
digitalWrite(coil[i],LOW);
Serial.print("Coil: ");
Serial.print(coil[i]);
Serial.println(" activated as output");
delay(125);
}
}
void loop()
{
if (Rotary.GetButtonDown()){
Serial.println(Counter);
}
if (LastCount != Counter)
{
Serial.println(Counter);
LastCount = Counter;
}
}
//Funcion para Mover el Motor.
void MoverMotorHorario()
{
// Secuencia de medio paso en sentido horario
//for(int a = 0; a < 1; a++){
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
digitalWrite(coil[j], states2[i][j]);
}
delay(timer);
}
//}
}