用Tinkercad学arduino之 电位器控制伺服电机转向

项目地址:https://www.tinkercad.com/things/azCWlFFOMtQ

#include <Servo.h>
Servo myServo;

const int serialPort = 9600;
const int potPin = A0;
const int servoPin = 9;
const int potValMin = 0;
const int potValMax = 1023;
const int angleMin = 0;
const int angleMax = 179;

int potVal;
int angle;

void setup()
{
  myServo.attach(servoPin);
  Serial.begin(serialPort);
}

void loop()
{
  potVal = analogRead(potPin);
  
  Serial.print("\n potVal: ");
  Serial.print(potVal);
  
  angle = map(potVal, potValMin, potValMax, angleMin, angleMax);

  Serial.print("\t angle: ");
  Serial.print(angle);
  
  myServo.write(angle);
  delay(15);
}

 

posted @ 2020-12-03 12:03  meetrice  阅读(1132)  评论(0编辑  收藏  举报