שימוש בארדואינו
AFMOTORSHILED - Run 2 DC motor - 12V
הפעלת שני מנועי מתח ישר

#include <AFMotor.h>
AF_DCMotor motor_1(1); // DC motor on M1
AF_DCMotor motor_2(2); // DC motor on M2
int i = 0;
void setup() { // put your setup code here, to run once:
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Begin Motor test using the ADAFRUIT motor shiled usin 2 DC motors"); // prints the words inside the "" on the serial monitor
motor_1.setSpeed(200); // set speed for the motor called 'motor' (DC motor on M1)
motor_2.setSpeed(200); // set speed for the motor called 'motor' (DC motor on M2)
motor_1.run(RELEASE);
motor_2.run(RELEASE);
} // end of void setup
void loop() { // put your main code here, to run repeatedly
Serial.println("Motor 1 and 2 run FORWARD");
motor_1.run(FORWARD); // Coamds DC motor deifined on M1 to run forward
motor_2.run(FORWARD); // Coamds DC motor deifined on M1 to run forward
delay (2000);
Serial.println("Motor 1 and 2 RELEASE");
motor_1.run(RELEASE); // Coamds DC motor deifined on M1 to run forward
motor_2.run(RELEASE); // Coamds DC motor deifined on M1 to run forward
delay (2000);
Serial.println("Motor 1 and 2 run BACKWARD");
motor_1.run(BACKWARD); // Coamds DC motor deifined on M1 to run forward
motor_2.run(BACKWARD); // Coamds DC motor deifined on M1 to run forward
delay (2000);
/* DC MOTOR M1 TEST - Aceelerate and decelerate*/
/* GOING UP FOR LOOP FROM i=0 TO i=255 */
Serial.println("Motor 1 and 2 Will accelerate");
motor_1.run(FORWARD); // Coamds DC motor deifined on M1 to run forward
motor_2.run(FORWARD); // Coamds DC motor deifined on M2 to run forward
for (i=0; i<255; i++) { // for loop will operate 'i' times. in this case 255 times, eache time the loop goes, the value of the variable i grows by +1
motor_1.setSpeed(i); // Sets the speed of DC motor 1 to 'i'
motor_2.setSpeed(i); // Sets the speed of DC motor 2 to 'i'
delay(30); // Wait 3 milliseconds
} // end of 'for' loop
/* GOING DOWN FOR LOOP FROM i=255 TO i=0 */
Serial.println("Motor 1 and 2 Will decelerate");
for (i=255; i!=0; i--) { // for loop will operate 'i' times. in this case 255 times, eache time the loop goes, the value of the variable i grows down by -1
motor_1.setSpeed(i); // Sets the speed of DC motor 1 to 'i'
motor_2.setSpeed(i); // Sets the speed of DC motor 2 to 'i'
delay(30);
} // end of 'for' loop
Serial.println("Motor 1 and 2 RELEASE");
motor_1.run(RELEASE); // Coamds DC motor deifined on M1 to stop
motor_2.run(RELEASE); // Coamds DC motor deifined on M2 to stop
motor_1.setSpeed(200); // Sets the speed of DC motor 1 to '200'
motor_2.setSpeed(200); // Sets the speed of DC motor 2 to '200'
} // end of void loop
AFMOTORSHILED - Run 2 DC motor - 12V
הפעלת שני מנועי מתח ישר
#include <AFMotor.h>
#include <Servo.h>
AF_DCMotor motor_1(1); // DC motor on M1
AF_DCMotor motor_2(2); // DC motor on M1
Servo servo_1; // create servo object to control a servo
AF_Stepper stepper(200, 2); // Connect a stepper motor with 200 steps per revolution (1.85 degrees per step) to motor port #2 (M3 and M4)
// Stepper to motor port #2 (M3 and M4)
int i; // defines a vatiable from the kind integer (int) to be used as a counted in the for loop
void setup() { // put your setup code here, to run once:
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Begin 3 motor test: 2 DC motors, 1 Servo motor and 1 Steper motor"); // prints the words inside the "" on the serial monitor
servo_1.attach(10); // cammands the arduino to use digital pin 10 as signal pin for the servo we named servo1
motor_1.setSpeed(200); // set speed for the motor called 'motor' (DC motor on M1)
motor_2.setSpeed(200); // set speed for the motor called 'motor' (DC motor on M2)
motor_1.run(RELEASE);
motor_2.run(RELEASE);
stepper.setSpeed(10); // 10 rpm
} // end of void setup
void loop() { // put your main code here, to run repeatedly
Serial.println("Motor 1 and 2 run FORWARD");
motor_1.run(FORWARD); // Coamds DC motor deifined on M1 to run forward
motor_2.run(FORWARD); // Coamds DC motor deifined on M1 to run forward
delay (2000);
Serial.println("Motor 1 and 2 RELEASE");
motor_1.run(RELEASE); // Coamds DC motor deifined on M1 to run forward
motor_2.run(RELEASE); // Coamds DC motor deifined on M1 to run forward
delay (2000);
Serial.println("Motor 1 and 2 run BACKWARD");
motor_1.run(BACKWARD); // Coamds DC motor deifined on M1 to run forward
motor_2.run(BACKWARD); // Coamds DC motor deifined on M1 to run forward
delay (2000);
/* DC MOTOR M1 TEST - Aceelerate and decelerate*/
/* GOING UP FOR LOOP FROM i=0 TO i=255 */
Serial.println("Motor 1 and 2 Will accelerate");
motor_1.run(FORWARD); // Coamds DC motor deifined on M1 to run forward
motor_2.run(FORWARD); // Coamds DC motor deifined on M2 to run forward
for (i=0; i<255; i++) { // for loop will operate 'i' times. in this case 255 times, eache time the loop goes, the value of the variable i grows by +1
motor_1.setSpeed(i); // Sets the speed of DC motor 1 to 'i'
motor_2.setSpeed(i); // Sets the speed of DC motor 2 to 'i'
delay(30); // Wait 3 milliseconds
} // end of 'for' loop
/* GOING DOWN FOR LOOP FROM i=255 TO i=0 */
Serial.println("Motor 1 and 2 Will decelerate");
for (i=255; i!=0; i--) { // for loop will operate 'i' times. in this case 255 times, eache time the loop goes, the value of the variable i grows down by -1
motor_1.setSpeed(i); // Sets the speed of DC motor 1 to 'i'
motor_2.setSpeed(i); // Sets the speed of DC motor 2 to 'i'
delay(30);
} // end of 'for' loop
Serial.println("Motor 1 and 2 RELEASE");
motor_1.run(RELEASE); // Coamds DC motor deifined on M1 to stop
motor_2.run(RELEASE); // Coamds DC motor deifined on M2 to stop
motor_1.setSpeed(200); // set speed for the motor called 'motor' (DC motor on M1)
motor_2.setSpeed(200); // set speed for the motor called 'motor' (DC motor on M2)
/* SERVO TEST - Move Clockwise and Counter clockwise*/
/* GOING UP FOR LOOP FROM i=0 TO i=255 */
Serial.println("Servo motor will move from '0' to '180'");
for (i=0; i<180; i++) { // for loop will operate 'i' times. in this case 255 times, eache time the loop goes, the value of the variable i grows by +1
servo_1.write(i); // Coamds The servo motor we named 'servo1' to go to position 'i'
delay(15); // Wait 3 milliseconds
} // end of 'for' loop
/* GOING DOWN FOR LOOP FROM i=255 TO i=0 */
Serial.println("Servo motor will move from '180' to '0'");
for (i=180; i!=0; i--) { // for loop will operate 'i' times. in this case 255 times, eache time the loop goes, the value of the variable i grows down by -1
servo_1.write(i); // Coamds The servo motor we named 'servo_1' to go to position 'i'
delay(15);
} // end of 'for' loop
/* The stepper motor recieves 3 valuse stepper.step( NUMBER OF STEPS, DIRECTION (FORWARD or BACKWARD), KIND OF STEP (SINGLE, DOUBLE, INTERLEAVE and MICROSTEP)) this programs run forward and backword and demonstrate all 4 kinds of steps */
Serial.println("Now for the stepper motor test"); // print the line 'SinGle coil steps' to the serial monitor
Serial.println("Single coil steps"); // print the line 'SinGle coil steps' to the serial monitor
stepper.step(100, FORWARD, SINGLE); // 100 steps FORWARD in SINGLE step mode
stepper.step(100, BACKWARD, SINGLE); // 100 steps BACKWARD in SINGLE step mode
Serial.println("Double coil steps"); // print the line 'Double coil steps' to the serial monitor
stepper.step(100, FORWARD, DOUBLE); // 100 steps FORWARD in DOUBLE step mode
stepper.step(100, BACKWARD, DOUBLE); // 100 steps BACKWARD in DOUBLE step mode
Serial.println("Interleave coil steps"); // print the line 'Interleave coil steps' to the serial monitor
stepper.step(100, FORWARD, INTERLEAVE); // 100 steps FORWARD in INTERLEAVE step mode
stepper.step(100, BACKWARD, INTERLEAVE); // 100 steps BACKWARD in INTERLEAVE step mode
Serial.println("Micrsostep steps"); // print the line 'Micrsostep steps' to the serial monitor
stepper.step(100, FORWARD, MICROSTEP); // 100 steps FORWARD in MICROSTEP step mode
stepper.step(100, BACKWARD, MICROSTEP); // 100 steps BACKWARD in MICROSTEP step mode
} // end of void loop
#include <AFMotor.h>
#include <Servo.h>
AF_DCMotor motor_1(1); // DC motor on M1
AF_DCMotor motor_2(2); // DC motor on M1
Servo servo_1; // create servo object to control a servo
AF_Stepper stepper(200, 2); // Connect a stepper motor with 200 steps per revolution (1.85 degrees per step) to motor port #2 (M3 and M4)
// Stepper to motor port #2 (M3 and M4)
int i; // defines a vatiable from the kind integer (int) to be used as a counted in the for loop
void setup() { // put your setup code here, to run once:
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Begin 3 motor test: 2 DC motors, 1 Servo motor and 1 Steper motor"); // prints the words inside the "" on the serial monitor
servo_1.attach(10); // cammands the arduino to use digital pin 10 as signal pin for the servo we named servo1
motor_1.setSpeed(200); // set speed for the motor called 'motor' (DC motor on M1)
motor_2.setSpeed(200); // set speed for the motor called 'motor' (DC motor on M2)
motor_1.run(RELEASE);
motor_2.run(RELEASE);
stepper.setSpeed(10); // 10 rpm
} // end of void setup
void loop() { // put your main code here, to run repeatedly
Serial.println("Motor 1 and 2 run FORWARD");
motor_1.run(FORWARD); // Coamds DC motor deifined on M1 to run forward
motor_2.run(FORWARD); // Coamds DC motor deifined on M1 to run forward
delay (2000);
Serial.println("Motor 1 and 2 RELEASE");
motor_1.run(RELEASE); // Coamds DC motor deifined on M1 to run forward
motor_2.run(RELEASE); // Coamds DC motor deifined on M1 to run forward
delay (2000);
Serial.println("Motor 1 and 2 run BACKWARD");
motor_1.run(BACKWARD); // Coamds DC motor deifined on M1 to run forward
motor_2.run(BACKWARD); // Coamds DC motor deifined on M1 to run forward
delay (2000);
/* DC MOTOR M1 TEST - Aceelerate and decelerate*/
/* GOING UP FOR LOOP FROM i=0 TO i=255 */
Serial.println("Motor 1 and 2 Will accelerate");
motor_1.run(FORWARD); // Coamds DC motor deifined on M1 to run forward
motor_2.run(FORWARD); // Coamds DC motor deifined on M2 to run forward
for (i=0; i<255; i++) { // for loop will operate 'i' times. in this case 255 times, eache time the loop goes, the value of the variable i grows by +1
motor_1.setSpeed(i); // Sets the speed of DC motor 1 to 'i'
motor_2.setSpeed(i); // Sets the speed of DC motor 2 to 'i'
delay(30); // Wait 3 milliseconds
} // end of 'for' loop
/* GOING DOWN FOR LOOP FROM i=255 TO i=0 */
Serial.println("Motor 1 and 2 Will decelerate");
for (i=255; i!=0; i--) { // for loop will operate 'i' times. in this case 255 times, eache time the loop goes, the value of the variable i grows down by -1
motor_1.setSpeed(i); // Sets the speed of DC motor 1 to 'i'
motor_2.setSpeed(i); // Sets the speed of DC motor 2 to 'i'
delay(30);
} // end of 'for' loop
Serial.println("Motor 1 and 2 RELEASE");
motor_1.run(RELEASE); // Coamds DC motor deifined on M1 to stop
motor_2.run(RELEASE); // Coamds DC motor deifined on M2 to stop
motor_1.setSpeed(200); // set speed for the motor called 'motor' (DC motor on M1)
motor_2.setSpeed(200); // set speed for the motor called 'motor' (DC motor on M2)
/* SERVO TEST - Move Clockwise and Counter clockwise*/
/* GOING UP FOR LOOP FROM i=0 TO i=255 */
Serial.println("Servo motor will move from '0' to '180'");
for (i=0; i<180; i++) { // for loop will operate 'i' times. in this case 255 times, eache time the loop goes, the value of the variable i grows by +1
servo_1.write(i); // Coamds The servo motor we named 'servo1' to go to position 'i'
delay(15); // Wait 3 milliseconds
} // end of 'for' loop
/* GOING DOWN FOR LOOP FROM i=255 TO i=0 */
Serial.println("Servo motor will move from '180' to '0'");
for (i=180; i!=0; i--) { // for loop will operate 'i' times. in this case 255 times, eache time the loop goes, the value of the variable i grows down by -1
servo_1.write(i); // Coamds The servo motor we named 'servo_1' to go to position 'i'
delay(15);
} // end of 'for' loop
/* The stepper motor recieves 3 valuse stepper.step( NUMBER OF STEPS, DIRECTION (FORWARD or BACKWARD), KIND OF STEP (SINGLE, DOUBLE, INTERLEAVE and MICROSTEP)) this programs run forward and backword and demonstrate all 4 kinds of steps */
Serial.println("Now for the stepper motor test"); // print the line 'SinGle coil steps' to the serial monitor
Serial.println("Single coil steps"); // print the line 'SinGle coil steps' to the serial monitor
stepper.step(100, FORWARD, SINGLE); // 100 steps FORWARD in SINGLE step mode
stepper.step(100, BACKWARD, SINGLE); // 100 steps BACKWARD in SINGLE step mode
Serial.println("Double coil steps"); // print the line 'Double coil steps' to the serial monitor
stepper.step(100, FORWARD, DOUBLE); // 100 steps FORWARD in DOUBLE step mode
stepper.step(100, BACKWARD, DOUBLE); // 100 steps BACKWARD in DOUBLE step mode
Serial.println("Interleave coil steps"); // print the line 'Interleave coil steps' to the serial monitor
stepper.step(100, FORWARD, INTERLEAVE); // 100 steps FORWARD in INTERLEAVE step mode
stepper.step(100, BACKWARD, INTERLEAVE); // 100 steps BACKWARD in INTERLEAVE step mode
Serial.println("Micrsostep steps"); // print the line 'Micrsostep steps' to the serial monitor
stepper.step(100, FORWARD, MICROSTEP); // 100 steps FORWARD in MICROSTEP step mode
stepper.step(100, BACKWARD, MICROSTEP); // 100 steps BACKWARD in MICROSTEP step mode
} // end of void loop

L298N Motor driver - Using two DC motors. Up to 36V
הפעלת שני מנועי מתח ישר
// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;
void setup()
{
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Begin Motor test using the L298n motor driver"); // prints the words inside the "" on the serial monitor
}
void demoOne()
{
// this function will run the motors in both directions at a fixed speed
// turn on motor A
Serial.println("Motor A speed 200");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enA, 200);
// turn on motor B
Serial.println("Motor B speed 200");
digitalWrite(in3 , HIGH);
digitalWrite(in4, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enB, 200);
delay(2000);
// now change motor directions
Serial.println("Motor A and B REVERSE speed 200");
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
// now turn off motors
Serial.println("Motor A and B OFF");
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void demoTwo()
{
// this function will run the motors across the range of possible speeds
// note that maximum speed is determined by the motor itself and the operating voltage
// the PWM values sent by analogWrite() are fractions of the maximum speed possible
// by your hardware
// turn on motors
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// accelerate from zero to maximum speed
Serial.println("Motor A and B Slowly accelerating");
for (int i = 0; i < 256; i++) {
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
// decelerate from maximum speed to zero
Serial.println("Motor A and B Slowly decelerating");
for (int i = 255; i > 0; --i)
{
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
// now turn off motors
Serial.println("Motor A and B OFF");
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop()
{
demoOne();
delay(1000);
demoTwo();
delay(1000);
}