HI,
I'm trying to use an ardumoto shield, an arduino and another (3v) microcontroller device to control both the arduino/ardumoto
I have a power supply for my arduino/ardumoto and a separate power supply for my other device.
I have connected ground from my other device to GND (above pin13) on the arduino
I have three outputs from my device connected to inputs 5, 6, 7 on the arduino.
I am expecting something to happen when I turn one or more of these outputs high, and that correspondingly my arduino should pick this up with digitalread in an if statement but nothing happens
(All I see is stop stop stop ... on my serial monitor.)
My sketch is here ...
// 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
int pin0 = 5; //for reading the outputs from the other device
int pin1 = 6;
int pin2 = 7;
void setup()
{
Serial.begin(9600);
setupArdumoto(); // Set all pins as outputs
pinMode(pin0, INPUT);
pinMode(pin1, INPUT);
pinMode(pin2, INPUT);
}
void loop()
{
Serial.println("main loop");
// 000 all stop
if ((digitalRead(pin0 == LOW)) && (digitalRead(pin0 == LOW)) && (digitalRead(pin0 == LOW)))
{
Serial.println("stop");
stopArdumoto(MOTOR_A);
stopArdumoto(MOTOR_B);
}
//001 left
else if ((digitalRead(pin0 == LOW)) && (digitalRead(pin0 == LOW)) && (digitalRead(pin0 == HIGH)))
{
Serial.println("left");
driveArdumoto(MOTOR_A, CCW, 255);
driveArdumoto(MOTOR_B, CW, 255);
}
//010 right
else if ((digitalRead(pin0 == LOW)) && (digitalRead(pin0 == HIGH)) && (digitalRead(pin0 == LOW)))
{
Serial.println("right");
driveArdumoto(MOTOR_A, CW, 255);
driveArdumoto(MOTOR_B, CCW, 255);
}
//100 forward
else if ((digitalRead(pin0 == HIGH)) && (digitalRead(pin0 == LOW)) && (digitalRead(pin0 == LOW)))
{
Serial.println("forward");
driveArdumoto(MOTOR_A, CCW, 255);
driveArdumoto(MOTOR_B, CCW, 255);
}
//101 reverse
else if ((digitalRead(pin0 == HIGH)) && (digitalRead(pin0 == LOW)) && (digitalRead(pin0 == HIGH)))
{
Serial.println("reverse");
driveArdumoto(MOTOR_A, CW, 255);
driveArdumoto(MOTOR_B, CW, 255);
}
}
// 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);
}