Advertisement
Guest User

ghost_trap_v3

a guest
Sep 1st, 2018
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. // --door variables: servo1--  SERVO OPEN AND CLOSED VALUES NEED TO BE ADJUSTED FOR YOUR SERVOS!
  4. Servo servo1;
  5. const int servo1Pin = 9;
  6. const int servo1OpenPosition = 68;
  7. const int servo1ClosedPosition = 32;
  8.  
  9. // --door variables: servo_2--
  10. Servo servo2;
  11. const int servo2Pin = 10;
  12. const int servo2OpenPosition = 15;
  13. const int servo2ClosedPosition = 63;
  14.  
  15. // --door variables: state--
  16. bool doorOpen = false;
  17. const int doorWaitTime = 300; // If your doors don't have enough time to close, make this longer
  18.  
  19. // --input variables--
  20. const int postInputDelay = 100;
  21.  
  22. // --Pedal variables--
  23. const int pedalPin = 2;
  24. // pedalState tracks whether or not the pedal is on or off
  25. int pedalState = 0;
  26. int pedalPress = false;
  27.  
  28. void setup() {
  29.   Serial.begin(9600);
  30.   // Set up the pin that watches for the pedal button
  31.   pinMode(pedalPin, INPUT);
  32.   CloseDoors();
  33. }
  34.  
  35. void loop() {
  36.   // First, read the state of the pedal
  37.   pedalState = digitalRead(pedalPin);
  38.  
  39.   // This block detects if a button has been newly pressed (button down)
  40.   if(pedalState == HIGH && pedalPress == false) {
  41.     Serial.print("Pedal Press");
  42.     Serial.print('\n');
  43.     pedalPress = true;
  44.    
  45.     if(pedalPress == true && doorOpen == false) {
  46.       OpenDoors();
  47.       doorOpen = true;
  48.     }
  49.  
  50.     else if(pedalPress == true && doorOpen == true) {
  51.       CloseDoors();
  52.       doorOpen = false;
  53.     }
  54.     delay(postInputDelay);
  55.   }
  56.  
  57.   // This detects if the button is let go (button up)
  58.   else if(pedalState == LOW && pedalPress == true) {
  59.     pedalPress = false;
  60.     Serial.print("Pedal Release");
  61.     Serial.print('\n');
  62.     delay(postInputDelay);
  63.   }
  64. }
  65.  
  66. void OpenDoors() {
  67.   AttachServos();
  68.   MoveServos(servo1OpenPosition, servo2OpenPosition);
  69.   delay(doorWaitTime);
  70.   DetachServos();
  71. }
  72.  
  73. void CloseDoors() {
  74.   AttachServos();
  75.   MoveServos(servo1ClosedPosition, servo2ClosedPosition);
  76.   delay(doorWaitTime);
  77.   DetachServos();
  78. }
  79.  
  80. void AttachServos() {
  81.   servo1.attach(servo1Pin);
  82.   servo2.attach(servo2Pin);
  83. }
  84.  
  85. void MoveServos(int servo1Position, int servo2Position) {
  86.   servo1.write(servo1Position);
  87.   servo2.write(servo2Position);
  88. }
  89.  
  90. void DetachServos() {
  91.   servo1.detach();
  92.   servo2.detach();
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement