I have reduced my UNO program to a minimum to find and demonstrate my problem:
If I un-comment this line then A motor will not run://servoTurret.attach(6); // Turret servo pin 6
If I un-comment this line then B motor will not run://irrecv.enableIRIn(); // Start the IR receiver
If I remove the above lines then both motors run correctly.
All libraries are as downloaded from arduino.cc. The drive is: http://www.elechouse.com/elechouse/index.php?main_page=product_info&cPath=100_146&products_id=2179 I tried their library for the drive but that was also buggy (did not allow A forward if B is reverse, etc.).
Interrupt timer conflict in the libraries?
Do I have to put the motor drive software in a separate UNO like the arduino robot?
Here is my code:
#include <Servo.h>
#include <IRremote.h> // IR receiver library
Servo servoTurret; // Turret servo object
IRrecv irrecv(12);
int IN_A1 = 3; // input RPWM (forward A motor)
int IN_A2 = 10; // input LPWM (reverse A motor)
int IN_B1 = 9; // input RPWM (forward B motor)
int IN_B2 = 11; // input LPWM (reverse B motor)
int motorAdjustment = 0;
void setup(void)
{
//servoTurret.attach(6); // Turret servo pin 6
//irrecv.enableIRIn(); // Start the IR receiver
/** PWM pin configurate */
pinMode(IN_A1, OUTPUT); //(PWM forward A motor)
pinMode(IN_A2, OUTPUT); //(PWM reverse A motor)
pinMode(IN_B1, OUTPUT); //(PWM forward B motor)
pinMode(IN_B2, OUTPUT); //(PWM reverse B motor)
//motors stop
digitalWrite(IN_A1, HIGH);
digitalWrite(IN_A2, HIGH);
digitalWrite(IN_B1, HIGH);
digitalWrite(IN_B2, HIGH);
// increase frequency of PWM on pins 9 & 10 (TCCR1B) and pins 3 & 11 (TCCR2B)
int prescalerVal = 0x07; // create a variable called prescalerVal and set it equal to the binary number "00000111"
TCCR1B &= ~prescalerVal; //AND the value in TCCR0B with binary number "11111000"
TCCR2B &= ~prescalerVal; //AND the value in TCCR0B with binary number "11111000"
prescalerVal = 0x02; //set prescalerVal equal to binary number "00000010"
TCCR1B |= prescalerVal; //OR the value in TCCR0B with binary number "00000010"
TCCR2B |= prescalerVal; //OR the value in TCCR0B with binary number "00000010"
}
void loop(void)
{
motorsWrite(20, 20);
delay(3000); // wait for 3 seconds
motorsStop();
delay(3000);
}
void motorsStop() { //Robot.motorsStop(); // fast stop
analogWrite(IN_A1,255);
analogWrite(IN_A2,255);
analogWrite(IN_B1,255);
analogWrite(IN_B2,255);
}
void motorsWrite(int speedL, int speedR){
speedL = ~speedL; // invert speed because
speedR = ~speedR; // lower PWM = higher speed.
//motor adjustment to run straight using percentage
if(motorAdjustment<0){
speedR*=(1+motorAdjustment);
}else{
speedL*=(1-motorAdjustment);
}
if(speedL>0){ // positive = forward
analogWrite(IN_A1,speedL);
analogWrite(IN_A2,0);
}else{ // negative = reverse
analogWrite(IN_A1,0);
analogWrite(IN_A2,-speedL);
}
if(speedR>0){
analogWrite(IN_B1,speedR);
analogWrite(IN_B2,0);
}else{
analogWrite(IN_B1,0);
analogWrite(IN_B2,-speedR);
}
}