#define encoder_a 2
#define encoder_b 3
#define motor_step 4
#define motor_direction 5
volatile long motor_position, encoder;
void setup ()
{
//set up the various outputs
pinMode(motor_step, OUTPUT);
pinMode(motor_direction, OUTPUT);
// then the encoder inputs
pinMode(encoder_a, INPUT);
pinMode(encoder_b, INPUT);
digitalWrite(encoder_a, HIGH);
digitalWrite(encoder_b, HIGH);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(0, encoderPinChangeA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(1, encoderPinChangeB, CHANGE);
encoder = 0;
}
void encoderPinChangeA()
{
if (digitalRead(encoder_a) == digitalRead(encoder_b))
{
encoder--;
}
else
{
encoder++;
}
}
void encoderPinChangeB()
{
if (digitalRead(encoder_a) != digitalRead(encoder_b))
{
encoder--;
}
else
{
encoder++;
}
}
void loop()
{
if (encoder > 0)
{
digitalWrite(motor_direction, HIGH);
digitalWrite(motor_step, HIGH);
digitalWrite(motor_step, LOW);
motor_position++;
encoder = 0;
}
else if (encoder < 0)
{
digitalWrite(motor_direction, LOW);
digitalWrite(motor_step, HIGH);
digitalWrite(motor_step, LOW);
motor_position--;
encoder = 0;
}
}
This is the code I wrote.
Stepper motor rotates when encoder rotates.
But I want the motor to make 100 turns when the encoder makes a full rotation.
We can rotate the encoder slowly.
In this code , One turn of the encoder makes the step motor 2 turns.
I'm going to use it on a wind turbine.
The instrument has an encoder inside the wind direction meter. with the rotation of this encoder, the stepper motor will be activated. but it doesn't have to turn all the time it can check every 5 minutes.
Because the engine must be rested.
I used the stepping motor to control the wind turbine blades.
The stepper motor will direct the wind turbine blades according to the wind.
The reason I want 100 times is our proportion.
How should I make changes to this code ?