Chào các bạn,
Bài viết này sẽ đưa chúng ta tới một ví dụ rất cơ bản trong điều khiển
động cơ servo cùng arduino. Đó là làm thế nào để nhận tín hiệu từ chiết áp và điều khiển
động cơ servo. Bài học này sẽ làm tiền đề cho các bạn chế tạo các thiết bị
điều khiển từ xa với arduino. Nào, hãy bắt đầu thôi.
Học lập trình ở đâu
Đây là video hướng dẫn nhé
Code mẫu đây các bạn nhé
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int val; // variable to read the value from the analog pin
void setup()
{
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
val = analogRead(A1); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
Serial.println(val);
}