I have tested this code on my breadboard and subsequently on my PCB. It had worked.
The motor is supposed to turn only when the variable receiver_value goes HIGH.
But today, the motor was working without the receiver_value going HIGH.
The soldered wires that I had put on the encoder chip of my pololu motor were coming off. Could that be the problem. Please help. This is very urgent.
// 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
boolean switch_value = LOW; // This variable is used to read the state of the micro-switch (i.e. whether it has been pressed or not)
boolean receiver_value = LOW; // 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;
// The following variable is the loop variable
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);
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)
{
turn_motor(50, -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(50, 1); // The motor turns anti-clockwise and closes the door
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);
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 == 670)
{
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];
Serial.println(count);
break;
}
}
encoder_one_previous = encoder_one_current;
encoder_two_previous = encoder_two_current;
}
}