PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 line
939B

  1. /*
  2. Controlling a servo position using a potentiometer (variable resistor)
  3. by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
  4. modified on 8 Nov 2013
  5. by Scott Fitzgerald
  6. http://arduino.cc/en/Tutorial/Knob
  7. */
  8. #include <Servo.h>
  9. Servo myservo; // create servo object to control a servo
  10. int potpin = 0; // analog pin used to connect the potentiometer
  11. int val; // variable to read the value from the analog pin
  12. void setup()
  13. {
  14. myservo.attach(9); // attaches the servo on pin 9 to the servo object
  15. }
  16. void loop()
  17. {
  18. val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
  19. val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
  20. myservo.write(val); // sets the servo position according to the scaled value
  21. delay(15); // waits for the servo to get there
  22. }