Ein Servo wird von einem Potentiometer kontrolliert.
/*
* Poti controls Servo
* Controlling a servo position using a potentiometer (variable resistor)
*
* Anmerkung: was bei Fehlern hilft: einen Pin ohne PWM und RX/TX benutzen (also 2, 4, 7, 8, 12 oder 13)
*
* UNBEDINGT BEACHTEN: Wenn die Servo-Library benutzt wird, geht auf den digitalen
* Pins 9 und 10 die PWM-Funktionalitaet verloren. D.h., dass man auf diesen
* Pins z.B. keine LEDs mehr dimmen kann.
*/
#include <Servo.h> // servo-bibliothek laden
Servo myservo; // create servo object to control a servo
int servoPin = 8; // choose pin 8 to connect servo
int potpin = 2; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(servoPin); // attaches the servo on pin 8 to the servo object
Serial.begin(9600); // initialize serial interface
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
Serial.println(val); // print value to Serial Monitor
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
Dieses Beispiel herunterladen
