enter image description here enter image description here enter image description here enter image description here enter image description hereI verified my code and uploaded it to the board and that was successful.
Info: - Using Mega 2560 Arduino Board with Adafruit V1 - 3 DC 12 volt motors - on HP computer
Here is my code:
#include <AFMotor.h>
AF_DCMotor motor(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
AF_DCMotor motor3(2, MOTOR12_1KHZ);
void setup() {
Serial.begin(9600);
Serial.println("Motor Test!");
motor.setSpeed(20); //set speed of motor
motor2.setSpeed(20); //set speed of motor
motor3.setSpeed(25); //set speed of motor
}
void loop() {
Serial.print("tick");
motor.run(FORWARD);
delay(2000);
Serial.print("tock");
motor.run(RELEASE);
delay(5000);
Serial.print("tack"); //first song starts
motor.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor.run(RELEASE);
delay(14000);
Serial.print("tack");
motor.run(BACKWARD);
delay(2000);
Serial.print("tock"); //first song ends
motor.run(RELEASE);
delay(13000);
Serial.print("tick");
motor.run(FORWARD);
delay(2000);
Serial.print("tock"); //second song starts
motor.run(RELEASE);
delay(24000);
Serial.print("tick");
motor.run(FORWARD);
delay(2000);
Serial.print("tock"); //second song ends
motor.run(RELEASE);
delay(17000);
Serial.print("tick");
motor.run(BACKWARD);
delay(2000);
Serial.print("tock"); //third song starts
motor.run(RELEASE);
delay(23000);
Serial.print("tick");
motor.run(BACKWARD);
delay(2000);
Serial.print("tock"); //third song ends
motor.run(RELEASE);
delay(21000);
Serial.print("tick");
motor.run(FORWARD);
delay(2000);
Serial.print("tock");
motor.run(RELEASE);
delay(5000);
Serial.print("tick");
motor.run(FORWARD);
delay(3000);
Serial.print("tack"); //fourth song starts
motor.run(RELEASE);
delay(5000);
Serial.print("tack");
motor.run(BACKWARD);
delay(2000);
Serial.print("tock"); //fourth song ends
motor.run(RELEASE);
delay(21000);
//waving motor
Serial.print("tick");
motor2.run(FORWARD);
delay(2000);
Serial.print("tock");
motor2.run(RELEASE);
delay(5000);
//Waving motor
Serial.print("tick"); //first song starts
motor2.run(FORWARD);
delay(2000);
Serial.print("tack");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(FORWARD);
delay(2000);
Serial.print("tick");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(FORWARD);
delay(2000);
Serial.print("tack");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(FORWARD);
delay(1000);
Serial.print("tick");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(FORWARD);
delay(2000);
Serial.print("tack");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock"); //first song ends
motor2.run(RELEASE);
delay(14000);
Serial.print("tick");
motor2.run(FORWARD);
delay(2000);
Serial.print("tock");
motor2.run(RELEASE);
delay(5000);
Serial.print("tack"); //SEcond song starts
motor2.run(BACKWARD);
delay(2000);
Serial.print("tick");
motor2.run(FORWARD);
delay(2000);
Serial.print("tack");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(FORWARD);
delay(2000);
Serial.print("tick");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(FORWARD);
delay(2000);
Serial.print("tack");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(FORWARD);
delay(2000);
Serial.print("tick");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(FORWARD);
delay(2000);
Serial.print("tack");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tick");
motor2.run(RELEASE);
delay(1000);
Serial.print("tack");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock"); //second song ends
motor2.run(RELEASE);
delay(15000);
Serial.print("tick");
motor2.run(FORWARD);
delay(2000);
Serial.print("tock");
motor2.run(RELEASE);
delay(5000);
Serial.print("tack"); //Third song starts
motor2.run(BACKWARD);
delay(2000);
Serial.print("tick");
motor2.run(FORWARD);
delay(2000);
Serial.print("tack");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(FORWARD);
delay(2000);
Serial.print("tick");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(FORWARD);
delay(2000);
Serial.print("tack");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(FORWARD);
delay(2000);
Serial.print("tick");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(FORWARD);
delay(2000);
Serial.print("tack");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tick");
motor2.run(RELEASE);
delay(1000);
Serial.print("tack");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tick");
motor2.run(FORWARD);
delay(2000);
Serial.print("tick");
motor2.run(RELEASE);
delay(1000);
Serial.print("tock"); //third song ends
motor2.run(RELEASE);
delay(14000);
Serial.print("tick"); //fourth song starts
motor2.run(FORWARD);
delay(2000);
Serial.print("tack");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(FORWARD);
delay(2000);
Serial.print("tick");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(FORWARD);
delay(2000);
Serial.print("tack");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock");
motor2.run(FORWARD);
delay(1000);
Serial.print("tick");
motor2.run(RELEASE);
delay(1000);
Serial.print("tick");
motor2.run(FORWARD);
delay(2000);
Serial.print("tack");
motor2.run(BACKWARD);
delay(2000);
Serial.print("tock"); //fourth song ends
motor2.run(RELEASE);
delay(7000);
//dancing motor
Serial.print("tock"); //first song starts
motor3.run(RELEASE);
delay(11000);
Serial.print("tick");
motor3.run(FORWARD);
delay(22000);
Serial.print("tock"); //first song stops //second song starts
motor3.run(RELEASE);
delay(16000);
Serial.print("tick"); //Second song stops
motor3.run(FORWARD);
delay(28000);
Serial.print("tock"); //Third song starts
motor3.run(RELEASE);
delay(15000);
Serial.print("tick"); //Third song stops
motor3.run(FORWARD);
delay(33000);
Serial.print("tock"); //fourth song starts
motor3.run(RELEASE);
delay(16000);
Serial.print("tick");
motor3.run(FORWARD);
delay(5000);
Serial.print("tock");
motor3.run(RELEASE);
delay(1000);
Serial.print("tick"); //fourth song ends
motor3.run(FORWARD);
delay(16000);
Serial.print("tock");
motor3.run(RELEASE);
delay(5000);
}
-
1I hope you're not trying to drive the motors directly from the output pins ...Rohan– Rohan2020年04月14日 11:31:15 +00:00Commented Apr 14, 2020 at 11:31
1 Answer 1
As you did not give details I assume the following:
- You supply the MEGA via USB or external 5-12V power supply
- the motors are supplied via an extra external 12V power supply with sufficiant amperage
- All cabeling is checked and correct
Looks like this schematic (without 12V power supply attached to the motor shield) AF_motor_shield_v1 AF_Motor_shield_v2
To your code:
Get rid of the delay()s - they block processing so what happens:
motor.run(FORWARD); // => You give a command via lib delay(2000); // You stop your processing so the motor is not running
so you have two options both need the definition of a timeStamp var
- working with a (blocking) while loops
- or creating a finite state machine (search here on SO for details)
So I will for simplicity use the first approach
// Copy this var before your setup
unsigned long timeStamp = 0;
// use your complete setup
void setup(){...}
// Change your loop functions like thes first three converted lines
void loop(){
timeStamp = millis();
Serial.print("tick");
while(millis()-timeStamp < 2000){
motor.run(FORWARD);
}
timeStamp = millis(); // resets the time to current
Serial.print("tock");
while(millis()-timeStamp < 5000){
motor.run(RELEASE);
}
timeStamp = millis(); // resets the time to current
Serial.print("tack"); //first song starts
while(millis()-timeStamp < 2000){
motor.run(BACKWARD);
}
// .... AND SO ON .....
}
Be aware that the whiles are blocking but for your sequential processing its ok.
-
Comments are not for extended discussion; this conversation has been moved to chat.VE7JRO– VE7JRO2020年04月12日 20:13:49 +00:00Commented Apr 12, 2020 at 20:13
-
2Codebreaker, you are more patient than I.Duncan C– Duncan C2020年04月13日 21:51:03 +00:00Commented Apr 13, 2020 at 21:51
Explore related questions
See similar questions with these tags.