Before:
I modified the program by adding the 2 back motors. The new motors will move in sync with the motor in front of it.
// Rover 5 4WD Code
// Modified from 2WD Code
volatile int rotaryCount = 0;
#define PINA 8
#define PINB 9
#define INTERRUPT 0 // that is, pin 2
// CH 1
#define DIRECTIONA 4
#define MOTORA 5
// CH 2
#define DIRECTIONB 7
#define MOTORB 6
// CH 3
#define DIRECTIONC 9
#define MOTORC 8
// CH 4
#define DIRECTIOND 11
#define MOTORD 10
#define TIME_FORWARDS 10000
#define TIME_BACKWARDS 10000
#define TIME_TURN 1200
// Interrupt Service Routine for a change to encoder pin A
void isr ()
{
boolean up;
if (digitalRead (PINA))
up = digitalRead (PINB);
else
up = !digitalRead (PINB);
if (up)
rotaryCount++;
else
rotaryCount--;
} // end of isr
void setup ()
{
attachInterrupt (INTERRUPT, isr, CHANGE); // interrupt 0 is pin 2, interrupt 1 is pin 3
pinMode (MOTORA, OUTPUT);
pinMode (DIRECTIONA, OUTPUT);
pinMode (MOTORB, OUTPUT);
pinMode (DIRECTIONB, OUTPUT);
pinMode (MOTORC, OUTPUT);
pinMode (DIRECTIONC, OUTPUT);
pinMode (MOTORD, OUTPUT);
pinMode (DIRECTIOND, OUTPUT);
} // end of setup
byte phase;
unsigned long start;
int time_to_go;
void loop ()
{
analogWrite (MOTORA, 200);
analogWrite (MOTORB, 200);
analogWrite (MOTORC, 200);
analogWrite (MOTORD, 200);
start = millis ();
// check current drain
while (millis () - start < time_to_go)
{
if (analogRead (0) > 325) // > 1.46 amps
break;
}
switch (phase++ & 3)
{
case 0:
digitalWrite (DIRECTIONA, 1);
digitalWrite (DIRECTIONB, 1);
digitalWrite (DIRECTIONC, 1);
digitalWrite (DIRECTIOND, 1);
time_to_go = TIME_FORWARDS;
break;
case 1:
// turn
digitalWrite (DIRECTIONA, 1);
digitalWrite (DIRECTIONB, 0);
digitalWrite (DIRECTIONC, 1);
digitalWrite (DIRECTIOND, 0);
time_to_go = TIME_TURN;
break;
case 2:
digitalWrite (DIRECTIONA, 0);
digitalWrite (DIRECTIONB, 0);
digitalWrite (DIRECTIONC, 0);
digitalWrite (DIRECTIOND, 0);
time_to_go = TIME_BACKWARDS;
break;
case 3:
digitalWrite (DIRECTIONA, 0);
digitalWrite (DIRECTIONB, 1);
digitalWrite (DIRECTIONC, 0);
digitalWrite (DIRECTIOND, 1);
time_to_go = TIME_TURN;
break;
} // end of switch
analogWrite (MOTORA, 0);
analogWrite (MOTORB, 0);
analogWrite (MOTORC, 0);
analogWrite (MOTORD, 0);
delay (500);
} // end of loop
I also had to slightly change the wiring. After connecting the wires for CH1 and CH2 as instructed on the website, I had to connect CH3 & CH4.
CH 3
PWM > D8
DIR > D9
CH 4
PWM > D10
DIR > D11
No comments:
Post a Comment