伺服馬達使用與狀態監控
跟隨佑來認真教練習伺服馬達使用與狀態監控
影片
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int sensor = 0;
int angle = 0 ;
int beginState =0;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop() {
sensor = analogRead(A0);
angle = map(sensor,0,1023,0,180);
if(sensor !=beginState){
Serial.print(sensor);
Serial.println(angle);
myservo.write(angle); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
beginState = sensor;
delay(15);
}
留言
張貼留言