arduino机械臂原理(Arduino实例三十一)
arduino机械臂原理(Arduino实例三十一)#include <Servo.h> #include <SoftwareSerial.h> Servo myservo; // create servo object to control a servo int pot = 0; // analog pin used to connect the potentiometer 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(pot);
1 电路连接图示
GND(棕色)接 Arduino GND PWM(橙色)接 Arduino Digital 10 VCC(红色)接 Arduino 5V
2 实物连接图示
3 程序
#include <Servo.h>
#include <SoftwareSerial.h>
Servo myservo; // create servo object to control a servo
int pot = 0; // analog pin used to connect the potentiometer
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(pot); // 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
Serial.println(val);
delay(30); // waits for the servo to get there
}
4 视频效果
可调电阻控制机械臂底盘旋转 - 西瓜视频 (ixigua.com)