Why is it that the following code stops the motor from working, specifically the first line (see full code further down) ...
if (!SD.begin()) {
Serial.println("initialisation failed!");
while (true); // Stay here.
}
... but I still get printouts of the following every second in the COM3 window?
12:40:16.931 -> Motor running forwards ...
12:40:17.955 -> Motor running backwards ...
12:40:18.982 -> Motor running forwards ...
12:40:19.970 -> Motor running backwards ...
I am using an Adafruit Motor Shield (HW-130), an Arduino Uno clone, and a Micro SD TF Card Memory Shield Module.
The SD card initialises fine, and "initialisation failed!" is never displayed. But this line of code just stops the motor from working at all. As soon as I remove it, the code works fine!
#include <SPI.h>
#include <SD.h>
#include <AFMotor.h>
AF_DCMotor motor(1, MOTOR12_64KHZ); // Define motor on channel 1 with 64KHz PWM.
void setup() {
motor.setSpeed(255);
Serial.begin(9600); // Open serial communications and wait for port to open.
while (!Serial) {
; // Wait for serial port to connect. Needed for native USB port only.
}
// Initialise SD card.
Serial.print("Initialising SD card... ");
if (!SD.begin()) {
Serial.println("initialisation failed!");
while (true); // Stay here.
}
Serial.println("initialisation succeeded!");
}
void loop() {
motor.run(FORWARD);
Serial.println("Motor running forwards ...");
delay(1000);
motor.run(BACKWARD);
Serial.println("Motor running backwards ...");
delay(1000);
}
Excellent, thank you, exactly the answer I was looking for I checked out the V2 motor sheild details, but it didn't mention which pins it uses, only "two data pins SDA and SCL". Which pins are those? And will it definitely solve this particular issue?
jimixter:
Excellent, thank you, exactly the answer I was looking for I checked out the V2 motor sheild details, but it didn't mention which pins it uses, only "two data pins SDA and SCL". Which pins are those? And will it definitely solve this particular issue?
On an Uno, SDA and SCL are the same as A4 and A5 (analog pins). The version 2 motor shield communicates with the board over the two wire interface (TWI) which is the wire library. It receives commands from your board and does all the PWM on the shield so you don't need to use any other pins or resources of your board. That is one of the main reasons they came out with V2.
Ok thank you. Could I please ask if it sounds like my V1 motor shield could be causing the problems in my other post below too? Sounds like I need to get a V2 board on order...