Jenis Servo di bagi menjadi 2, servo continues (360 derajat) dan non contiunes (180 derajat).
pada kesempatan kali ini sobat akan belajar bagaimana mengendalikan motor servo tapi tidak menggunakan delay, artinya ada beberapa alasan kenapa perintah delay tidak digunakan dalamsuatu pemrograman. Salah satunya adalah jika ingin multitasking maka akan sulit untuk menentukan, karena delay tidak bisa di ganggu kecuali dengan perintah interupt.
Oke langsung saja,
bahan yang di butuhkan
- Arduino Nano
- Servo
- Kabel Jumper
Sambungkan Kabel Kuning/Orange ke pin 9
Kabel merah ke 5v
kabel hitam ke GND
Program
#include<Servo.h>
Servo myServo;
int servoPin = 9;
int pos;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //default comm speed between arduino and serial monitor. no need to change.
Serial.println("Servo with milis"); // so we know what sketch is running
myServo.attach(servoPin);
}
void loop() {
// put your main code here, to run repeatedly:
currentMillis = millis(); // capture the latest value of millis()
moveservo(70,110,500,500);
}
void moveservo(int pos1, int pos2, int interval1, int interval2) {
if (pos == pos1) {
if ((unsigned long) currentMillis - previousMillis >= interval1) {
pos = pos2;
myServo.write(pos);
// and save the time when we made the change
previousMillis += interval1;
}
}
else {
if ((unsigned long) currentMillis - previousMillis >= interval2) {
pos = pos1;
myServo.write(pos);
// and save the time when we made the change
previousMillis += interval2;
}
}
Selamat Belajar Arduino, Salam Arduino Indonesia
Servo myServo;
int servoPin = 9;
int pos;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //default comm speed between arduino and serial monitor. no need to change.
Serial.println("Servo with milis"); // so we know what sketch is running
myServo.attach(servoPin);
}
void loop() {
// put your main code here, to run repeatedly:
currentMillis = millis(); // capture the latest value of millis()
moveservo(70,110,500,500);
}
void moveservo(int pos1, int pos2, int interval1, int interval2) {
if (pos == pos1) {
if ((unsigned long) currentMillis - previousMillis >= interval1) {
pos = pos2;
myServo.write(pos);
// and save the time when we made the change
previousMillis += interval1;
}
}
else {
if ((unsigned long) currentMillis - previousMillis >= interval2) {
pos = pos1;
myServo.write(pos);
// and save the time when we made the change
previousMillis += interval2;
}
}
ifelse-2button-1servo:26:41: error: 'previousMillis' was not declared in this scope
BalasHapus#include
HapusServo myServo;
int servoPin = 9;
long previousMillis = 0;
unsigned long currentMillis;
int pos;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //default comm speed between arduino and serial monitor. no need to change.
Serial.println("Servo with milis"); // so we know what sketch is running
myServo.attach(servoPin);
}
void loop() {
// put your main code here, to run repeatedly:
// unsigned long currentMillis = millis();
currentMillis = millis(); // capture the latest value of millis()
moveservo(70,110,500,500);
}
void moveservo(int pos1, int pos2, int interval1, int interval2) {
if (pos == pos1) {
if (currentMillis - previousMillis >= interval1) {
pos = pos2;
myServo.write(pos);
// and save the time when we made the change
previousMillis += interval1;
}
}
else {
if (currentMillis - previousMillis >= interval2) {
pos = pos1;
myServo.write(pos);
// and save the time when we made the change
previousMillis += interval2;
}
}
}