Hi I'm new to Arduino motors and currently can't get my motors to work when when my Mega is not plugged into the compter. It works fine when plugged in though. My set up looks roughly like this.
Thought the motor shield is actually a Velleman and my gear motors are these.
I've also tried using a 9V to power the motor shield.
Am I just not using enough power?
EDIT: There are AA batteries NOT AAA.
#define FORWARD 0
#define LEFT 1
#define RIGHT 2
#define BACKWARD 3
#define STOP 4
#define STOP_SPD 0
#define TOP_SPD 255
int pwn_left = 3; //PWM control for motor outputs 1 and 2
int pwn_right = 9; //PWM control for motor outputs 3 and 4
int dir_left = 2; //direction control for motor outputs 1 and 2
int dir_right = 8; //direction control for motor outputs 3 and 4
int cur_cmd, last_cmd;
void setup()
{
Serial.begin(9600);
pinMode(pwn_left, OUTPUT); //Set control pins to be outputs
pinMode(pwn_right, OUTPUT);
pinMode(dir_left, OUTPUT);
pinMode(dir_right, OUTPUT);
analogWrite(pwn_left, 100); //set both motors to run at (100/255 = 39)% duty cycle (slow)
analogWrite(pwn_right, 100);
last_cmd = 0;
cur_cmd = 0;
}
void loop()
{
motorHub(FORWARD);
delay(5000);
motorHub(LEFT);
delay(5000);
motorHub(RIGHT);
delay(5000);
motorHub(BACKWARD);
delay(5000);
}
void motorHub(int command){
switch (command){
case FORWARD:
Serial.println("Forward");
digitalWrite(dir_left, HIGH);
digitalWrite(dir_right, HIGH);
analogWrite(pwn_left, TOP_SPD);
analogWrite(pwn_right, TOP_SPD);
break;
case LEFT:
Serial.println("Left");
digitalWrite(dir_right, HIGH);
analogWrite(pwn_left, STOP_SPD);
analogWrite(pwn_right, TOP_SPD);
break;
case RIGHT:
Serial.println("Right");
digitalWrite(dir_left, HIGH);
analogWrite(pwn_right, STOP_SPD);
analogWrite(pwn_left, TOP_SPD);
break;
case BACKWARD:
Serial.println("Backward");
analogWrite(pwn_right, STOP_SPD);
analogWrite(pwn_left, STOP_SPD);
delay(500);
digitalWrite(dir_left, HIGH);
digitalWrite(dir_right, HIGH);
Serial.println("Stop");
analogWrite(pwn_left, TOP_SPD);
analogWrite(pwn_right, TOP_SPD);
break;
default:
Serial.println("Stop");
analogWrite(pwn_right, STOP_SPD);
analogWrite(pwn_left, STOP_SPD);
delay(500);
break;
}
}