Using Arduino UNO, generic ardumoto shield, and two momentary tactile buttons.
I want the motor to go forward only while btnA is pressed then stop when released, and the same for btnB except in reverse.
btnA does nothing btnB works as it should, I swapped pin numbers between them and btnA worked while btnB failed.
Then I swapped both pin num's to new one's, and got the same failure. So I concluded it's most likely in the code.
Here is my code, it is a modified ardumoto example I found with google.
/* Ardumoto Example Sketch
by: Jim Lindblom
date: November 8, 2013
license: Public domain. Please use, reuse, and modify this
sketch!
This sketch has been modified to use buttons instead of predefined actions
Also not using motor B
Three useful functions are defined:
setupArdumoto() -- Setup the Ardumoto Shield pins
driveArdumoto([motor], [direction], [speed]) -- Drive [motor]
(0 for A, 1 for B) in [direction] (0 or 1) at a [speed]
between 0 and 255. It will spin until told to stop.
stopArdumoto([motor]) -- Stop driving [motor] (0 or 1).
setupArdumoto() is called in the setup().
The loop() demonstrates use of the motor driving functions.
*/
// Clockwise and counter-clockwise definitions.
// Depending on how you wired your motors, you may need to swap.
#define CW 0
#define CCW 1
// Motor definitions to make life easier:
#define MOTOR_A 0
#define MOTOR_B 1
// Pin Assignments //
// Don't change these! These pins are statically defined by shield layout
const byte PWMA = 3; // PWM control (speed) for motor A
const byte PWMB = 11; // PWM control (speed) for motor B
const byte DIRA = 12; // Direction control for motor A
const byte DIRB = 13; // Direction control for motor B
const byte btnA = 8; // FWD button
const byte btnB = 9; // REV button
int FWD = 0; // Variable to store btnA value
int REV = 0; // Variable to store btnB value
void setup()
{
pinMode(btnA, INPUT_PULLUP);
pinMode(btnB, INPUT_PULLUP);
setupArdumoto(); // Set all pins as outputs
}
void loop()
{
FWD = digitalRead(btnA); // Read button A's value
REV = digitalRead(btnB); // Read button B's value
if (FWD == LOW)
{
driveArdumoto(MOTOR_A, CW, 255); // Set motor A to CCW at max
}
else if (REV == LOW)
{
driveArdumoto(MOTOR_A, CCW, 255); // Set motor A to CCW at max
}
else
{
stopArdumoto(MOTOR_A); // STOP motor A
}
}
// driveArdumoto drives 'motor' in 'dir' direction at 'spd' speed
void driveArdumoto(byte motor, byte dir, byte spd)
{
if (motor == MOTOR_A)
{
digitalWrite(DIRA, dir);
analogWrite(PWMA, spd);
}
else if (motor == MOTOR_B)
{
digitalWrite(DIRB, dir);
analogWrite(PWMB, spd);
}
}
// stopArdumoto makes a motor stop
void stopArdumoto(byte motor)
{
driveArdumoto(motor, 0, 0);
}
// setupArdumoto initialize all pins
void setupArdumoto()
{
// All pins should be setup as outputs:
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(DIRA, OUTPUT);
pinMode(DIRB, OUTPUT);
// Initialize all pins as low:
digitalWrite(PWMA, LOW);
digitalWrite(PWMB, LOW);
digitalWrite(DIRA, LOW);
digitalWrite(DIRB, LOW);
}
any help is appreciated .
Submicro