Download - Sketch P10 - Antarmuka Motor DC
P10 : ANTARMUKA MOTOR DCP10 : ANTARMUKA MOTOR DC
P10.1 – SERVO P10.2 – STEPPER
A. Controlling position (sweep)
#include <Servo.h>
Servo myservo; // create servo object to control a servo int angle = 0; // variable to store the servo position
void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object }
void loop() { for(angle = 0; angle < 180; angle += 1) // goes from 0 to 180 degrees { // in steps of 1 degree myservo.write(angle); // tell servo to go to position in var 'angle' delay(20); // waits 20ms between servo commands } for(angle = 180; angle >= 1; angle = 1) // goes from 180 to 0 degrees { myservo.write(angle); // move servo in opposite direction delay(20); // waits 20ms between servo commands
… continued
A. One Step at a Time
/* Stepper Motor Control one step at a time This program drives a unipolar or bipolar stepper motor. The motor is attached to digital pins 8 11 of the Arduino. The motor will step one step at a time, very slowly. You can use this to test that you've got the four wires wired to the correct pins. If wired correctly, all steps should be in the same direction. Use this also to count the number of steps per revolution of your motor, if you don't know it. Then plug that number into the oneRevolution example to see if you got it right.*/
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the // of steps per revolution for your motor
… continued
Page 1 of 10 | P10 Antarmuka Motor DC
} } B. Controlling with potentiometer (Knob)
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin
void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object }
void loop() { val = analogRead(potpin); // reads the value of the potentiometer val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo myservo.write(val); // sets position to the scaled value delay(15); // waits for the servo to get there } C. Controlling the speed of 2 servos
#include <Servo.h>
… continued
// initialize the stepper library on pins 8 through 11:Stepper myStepper(stepsPerRevolution, 8,9,10,11);
int stepCount = 0; // number of steps the motor has taken
void setup() { // initialize the serial port: Serial.begin(9600);}
void loop() { // step one step: myStepper.step(1); Serial.print("steps:" ); Serial.println(stepCount); stepCount++; delay(500);}
B. One Revolution
/* Stepper Motor Control one revolution This program drives a unipolar or bipolar stepper motor. The motor is attached to digital pins 8 11 of the Arduino. The motor should revolve one revolution in one direction, then one revolution in the other direction.
… continued
Page 2 of 10 | P10 Antarmuka Motor DC
Servo myservoLeft; // create servo object to control a servo Servo myservoRight; // create servo object to control a servo
int angle = 0; // variable to store the servo position
void setup() { myservoLeft.attach(9); // attach left servo on pin 9 to servo obj myservoRight.attach(10); // attach right servo on pin 10 to servo obj}
void loop() { for(angle = 90; angle < 180; angle += 1) // goes from 90 to 180 deg { // in steps of 1 degree // 90 degrees is stopped myservoLeft.write(angle); // rotate servo at speed given by 'angle' myservoRight.write(180angle); // go in the opposite direction
delay(20); // waits 20ms between servo commands } for(angle = 180; angle >= 90; angle = 1) // goes from 180 to 90 deg { myservoLeft.write(angle); // rotate at a speed given by 'angle' myservoRight.write(180angle); // other servo goes in opposite dir } }
*/#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number // of steps per revolution for your motor
// initialize the stepper library on pins 8 through 11:Stepper myStepper(stepsPerRevolution, 8,9,10,11);
void setup() { // set the speed at 60 rpm: myStepper.setSpeed(60); // initialize the serial port: Serial.begin(9600);}
void loop() { // step one revolution in one direction: Serial.println("clockwise"); myStepper.step(stepsPerRevolution); delay(500); // step one revolution in the other direction: Serial.println("counterclockwise"); myStepper.step(stepsPerRevolution); delay(500); }
… next
Page 3 of 10 | P10 Antarmuka Motor DC
C. Speed control
/* Stepper Motor Control speed control This program drives a unipolar or bipolar stepper motor. The motor is attached to digital pins 8 11 of the Arduino. A potentiometer is connected to analog input 0. The motor will rotate in a clockwise direction. The higher the potentiometer value, the faster the motor speed. Because setSpeed() sets the delay between steps, you may notice the motor is less responsive to changes in the sensor value at low speeds. */
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number // of steps per revolution for your motor
// initialize the stepper library on pins 8 through 11:Stepper myStepper(stepsPerRevolution, 8,9,10,11);
int stepCount = 0; // number of steps the motor has taken
void setup() { // nothing to do inside the setup
… continued
Page 4 of 10 | P10 Antarmuka Motor DC
}
void loop() { // read the sensor value: int sensorReading = analogRead(A0); // map it to a range from 0 to 100: int motorSpeed = map(sensorReading, 0, 1023, 0, 100); // set the motor speed: if (motorSpeed > 0) { myStepper.setSpeed(motorSpeed); // step 1/100 of a revolution: myStepper.step(stepsPerRevolution/100); } }
P10.3 – MOTOR SHIELD
///This motor shield use Pin 6,5,7,4 to control the motor// Simply connect your motors to M1+,M1, M2+,M2// Upload the code to Arduino// Through serial monitor, type 'a','s', 'w','d','x' to control the motor int EN1 = 6; int EN2 = 5; int IN1 = 7;int IN2 = 4;
Page 5 of 10 | P10 Antarmuka Motor DC
… continuedvoid Motor1(int pwm, boolean reverse) { analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed if(reverse) { digitalWrite(IN1,HIGH); } else { digitalWrite(IN1,LOW); } } void Motor2(int pwm, boolean reverse) { analogWrite(EN2,pwm); if(reverse) { digitalWrite(IN2,HIGH); } else { digitalWrite(IN2,LOW); } } void setup() { int i;
… continued
Page 6 of 10 | P10 Antarmuka Motor DC
// for(i=6;i<=9;i++) //For Roboduino Motor Shield // pinMode(i, OUTPUT); //set pin 6,7,8,9 to output mode for(i=4;i<=7;i++) //For Arduino Motor Shield pinMode(i, OUTPUT); //set pin 4,5,6,7 to output mode Serial.begin(9600); } void loop() { int x,delay_en; char val; while(1) { val = Serial.read(); if(val!=1) { switch(val) { case 'w'://Move ahead Motor1(100,true); //You can change the speed, such as Motor(50,true) Motor2(100,true); break; case 'x'://move back Motor1(100,false); Motor2(100,false); break;
… continued
Page 7 of 10 | P10 Antarmuka Motor DC
case 'a'://turn left Motor1(100,false); Motor2(100,true); break; case 'd'://turn right Motor1(100,true); Motor2(100,false); break; case 's'://stop Motor1(0,false); Motor2(0,false); break; } } } }
Page 8 of 10 | P10 Antarmuka Motor DC
Page 9 of 10 | P10 Antarmuka Motor DC
P10.1 A P10.1 B P10.1 C
P10.2 ABC P10.3
Page 10 of 10 | P10 Antarmuka Motor DC