用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("
 potVal: ");
  Serial.print(potVal);
  
  angle = map(potVal, potValMin, potValMax, angleMin, angleMax);

  Serial.print("	 angle: ");
  Serial.print(angle);
  
  myServo.write(angle);
  delay(15);
}
原文地址:https://www.cnblogs.com/meetrice/p/14078818.html