DC MOTOR SHIELD

DC motor shield

#include <AFMotor.h>

AF_DCMotor Motor1(1);
AF_DCMotor Motor2(2);
AF_DCMotor Motor3(3);
AF_DCMotor Motor4(4);

void setup()
{
}

void loop()
{
//setting forward speed
Motor1.setSpeed(70);
Motor2.setSpeed(70);
Motor3.setSpeed(80);
Motor4.setSpeed(80);
//foward
Motor1.run(FORWARD);
Motor2.run(FORWARD);
Motor3.run(FORWARD);
Motor4.run(FORWARD);
delay(1000);
Motor1.run(BRAKE);
Motor2.run(BRAKE);
Motor3.run(BRAKE);
Motor4.run(BRAKE);
delay(1000);
//stop
Motor1.setSpeed(0);
Motor2.setSpeed(0);
Motor3.setSpeed(0);
Motor4.setSpeed(0);
delay(1000);
//setting backward speed
Motor1.setSpeed(70);
Motor2.setSpeed(70);
Motor3.setSpeed(80);
Motor4.setSpeed(80);
//backward
Motor1.run(BACKWARD);
Motor2.run(BACKWARD);
Motor3.run(BACKWARD);
Motor4.run(BACKWARD);
delay(1000);
Motor1.run(BRAKE);
Motor2.run(BRAKE);
Motor3.run(BRAKE);
Motor4.run(BRAKE);
delay(1000);

}

 

OTHER SKET

#include <AFMotor.h>

AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor3(3, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor4(4, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Motor test!”);

motor1.setSpeed(80); // set the speed to 200/255
motor2.setSpeed(80); // set the speed to 200/255
motor3.setSpeed(80); // set the speed to 200/255
motor4.setSpeed(80);
}

void loop() {
Serial.print(“tick”);

motor1.run(FORWARD); // turn it on going forward
delay(1000);

Serial.print(“tack”);
motor1.run(RELEASE); // stopped
delay(1000);

Serial.print(“tock”);
motor1.run(BACKWARD); // the other way
delay(1000);

Serial.print(“tack”);
motor1.run(RELEASE); // stopped
delay(1000);

}

OTHER SKET

#include <AFMotor.h>

AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor3(3, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor4(4, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Motor test!”);

motor1.setSpeed(80); // set the speed to 200/255
motor2.setSpeed(80); // set the speed to 200/255
motor3.setSpeed(80); // set the speed to 200/255
motor4.setSpeed(80);
}

void loop() {
Serial.print(“M1”);

motor1.run(FORWARD); // turn it on going forward
delay(1000);

Serial.print(“tack”);
motor1.run(RELEASE); // stopped
delay(1000);

Serial.print(“tock”);
motor1.run(BACKWARD); // the other way
delay(1000);

Serial.println(“tack”);
motor1.run(RELEASE); // stopped
delay(1000);

//————-

Serial.print(“M2”);

motor2.run(FORWARD); // turn it on going forward
delay(1000);

Serial.print(“tack”);
motor2.run(RELEASE); // stopped
delay(1000);

Serial.print(“tock”);
motor2.run(BACKWARD); // the other way
delay(1000);

Serial.println(“tack”);
motor2.run(RELEASE); // stopped
delay(1000);

//—————-

Serial.print(“M3”);

motor3.run(FORWARD); // turn it on going forward
delay(1000);

Serial.print(“tack”);
motor3.run(RELEASE); // stopped
delay(1000);

Serial.print(“tock”);
motor3.run(BACKWARD); // the other way
delay(1000);

Serial.println(“tack”);
motor3.run(RELEASE); // stopped
delay(1000);

//————–

Serial.print(“M4”);

motor4.run(FORWARD); // turn it on going forward
delay(1000);

Serial.print(“tack”);
motor4.run(RELEASE); // stopped
delay(1000);

Serial.print(“tock”);
motor4.run(BACKWARD); // the other way
delay(1000);

Serial.println(“tack”);
motor4.run(RELEASE); // stopped
delay(1000);

}

OTHER SKET

#include <AFMotor.h>

AF_DCMotor Motor1(1);

AF_DCMotor Motor2(2);

AF_DCMotor Motor3(3);

AF_DCMotor Motor4(4);

void setup()

{

Serial.begin(9600);

}

void loop()

{

if (Serial.available() > 0) {

int inByte = Serial.read();

int speed; // Local variable

switch (inByte) {

case ‘8’: // Forward

maju();

break;

case ‘2’: // Backward

mundur();

break;

case ‘6’: // Right

kanan();

break;

case ‘4’: // Left

kiri();

break;

case ‘5’: // Motor 1 Stop (Freespin)

stops();

break;

default:

break;

}

}

}

void maju(){

Motor1.setSpeed(70);

Motor2.setSpeed(70);

Motor3.setSpeed(80);

Motor4.setSpeed(80);

//foward

Motor1.run(FORWARD);

Motor2.run(FORWARD);

Motor3.run(FORWARD);

Motor4.run(FORWARD);

}

void mundur(){

Motor1.setSpeed(70);

Motor2.setSpeed(70);

Motor3.setSpeed(80);

Motor4.setSpeed(80);

//backward

Motor1.run(BACKWARD);

Motor2.run(BACKWARD);

Motor3.run(BACKWARD);

Motor4.run(BACKWARD);

}

void kanan(){

Motor1.setSpeed(70);

Motor2.setSpeed(70);

Motor3.setSpeed(80);

Motor4.setSpeed(80);

//foward

Motor1.run(FORWARD);

Motor2.run(FORWARD);

Motor3.run(BACKWARD);

Motor4.run(BACKWARD);

}

void kiri(){

Motor1.setSpeed(70);

Motor2.setSpeed(70);

Motor3.setSpeed(80);

Motor4.setSpeed(80);

//foward

Motor1.run(BACKWARD);

Motor2.run(BACKWARD);

Motor3.run(FORWARD);

Motor4.run(FORWARD);

}

void stops(){

Motor1.setSpeed(0);

Motor2.setSpeed(0);

Motor3.setSpeed(0);

Motor4.setSpeed(0);

}

OTHER SKET / RUNNING BY KEYBOARD

#include <AFMotor.h>

AF_DCMotor Motor1(1);
AF_DCMotor Motor2(2);
AF_DCMotor Motor3(3);
AF_DCMotor Motor4(4);

void setup()
{
Serial.begin(9600);
}

void loop()
{
if (Serial.available() > 0) {
int inByte = Serial.read();
int speed; // Local variable

switch (inByte) {
case ‘1’: // Motor 1 Forward
Motor1.setSpeed(70);
Motor2.setSpeed(70);
Motor3.setSpeed(80);
Motor4.setSpeed(80);
//foward
Motor1.run(FORWARD);
Motor2.run(FORWARD);
Motor3.run(FORWARD);
Motor4.run(FORWARD);
break;

case ‘2’: // Motor 1 Stop (Freespin)

Motor1.setSpeed(70);
Motor2.setSpeed(70);
Motor3.setSpeed(80);
Motor4.setSpeed(80);
//backward
Motor1.run(BACKWARD);
Motor2.run(BACKWARD);
Motor3.run(BACKWARD);
Motor4.run(BACKWARD);
break;

case ‘3’: // Motor 1 Reverse
Motor1.setSpeed(0);
Motor2.setSpeed(0);
Motor3.setSpeed(0);
Motor4.setSpeed(0);
break;
default:
break;

}
}
}