I have written the following code for the above problem statement. But the motor rotates anticlokwise for about 2 seconds only. How can I control how long it rotates in the anti-clockwise direction?
(Please use the code tags button next time, </> on the menu.Thanks, Moderator)
// The following lines initiliase some of the pins of the Arduino Nano which are connected to the TB6612FNG motor driver IC
int pwm_a = 3; // Required for PWM
int ain_two = 4; // Input of the motor
int ain_one = 5; // Input of the motor
int stand_by = 6; // Standy pin which is required to turn the motor driver IC ON or OFF
// The following line initialises the digital pin 8 of the Arduino Nano connected to the micro-switch
int micro_switch = 8; // Used to control the motor
// The following line initialises the digital pin 9 of the Arduino Nano that will be connected to the receiver module
int receiver = 9;
// The following lines initialise variables that are being used in the code
int switch_value = 0; // This variable is used to read the state of the micro-switch (i.e. whether it has been pressed or not)
int receiver_value = 0; // This varible is used to read the state of the receiver
// The following variables are used to study the encoder
int encoder_one = 11;
int encoder_two = 12;
// The following variables are used to check the previous values of the encoder
boolean encoder_one_previous = LOW;
boolean encoder_two_previous = LOW;
// The following variable is used to store the decimal value of the previous state of the encoder
int encoder_decimal_previous = 0;
// The follwing variables are used to check the current values of the encoder
boolean encoder_one_current = LOW;
boolean encoder_two_current = LOW;
// The following variable is used to check the current value of the encoder
int encoder_decimal_current = 0;
// The following is the lookup table that we will use for the encoder
int encoder_lookup_table[4][3] = {
{0, 2, -1},
{1, 0, -1},
{2, 3, -1},
{3, 1, -1},
};
// The following variable keep a count of the number of rotations of the motor
int count = 0;
int i = 0;
void setup()
{
// The following lines set all the pins that we initialised above as output pins for the motor
pinMode(pwm_a, OUTPUT);
pinMode(ain_two, OUTPUT);
pinMode(ain_one, OUTPUT);
pinMode(stand_by, OUTPUT);
//The following line sets digital pin 8 of Arduino Nano as Input using the internal pull-up resistor
pinMode(micro_switch, INPUT_PULLUP);
//The following line sets digital pin 9 of Arduino Nano as Input
pinMode(receiver, INPUT);
// The following lines set digital pin 11 and 12 of Arduino Nano as input0
pinMode(encoder_one, INPUT);
pinMode(encoder_two, INPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
receiver_value = digitalRead(receiver); // We need to keep a track of the receiver value to know when the door should be opened
if (receiver_value == HIGH)
{
// When the door receives a HIGH signal from the receiver
turn_motor(100, 1); // If the switch is not pressed, the motor will turn clockwise and open the door
delay(2000);
switch_value = digitalRead(micro_switch); // We need to keep a track of the state of the switch to know when the door should be opened
if (switch_value == LOW)
{
// When the switch is pressed the motor will stop working
stop_motor();
delay(3000);
turn_motor(100, -1); // The motor turns anti-clockwise and closes the door
//delay(2000);
encoder();
delay(3000);
}
}
}
void turn_motor(int motor_speed, int motor_direction)
{
// The standy pin of TB6612FNG must be high for the circuit to work
digitalWrite(stand_by, HIGH);
/*digitalWrite(test, HIGH);
delay(100);
digitalWrite(test, LOW);
delay(100);*/
if (motor_direction == 1)
{
// Turning the motor clockwise
digitalWrite(ain_one, HIGH);
digitalWrite(ain_two, LOW);
analogWrite(pwm_a, motor_speed);
}
else if (motor_direction == -1)
{
// Turning the motor anti-clockwise
digitalWrite(ain_one, LOW);
digitalWrite(ain_two, HIGH);
analogWrite(pwm_a, motor_speed);
//encoder();
}
}
void stop_motor()
{
digitalWrite(stand_by, LOW);
}
void encoder()
{
count = 0;
while (1)
{
if (count == 1500)
{
stop_motor();
break;
}
if ((encoder_one_previous == 0) && (encoder_two_previous == 0))
{
encoder_decimal_previous = 0; // (00)2 = (0)10
}
else if ((encoder_one_previous == 0) && (encoder_two_previous == 1))
{
encoder_decimal_previous = 1; // (01)2 = (1)10
}
else if ((encoder_one_previous == 1) && (encoder_two_previous == 0))
{
encoder_decimal_previous = 2; // (10)2 = (2)10
}
else if ((encoder_one_previous == 1) && (encoder_two_previous == 1))
{
encoder_decimal_previous = 3; // (11)2 = (3)10
}
// Taking inputs from the encoder pins and saving them in the corresponding variables
encoder_one_current = digitalRead(encoder_one);
encoder_two_current = digitalRead(encoder_two);
if ((encoder_one_current == 0) && (encoder_two_current == 0))
{
encoder_decimal_current = 0; // (00)2 = (0)1
}
else if ((encoder_one_current == 0) && (encoder_two_current == 1))
{
encoder_decimal_current = 1; // (01)2 = (1)10
}
else if ((encoder_one_current == 1) && (encoder_two_current == 0))
{
encoder_decimal_current = 2; // (10)2 = (2)10
}
else if ((encoder_one_current == 1) && (encoder_two_current == 1))
{
encoder_decimal_current = 3; // (11)2 = (3)10
}
for (i = 0; i < 4; i++)
{
if (encoder_lookup_table[i][1] == encoder_decimal_current)
{
count -= -encoder_lookup_table[i][2];
}
}
encoder_one_previous = encoder_one_current;
encoder_two_previous = encoder_two_current;
}
}
The following is the code. While moving anti-clockwise the motor needs to move an object through 200mm. How can I calculate that distance using the encoder and how can I calculate the speed. My code shows speed = -1.00.
// The following lines initiliase some of the pins of the Arduino Nano which are connected to the TB6612FNG motor driver IC
int pwm_a = 3; // Required for PWM
int ain_two = 4; // Input of the motor
int ain_one = 5; // Input of the motor
int stand_by = 6; // Standy pin which is required to turn the motor driver IC ON or OFF
// The following line initialises the digital pin 8 of the Arduino Nano connected to the micro-switch
int micro_switch = 8; // Used to control the motor
// The following line initialises the digital pin 9 of the Arduino Nano that will be connected to the receiver module
int receiver = 9;
// The following lines initialise variables that are being used in the code
int switch_value = 0; // This variable is used to read the state of the micro-switch (i.e. whether it has been pressed or not)
int receiver_value = 0; // This varible is used to read the state of the receiver
// The following variables are used to study the encoder
int encoder_one = 11;
int encoder_two = 12;
// The following variables are used to check the previous values of the encoder
boolean encoder_one_previous = LOW;
boolean encoder_two_previous = LOW;
// The following variable is used to store the decimal value of the previous state of the encoder
int encoder_decimal_previous = 0;
// The follwing variables are used to check the current values of the encoder
boolean encoder_one_current = LOW;
boolean encoder_two_current = LOW;
// The following variable is used to check the current value of the encoder
int encoder_decimal_current = 0;
// The following is the lookup table that we will use for the encoder
int encoder_lookup_table[4][3] = {
{0, 2, -1},
{1, 0, -1},
{2, 3, -1},
{3, 1, -1},
};
// The following variable keep a count of the number of rotations of the motor
int count = 0;
int i = 0;
unsigned long the_time = 0;
int new_position = 0;
int old_position = 0;
int new_time = 0;
int old_time = 0;
float velocity = 0;
void setup()
{
// The following lines set all the pins that we initialised above as output pins for the motor
pinMode(pwm_a, OUTPUT);
pinMode(ain_two, OUTPUT);
pinMode(ain_one, OUTPUT);
pinMode(stand_by, OUTPUT);
//The following line sets digital pin 8 of Arduino Nano as Input using the internal pull-up resistor
pinMode(micro_switch, INPUT_PULLUP);
//The following line sets digital pin 9 of Arduino Nano as Input
pinMode(receiver, INPUT);
// The following lines set digital pin 11 and 12 of Arduino Nano as input0
pinMode(encoder_one, INPUT);
pinMode(encoder_two, INPUT);
Serial.begin(9600);
}
void loop()
{
// put your main code here, to run repeatedly:
receiver_value = digitalRead(receiver); // We need to keep a track of the receiver value to know when the door should be opened
if (receiver_value == HIGH)
{
//Serial.print("Switch: ");
//Serial.println(switch_value);
// When the door receives a HIGH signal from the receiver
/*Serial.print("Time: ");
time = millis();
Serial.println(the_time);*/
Serial.print("New Position: ");
Serial.println(new_position);
Serial.print("Old Position: ");
Serial.println(old_position);
Serial.print("New Time: ");
Serial.println(new_time);
Serial.print("Old Time: ");
Serial.println(old_time);
turn_motor(100, 1); // If the switch is not pressed, the motor will turn clockwise and open the door
delay(2000);
switch_value = digitalRead(micro_switch); // We need to keep a track of the state of the switch to know when the door should be opened
if (switch_value == LOW)
{
//Serial.print("Switch2: ");
//Serial.println(switch_value);
// When the switch is pressed the motor will stop working
stop_motor();
delay(3000);
turn_motor(100, -1); // The motor turns anti-clockwise and closes the door
//delay(2000);
encoder();
delay(3000);
}
}
}
void turn_motor(int motor_speed, int motor_direction)
{
// The standy pin of TB6612FNG must be high for the circuit to work
digitalWrite(stand_by, HIGH);
/*digitalWrite(test, HIGH);
delay(100);
digitalWrite(test, LOW);
delay(100);*/
if (motor_direction == 1)
{
// Turning the motor clockwise
digitalWrite(ain_one, HIGH);
digitalWrite(ain_two, LOW);
analogWrite(pwm_a, motor_speed);
}
else if (motor_direction == -1)
{
// Turning the motor anti-clockwise
digitalWrite(ain_one, LOW);
digitalWrite(ain_two, HIGH);
analogWrite(pwm_a, motor_speed);
//encoder();
}
}
void stop_motor()
{
digitalWrite(stand_by, LOW);
}
void encoder()
{
count = 0;
while (1)
{
if (count == 500)
{
Serial.println(count);
stop_motor();
break;
}
new_position = count;
new_time = (0.001*millis());
velocity = (new_position - old_position)/(new_time - old_time);
Serial.print("New Position: ");
Serial.println(new_position);
Serial.print("Old Position: ");
Serial.println(old_position);
Serial.print("New Time: ");
Serial.println(new_time);
Serial.print("Old Time: ");
Serial.println(old_time);
Serial.print("Velocity: ");
Serial.println(velocity);
if ((encoder_one_previous == 0) && (encoder_two_previous == 0))
{
encoder_decimal_previous = 0; // (00)2 = (0)10
}
else if ((encoder_one_previous == 0) && (encoder_two_previous == 1))
{
encoder_decimal_previous = 1; // (01)2 = (1)10
}
else if ((encoder_one_previous == 1) && (encoder_two_previous == 0))
{
encoder_decimal_previous = 2; // (10)2 = (2)10
}
else if ((encoder_one_previous == 1) && (encoder_two_previous == 1))
{
encoder_decimal_previous = 3; // (11)2 = (3)10
}
// Taking inputs from the encoder pins and saving them in the corresponding variables
encoder_one_current = digitalRead(encoder_one);
encoder_two_current = digitalRead(encoder_two);
if ((encoder_one_current == 0) && (encoder_two_current == 0))
{
encoder_decimal_current = 0; // (00)2 = (0)1
}
else if ((encoder_one_current == 0) && (encoder_two_current == 1))
{
encoder_decimal_current = 1; // (01)2 = (1)10
}
else if ((encoder_one_current == 1) && (encoder_two_current == 0))
{
encoder_decimal_current = 2; // (10)2 = (2)10
}
else if ((encoder_one_current == 1) && (encoder_two_current == 1))
{
encoder_decimal_current = 3; // (11)2 = (3)10
}
for (i = 0; i < 4; i++)
{
if (encoder_lookup_table[i][1] == encoder_decimal_current)
{
count += -encoder_lookup_table[i][2];
Serial.println(count);
}
}
encoder_one_previous = encoder_one_current;
encoder_two_previous = encoder_two_current;
old_position = new_position;
old_time = new_time;
}
}
Moderator edit: </mark> <mark>[code]</mark> <mark>
The following is the code. While moving anti-clockwise the motor needs to move an object through 200mm. How can I calculate that distance using the encoder and how can I calculate the speed. My code shows speed = -1.00.
// The following lines initiliase some of the pins of the Arduino Nano which are connected to the TB6612FNG motor driver IC
int pwm_a = 3; // Required for PWM
int ain_two = 4; // Input of the motor
int ain_one = 5; // Input of the motor
int stand_by = 6; // Standy pin which is required to turn the motor driver IC ON or OFF
// The following line initialises the digital pin 8 of the Arduino Nano connected to the micro-switch
int micro_switch = 8; // Used to control the motor
// The following line initialises the digital pin 9 of the Arduino Nano that will be connected to the receiver module
int receiver = 9;
// The following lines initialise variables that are being used in the code
int switch_value = 0; // This variable is used to read the state of the micro-switch (i.e. whether it has been pressed or not)
int receiver_value = 0; // This varible is used to read the state of the receiver
// The following variables are used to study the encoder
int encoder_one = 11;
int encoder_two = 12;
// The following variables are used to check the previous values of the encoder
boolean encoder_one_previous = LOW;
boolean encoder_two_previous = LOW;
// The following variable is used to store the decimal value of the previous state of the encoder
int encoder_decimal_previous = 0;
// The follwing variables are used to check the current values of the encoder
boolean encoder_one_current = LOW;
boolean encoder_two_current = LOW;
// The following variable is used to check the current value of the encoder
int encoder_decimal_current = 0;
// The following is the lookup table that we will use for the encoder
int encoder_lookup_table[4][3] = {
{0, 2, -1},
{1, 0, -1},
{2, 3, -1},
{3, 1, -1},
};
// The following variable keep a count of the number of rotations of the motor
int count = 0;
int i = 0;
unsigned long the_time = 0;
int new_position = 0;
int old_position = 0;
int new_time = 0;
int old_time = 0;
float velocity = 0;
void setup()
{
// The following lines set all the pins that we initialised above as output pins for the motor
pinMode(pwm_a, OUTPUT);
pinMode(ain_two, OUTPUT);
pinMode(ain_one, OUTPUT);
pinMode(stand_by, OUTPUT);
//The following line sets digital pin 8 of Arduino Nano as Input using the internal pull-up resistor
pinMode(micro_switch, INPUT_PULLUP);
//The following line sets digital pin 9 of Arduino Nano as Input
pinMode(receiver, INPUT);
// The following lines set digital pin 11 and 12 of Arduino Nano as input0
pinMode(encoder_one, INPUT);
pinMode(encoder_two, INPUT);
Serial.begin(9600);
}
void loop()
{
// put your main code here, to run repeatedly:
receiver_value = digitalRead(receiver); // We need to keep a track of the receiver value to know when the door should be opened
if (receiver_value == HIGH)
{
//Serial.print("Switch: ");
//Serial.println(switch_value);
// When the door receives a HIGH signal from the receiver
/*Serial.print("Time: ");
time = millis();
Serial.println(the_time);*/
Serial.print("New Position: ");
Serial.println(new_position);
Serial.print("Old Position: ");
Serial.println(old_position);
Serial.print("New Time: ");
Serial.println(new_time);
Serial.print("Old Time: ");
Serial.println(old_time);
turn_motor(100, 1); // If the switch is not pressed, the motor will turn clockwise and open the door
delay(2000);
switch_value = digitalRead(micro_switch); // We need to keep a track of the state of the switch to know when the door should be opened
if (switch_value == LOW)
{
//Serial.print("Switch2: ");
//Serial.println(switch_value);
// When the switch is pressed the motor will stop working
stop_motor();
delay(3000);
turn_motor(100, -1); // The motor turns anti-clockwise and closes the door
//delay(2000);
encoder();
delay(3000);
}
}
}
void turn_motor(int motor_speed, int motor_direction)
{
// The standy pin of TB6612FNG must be high for the circuit to work
digitalWrite(stand_by, HIGH);
/*digitalWrite(test, HIGH);
delay(100);
digitalWrite(test, LOW);
delay(100);*/
if (motor_direction == 1)
{
// Turning the motor clockwise
digitalWrite(ain_one, HIGH);
digitalWrite(ain_two, LOW);
analogWrite(pwm_a, motor_speed);
}
else if (motor_direction == -1)
{
// Turning the motor anti-clockwise
digitalWrite(ain_one, LOW);
digitalWrite(ain_two, HIGH);
analogWrite(pwm_a, motor_speed);
//encoder();
}
}
void stop_motor()
{
digitalWrite(stand_by, LOW);
}
void encoder()
{
count = 0;
while (1)
{
if (count == 500)
{
Serial.println(count);
stop_motor();
break;
}
new_position = count;
new_time = (0.001*millis());
velocity = (new_position - old_position)/(new_time - old_time);
Serial.print("New Position: ");
Serial.println(new_position);
Serial.print("Old Position: ");
Serial.println(old_position);
Serial.print("New Time: ");
Serial.println(new_time);
Serial.print("Old Time: ");
Serial.println(old_time);
Serial.print("Velocity: ");
Serial.println(velocity);
if ((encoder_one_previous == 0) && (encoder_two_previous == 0))
{
encoder_decimal_previous = 0; // (00)2 = (0)10
}
else if ((encoder_one_previous == 0) && (encoder_two_previous == 1))
{
encoder_decimal_previous = 1; // (01)2 = (1)10
}
else if ((encoder_one_previous == 1) && (encoder_two_previous == 0))
{
encoder_decimal_previous = 2; // (10)2 = (2)10
}
else if ((encoder_one_previous == 1) && (encoder_two_previous == 1))
{
encoder_decimal_previous = 3; // (11)2 = (3)10
}
// Taking inputs from the encoder pins and saving them in the corresponding variables
encoder_one_current = digitalRead(encoder_one);
encoder_two_current = digitalRead(encoder_two);
if ((encoder_one_current == 0) && (encoder_two_current == 0))
{
encoder_decimal_current = 0; // (00)2 = (0)1
}
else if ((encoder_one_current == 0) && (encoder_two_current == 1))
{
encoder_decimal_current = 1; // (01)2 = (1)10
}
else if ((encoder_one_current == 1) && (encoder_two_current == 0))
{
encoder_decimal_current = 2; // (10)2 = (2)10
}
else if ((encoder_one_current == 1) && (encoder_two_current == 1))
{
encoder_decimal_current = 3; // (11)2 = (3)10
}
for (i = 0; i < 4; i++)
{
if (encoder_lookup_table[1] == encoder_decimal_current)
{
count += -encoder_lookup_table[2];
Serial.println(count);
}
}
encoder_one_previous = encoder_one_current;
encoder_two_previous = encoder_two_current;
old_position = new_position;
old_time = new_time;
}
}
The following is the code. While moving anti-clockwise the motor needs to move an object through 200mm. How can I calculate that distance using the encoder and how can I calculate the speed. My code shows speed = -1.00.
// The following lines initiliase some of the pins of the Arduino Nano which are connected to the TB6612FNG motor driver IC
int pwm_a = 3; // Required for PWM
int ain_two = 4; // Input of the motor
int ain_one = 5; // Input of the motor
int stand_by = 6; // Standy pin which is required to turn the motor driver IC ON or OFF
// The following line initialises the digital pin 8 of the Arduino Nano connected to the micro-switch
int micro_switch = 8; // Used to control the motor
// The following line initialises the digital pin 9 of the Arduino Nano that will be connected to the receiver module
int receiver = 9;
// The following lines initialise variables that are being used in the code
int switch_value = 0; // This variable is used to read the state of the micro-switch (i.e. whether it has been pressed or not)
int receiver_value = 0; // This varible is used to read the state of the receiver
// The following variables are used to study the encoder
int encoder_one = 11;
int encoder_two = 12;
// The following variables are used to check the previous values of the encoder
boolean encoder_one_previous = LOW;
boolean encoder_two_previous = LOW;
// The following variable is used to store the decimal value of the previous state of the encoder
int encoder_decimal_previous = 0;
// The follwing variables are used to check the current values of the encoder
boolean encoder_one_current = LOW;
boolean encoder_two_current = LOW;
// The following variable is used to check the current value of the encoder
int encoder_decimal_current = 0;
// The following is the lookup table that we will use for the encoder
int encoder_lookup_table[4][3] = {
{0, 2, -1},
{1, 0, -1},
{2, 3, -1},
{3, 1, -1},
};
// The following variable keep a count of the number of rotations of the motor
int count = 0;
int i = 0;
unsigned long the_time = 0;
int new_position = 0;
int old_position = 0;
int new_time = 0;
int old_time = 0;
float velocity = 0;
void setup()
{
// The following lines set all the pins that we initialised above as output pins for the motor
pinMode(pwm_a, OUTPUT);
pinMode(ain_two, OUTPUT);
pinMode(ain_one, OUTPUT);
pinMode(stand_by, OUTPUT);
//The following line sets digital pin 8 of Arduino Nano as Input using the internal pull-up resistor
pinMode(micro_switch, INPUT_PULLUP);
//The following line sets digital pin 9 of Arduino Nano as Input
pinMode(receiver, INPUT);
// The following lines set digital pin 11 and 12 of Arduino Nano as input0
pinMode(encoder_one, INPUT);
pinMode(encoder_two, INPUT);
Serial.begin(9600);
}
void loop()
{
// put your main code here, to run repeatedly:
receiver_value = digitalRead(receiver); // We need to keep a track of the receiver value to know when the door should be opened
if (receiver_value == HIGH)
{
//Serial.print("Switch: ");
//Serial.println(switch_value);
// When the door receives a HIGH signal from the receiver
/*Serial.print("Time: ");
time = millis();
Serial.println(the_time);*/
Serial.print("New Position: ");
Serial.println(new_position);
Serial.print("Old Position: ");
Serial.println(old_position);
Serial.print("New Time: ");
Serial.println(new_time);
Serial.print("Old Time: ");
Serial.println(old_time);
turn_motor(100, 1); // If the switch is not pressed, the motor will turn clockwise and open the door
delay(2000);
switch_value = digitalRead(micro_switch); // We need to keep a track of the state of the switch to know when the door should be opened
if (switch_value == LOW)
{
//Serial.print("Switch2: ");
//Serial.println(switch_value);
// When the switch is pressed the motor will stop working
stop_motor();
delay(3000);
turn_motor(100, -1); // The motor turns anti-clockwise and closes the door
//delay(2000);
encoder();
delay(3000);
}
}
}
void turn_motor(int motor_speed, int motor_direction)
{
// The standy pin of TB6612FNG must be high for the circuit to work
digitalWrite(stand_by, HIGH);
/*digitalWrite(test, HIGH);
delay(100);
digitalWrite(test, LOW);
delay(100);*/
if (motor_direction == 1)
{
// Turning the motor clockwise
digitalWrite(ain_one, HIGH);
digitalWrite(ain_two, LOW);
analogWrite(pwm_a, motor_speed);
}
else if (motor_direction == -1)
{
// Turning the motor anti-clockwise
digitalWrite(ain_one, LOW);
digitalWrite(ain_two, HIGH);
analogWrite(pwm_a, motor_speed);
//encoder();
}
}
void stop_motor()
{
digitalWrite(stand_by, LOW);
}
void encoder()
{
count = 0;
while (1)
{
if (count == 500)
{
Serial.println(count);
stop_motor();
break;
}
new_position = count;
new_time = (0.001*millis());
velocity = (new_position - old_position)/(new_time - old_time);
Serial.print("New Position: ");
Serial.println(new_position);
Serial.print("Old Position: ");
Serial.println(old_position);
Serial.print("New Time: ");
Serial.println(new_time);
Serial.print("Old Time: ");
Serial.println(old_time);
Serial.print("Velocity: ");
Serial.println(velocity);
if ((encoder_one_previous == 0) && (encoder_two_previous == 0))
{
encoder_decimal_previous = 0; // (00)2 = (0)10
}
else if ((encoder_one_previous == 0) && (encoder_two_previous == 1))
{
encoder_decimal_previous = 1; // (01)2 = (1)10
}
else if ((encoder_one_previous == 1) && (encoder_two_previous == 0))
{
encoder_decimal_previous = 2; // (10)2 = (2)10
}
else if ((encoder_one_previous == 1) && (encoder_two_previous == 1))
{
encoder_decimal_previous = 3; // (11)2 = (3)10
}
// Taking inputs from the encoder pins and saving them in the corresponding variables
encoder_one_current = digitalRead(encoder_one);
encoder_two_current = digitalRead(encoder_two);
if ((encoder_one_current == 0) && (encoder_two_current == 0))
{
encoder_decimal_current = 0; // (00)2 = (0)1
}
else if ((encoder_one_current == 0) && (encoder_two_current == 1))
{
encoder_decimal_current = 1; // (01)2 = (1)10
}
else if ((encoder_one_current == 1) && (encoder_two_current == 0))
{
encoder_decimal_current = 2; // (10)2 = (2)10
}
else if ((encoder_one_current == 1) && (encoder_two_current == 1))
{
encoder_decimal_current = 3; // (11)2 = (3)10
}
for (i = 0; i < 4; i++)
{
if (encoder_lookup_table[1] == encoder_decimal_current)
{
count += -encoder_lookup_table[2];
Serial.println(count);
}
}
encoder_one_previous = encoder_one_current;
encoder_two_previous = encoder_two_current;
old_position = new_position;
old_time = new_time;
}
}
I have only looked at the code in Reply #8 - I assume it is the latest version.
If you want to get positions from an encoder without losing count then there is no place in your program for delay()
And don't use while(1) anywhere. Allow loop() to do the repetition - that is what it is for.
How many pulses per second will the encoder be producing? If the pulses occur frequently you may need to use an interrupt to capture them.
How many pulses per revolution does the encoder produce?
To calculate speed you need to record the value of micros() from two successive pulses and use the time interval to determine the speed.
To calculate distance you need to count the pulses and relate the total to the number of pulses per revolution and the distance travelled per revolution.