Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. /*
  2. * Matthew Park
  3. * Servo Motor, function map, constrain, Learn to use libraries
  4. *
  5. * When you use a library, must INCLUDE the library.
  6. */
  7.  
  8. #include <Servo.h>
  9.  
  10. Servo servito;
  11. const int PinPot=A0;
  12. const int PinLed=2;
  13. int Value;
  14. int Angle;
  15. int Min = 1000;
  16. int Max = 0;
  17.  
  18.  
  19.  
  20.  
  21. void setup() {
  22. // put your setup code here, to run once:
  23. pinMode(PinPot, INPUT);
  24. servito.attach(9);
  25. Serial.begin(9600);
  26. calibrate();
  27. }
  28.  
  29. void calibrate()
  30. {
  31. digitalWrite(PinLed, HIGH);
  32. delay(50);
  33. digitalWrite(PinLed,LOW);
  34. unsigned long intTime=millis();
  35. while (millis()-intTime <=5000)
  36.  
  37. analogRead(PinPot);
  38. if( Value < Min)
  39. {
  40. Min = Value;
  41. }
  42. if( Value > Max)
  43. {
  44. Max=Value;
  45. }
  46. }
  47. void loop() {
  48. // put your main code here, to run repeatedly:
  49. Value = analogRead(PinPot);
  50. Angle = map(Value, Min,Max,0,180);
  51. Angle = constrain(Angle, 0, 180);
  52. servito.write(Angle);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement