Guest User

Untitled

a guest
Apr 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. //---------LIBRARIES
  2. #include <Servo.h>
  3.  
  4. //---------CONSTANTS(won't change)
  5.  
  6. const int servoPin = 9;
  7. const int buttonPin = 8;
  8.  
  9. const int servoMinDegrees = 10;
  10. const int servoMaxDegrees = 170;
  11.  
  12. //---------VARIABLES(will change)
  13. Servo myservo;
  14. int val = 5;
  15. int servoPosition = 90;
  16. int servoSlowInterval = 80;
  17. int servoFastInterval = 10;
  18. int servoInterval = servoSlowInterval;
  19. int servoDegrees = 25;
  20.  
  21. unsigned long currentMillis = 0;
  22. unsigned long previousServoMillis = 0;
  23.  
  24. //==============
  25. void setup() {
  26. Serial.begin(9600);
  27. Serial.println("Hinata is bae");
  28.  
  29. pinMode(buttonPin, INPUT_PULLUP);
  30.  
  31. myservo.write(servoPosition);
  32. myservo.attach(servoPin);
  33. }
  34. //==============
  35.  
  36. void loop() {
  37. currentMillis = millis();
  38. servoSweep();
  39. }
  40.  
  41. //==============
  42.  
  43. void servoSweep() {
  44. if (digitalRead(buttonPin) == HIGH){
  45. val = val + 1;
  46. }
  47.  
  48. if (val > 1){
  49. val = 0;
  50. }
  51.  
  52. if (val < 1){
  53. if (currentMillis - previousServoMillis >= servoInterval){
  54. previousServoMillis += servoInterval;
  55.  
  56. servoPosition = servoPosition + servoDegrees;
  57.  
  58. if(servoPosition <= servoMinDegrees){
  59. if(servoInterval == servoSlowInterval){
  60. servoInterval = servoFastInterval;
  61. }}
  62. else {
  63. servoInterval = servoSlowInterval;
  64. }
  65. if((servoPosition >= servoMaxDegrees)||(servoPosition <=servoMinDegrees)) {
  66. servoDegrees = - servoDegrees;
  67. servoPosition = servoPosition + servoDegrees;
  68. }
  69. myservo.write(servoPosition);
  70. }
  71. }
  72. }
Add Comment
Please, Sign In to add comment