PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

34 lines
1.6KB

  1. // Controlling a servo position using a potentiometer (variable resistor)
  2. // by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
  3. // http://arduiniana.org/libraries/pwmservo/
  4. // Board SERVO_PIN_A SERVO_PIN_B SERVO_PIN_C
  5. // ----- ----------- ----------- -----------
  6. // Arduino Uno, Duemilanove 9 10 (none)
  7. // Arduino Mega 11 12 13
  8. // Sanguino 13 12 (none)
  9. // Teensy 1.0 17 18 15
  10. // Teensy 2.0 14 15 4
  11. // Teensy++ 1.0 or 2.0 25 26 27
  12. // Teensy LC & 3.x (all PWM pins are usable)
  13. #include <PWMServo.h>
  14. PWMServo myservo; // create servo object to control a servo
  15. const int potpin = A0; // analog pin used to connect the potentiometer
  16. int val; // variable to read the value from the analog pin
  17. void setup() {
  18. myservo.attach(SERVO_PIN_A); // attaches the servo on pin 9 to the servo object
  19. //myservo.attach(SERVO_PIN_A, 1000, 2000); // some motors need min/max setting
  20. }
  21. void loop() {
  22. val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
  23. val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
  24. myservo.write(val); // sets the servo position according to the scaled value
  25. delay(15); // waits for the servo to get there
  26. }