Hello world,
I have two dc motors connected to a TB6612FNG motor driver with a separate 6V power supply. The driver is connected to a Arduino Nano.
I have been testing the setup with a POT, connected to the Nano, running the motors in one direction when the POT is turned CW, stopping the motors with the POT in the center position and running the motors in the other direction when the POT is turned CCW.
This is working as expected.
Next I am trying to replace the POT with the HX1838 IR remote and receiver, using three different buttons on the remote to control the motors at constant speed.
Running this code I am not able to get both motors running. The code from the remote is read, and the right function is called. but only Motor 2 is running.
Looking at the two codes I do not understand why this is not working. If someone can take a look at my codes and explain what I am doing wrong I would very much appreciate it.
Code with POT:
// Test of TB6612FNG with POT and two motors
// STBY for enabeling the H-bridge
const int STBY=12;
// Motor 1
const int AIN1=8;
const int AIN2=9;
const int PWMA=3;
// Motor 2
const int BIN1=10;
const int BIN2=11;
const int PWMB=5;
// POT on analog inn 0
const int POT=0;
int val = 0; // For storing the reading from POT
int velocity = 0; // For storing the velocity
void setup()
{
pinMode(STBY, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
brake(); // Start with motors stopped
}
void loop()
{
val = analogRead(POT);
// Forward
if (val > 562)
{
velocity = map(val, 563, 1023, 0, 255);
forward(velocity);
}
// Reverse
else if (val < 462)
{
velocity = map(val, 461, 0, 0, 255);
reverse(velocity);
}
// Brake
else
{
brake();
}
}
// Function Forward
void forward (int rate)
{
digitalWrite(STBY, LOW);
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, HIGH);
digitalWrite(STBY, HIGH);
analogWrite(PWMA, rate);
analogWrite(PWMB, rate);
}
// Function Reverse
void reverse (int rate)
{
digitalWrite(STBY, LOW);
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, HIGH);
digitalWrite(BIN1, HIGH);
digitalWrite(BIN2, LOW);
digitalWrite(STBY, HIGH);
analogWrite(PWMA, rate);
analogWrite(PWMB, rate);
}
// Function Brake
void brake ()
{
digitalWrite(STBY, LOW);
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, LOW);
digitalWrite(STBY, HIGH);
}
Code with IR-remote:
// Test of TB6612FNG with two motors and IR-remote
#include <IRremote.h>
// Constant for the speed
#define VELOCITY 200
// STBY for enabeling the H-bridge
const int STBY=12;
// Motor 1
const int AIN1=8;
const int AIN2=9;
const int PWMA=3;
// Motor 2
const int BIN1=10;
const int BIN2=11;
const int PWMB=5;
// IR-reciever
const int IR=2;
IRrecv irrecv(IR); // Create instance of 'irrecv'
decode_results results; // Create instance of 'decode_results'
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(STBY, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
brake(); // Start with motors stopped
}
void loop()
{
if (irrecv.decode(&results)) // Have we received an IR signal?
{
irrecv.resume(); // Receive the next value
Serial.println(results.value, HEX);
}
// Forward
if (results.value == 0xFF629D)
{
forward(VELOCITY);
}
// Reverse
if (results.value == 0xFFA857)
{
reverse(VELOCITY);
}
// Brake
if (results.value == 0xFF02FD)
{
brake();
}
}
// Function Forward
void forward (int rate)
{
Serial.println("-Forward-");
digitalWrite(STBY, LOW);
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, HIGH);
digitalWrite(STBY, HIGH);
analogWrite(PWMA, rate);
analogWrite(PWMB, rate);
}
// Function Reverse
void reverse (int rate)
{
Serial.println("-Reverse-");
digitalWrite(STBY, LOW);
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, HIGH);
digitalWrite(BIN1, HIGH);
digitalWrite(BIN2, LOW);
digitalWrite(STBY, HIGH);
analogWrite(PWMA, rate);
analogWrite(PWMB, rate);
}
// Function Brake
void brake ()
{
Serial.println("-Brake-");
digitalWrite(STBY, LOW);
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, LOW);
digitalWrite(STBY, HIGH);
}