Hi guys
Im trying hard to make my first robot. It should be wireless remote controlled using two Arduinos, some switches and RF links. At this point the communication and everything is fine. But as I set the motor to go forward with the same speed, one motor spins faster than the other one? I am using Arduino UNO with the official motor shield R3. The Arduino is powered with a 9v battery. The shield is powered by its own 9v battery. Im using two standard DC motors which are attached to the chassis using Tamiya Twin Gear motor box.
Can anyone tell me why one motor spins faster than the other? And maybe how to solve it?
Here is the code:
Transmitter:
#include <VirtualWire.h>
const char *forward = "f";
const char *backward = "b";
const char *right = "r";
const char *left = "l";
const char *zero = "0";
const int rightButton = 2;
const int forwardButton = 3;
const int backwardButton = 4;
const int leftButton = 5;
void setup()
{
Serial.begin(9600);
Serial.println("setup");
//Button pin setup
pinMode(rightButton, INPUT);
pinMode(forwardButton, INPUT);
pinMode(backwardButton, INPUT);
pinMode(leftButton, INPUT);
// Tranmitter setup
vw_setup(4000);
vw_set_tx_pin(7);
}
void loop()
{
if (digitalRead(leftButton) == HIGH){
digitalWrite(13, HIGH);
vw_send((byte *)left, strlen(left));
vw_wait_tx();
delay(200);
}
else if (digitalRead(backwardButton) == HIGH) {
digitalWrite(13, HIGH);
vw_send((byte *)backward, strlen(backward));
vw_wait_tx();
delay(200);
}
else if (digitalRead(forwardButton) == HIGH){
digitalWrite(13, HIGH);
vw_send((byte *)forward, strlen(forward));
vw_wait_tx();
delay(200);
}
else if (digitalRead(rightButton) == HIGH){
digitalWrite(13, HIGH);
vw_send((byte *)right, strlen(right));
vw_wait_tx();
delay(200);
}
else{
digitalWrite(13, LOW);
vw_send((byte *)zero, strlen(zero));
vw_wait_tx();
}
}
Receiver: (The code that controls the motors)
#include <VirtualWire.h>
// Højre motor
int hRetning = 13;
int hHastighed = 11;
int hBremse = 8;
//Venstre motor
int vRetning = 12
int vHastighed = 3;
int vBremse = 9;
void setup()
{
Serial.begin(9600);
Serial.println("Setup");
//Højre motor
pinMode(hRetning, OUTPUT);
pinMode(hBremse, OUTPUT);
//Venstre motor
pinMode(vRetning, OUTPUT);
pinMode(vBremse, OUTPUT);
//Modtager modulet
vw_set_rx_pin(7);
vw_setup(4000);
vw_rx_start();
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
int i;
char c = (buf[i]);
if (vw_get_message(buf, &buflen))
{
for (i = 0; i < buflen; i++)
{
Serial.print(c);
Serial.print("");
}
Serial.println("");
}
if (c == 'f'){
//Højre motor forward
digitalWrite(hRetning, HIGH);
digitalWrite(hBremse, LOW);
analogWrite(hHastighed, 100);
//Venstre motor forward
digitalWrite(vRetning, HIGH);
digitalWrite(vBremse, LOW);
analogWrite(vHastighed, 100);
}
else {
STOP ();
}
if (c == 'b'){
//Højre motor forward
digitalWrite(hRetning, LOW);
digitalWrite(hBremse, LOW);
analogWrite(hHastighed, 100);
//Venstre motor forward
digitalWrite(vRetning, LOW);
digitalWrite(vBremse, LOW);
analogWrite(vHastighed, 100);
}
else {
STOP ();
}
if (c == 'r'){
//Højre motor forward
digitalWrite(hRetning, HIGH);
digitalWrite(hBremse, LOW);
analogWrite(hHastighed, 20);
//Venstre motor forward
digitalWrite(vRetning, HIGH);
digitalWrite(vBremse, LOW);
analogWrite(vHastighed, 120);
}
else {
STOP ();
}
if (c == 'l'){
//Højre motor forward
digitalWrite(hRetning, HIGH);
digitalWrite(hBremse, LOW);
analogWrite(hHastighed, 120);
//Venstre motor forward
digitalWrite(vRetning, HIGH);
digitalWrite(vBremse, LOW);
analogWrite(vHastighed, 20);
}
else {
STOP ();
}
if (c == '0'){
STOP ();
}
}
int STOP (){
digitalWrite(hBremse, HIGH);
digitalWrite(vBremse, HIGH);
}