nothing happens when I connect the power supply (4.5 or 6.5 Volts) and motors (3V dc)
the program compiles and uploads successfully
it appears to run from the messages on the serial monitor from upload and each reset press:
starting loop
inside loop
all I am trying to do is simply rotate the motors 1 sec cw (forward), 1 sec ccw (reverse)
could this be the problem?
//Must set STBY pin to HIGH in order to move
Does the pin STBY is on (42) need to be set high by a s/w command?
doesn’t this code set STBY to high?
pinMode(STBY,OUTPUT);
motor_standby(false); //Must set STBY pin to HIGH in order to move
//Turns off the outputs of the Motor Driver when true
void motor_standby(char standby)
{
if (standby == true) digitalWrite(STBY,LOW);
else digitalWrite(STBY,HIGH);
}
I get an error msg from the arduino sketch when I click ‘copy for forum’
Binary sketch size: 4,886 bytes (of a 258,048 byte maximum)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 23
at processing.app.tools.DiscourseFormat.appendFormattedLine(DiscourseFormat.java:192)
at processing.app.tools.DiscourseFormat.show(DiscourseFormat.java:99)
at processing.app.Editor$34.actionPerformed(Editor.java:1156)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1225)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1266)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
#define PWMA 7
#define AIN1 38
#define AIN2 34
#define BIN1 49
#define BIN2 51
#define PWMB 4
#define STBY 42
#define motor_A 0
#define motor_B 1
#define FORWARD 1
#define REVERSE 0
#define RIGHT 1
#define LEFT 0
void setup()
{
pinMode(PWMA,OUTPUT);
pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(PWMB,OUTPUT);
pinMode(BIN1,OUTPUT);
pinMode(BIN2,OUTPUT);
pinMode(STBY,OUTPUT);
motor_standby(false); //Must set STBY pin to HIGH in order to move
// open the serial port at 9600 bps:
Serial.begin(9600);
Serial.println("starting loop");
}
void loop() {
Serial.println("inside loop");
motor_brake(); //STOP for 1s
delay(1000);
motor_drive(FORWARD, 150); //Move FORWARD at full speed for 1s
delay(1000);
motor_brake(); //STOP for 1s
delay(1000);
motor_drive(REVERSE, 150); //REVERSE at about half-speed for 1s
delay(1000);
motor_brake(); //STOP for 1s
delay(1000);
motor_turn(RIGHT, 150, 150); //Spin RIGHT for 0.5s
delay(1000);
motor_brake();
delay(100000000);
}
//Turns off the outputs of the Motor Driver when true
void motor_standby(char standby)
{
if (standby == true) digitalWrite(STBY,LOW);
else digitalWrite(STBY,HIGH);
}
//Stops the motors from spinning and locks the wheels
void motor_brake()
{
digitalWrite(AIN1,1);
digitalWrite(AIN2,1);
digitalWrite(PWMA,LOW);
digitalWrite(BIN1,1);
digitalWrite(BIN2,1);
digitalWrite(PWMB,LOW);
}
//Controls the direction the motors turn, speed from 0(off) to 255(full speed)
void motor_drive(char direction, unsigned char speed)
{
if (direction == FORWARD)
{
motor_control(motor_A, FORWARD, speed);
motor_control(motor_B, FORWARD, speed);
}
else
{
motor_control(motor_A, REVERSE, speed);
motor_control(motor_B, REVERSE, speed);
}
}
//You can control the turn radius by specifying the speed of each motor
//Set both to 255 for it to spin in place
void motor_turn(char direction, unsigned char speed_A, unsigned char speed_B )
{
if (direction == RIGHT)
{
motor_control(motor_A, REVERSE, speed_A);
motor_control(motor_B, FORWARD, speed_B);
}
else
{
motor_control(motor_A, FORWARD, speed_A);
motor_control(motor_B, REVERSE, speed_B);
}
}
void motor_control(char motor, char direction, unsigned char speed)
{
if (motor == motor_A)
{
if (direction == FORWARD)
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
}
else
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH);
}
analogWrite(PWMA,speed);
}
{
if (direction == FORWARD) //Notice how the direction is reversed for motor_B
{ //This is because they are placed on opposite sides so
digitalWrite(BIN1,LOW); //to go FORWARD, motor_A spins CW and motor_B spins CCW
digitalWrite(BIN2,HIGH);
}
else
{
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
}
analogWrite(PWMB,speed);
}
}
output (after each press of the reset button):
starting loop
inside loop
starting loop
inside loop
starting loop
inside loop
starting loop
inside loop