servo.ino 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Servo Logic Code
  3. This script handles logic related to servo operations
  4. */
  5. //Quickily push the switch back
  6. void pushSwitchNow() {
  7. pushSwitchDelayed(270, 270);
  8. }
  9. //Push switch back with delay
  10. void pushSwitchDelayed(int coverDelay, int pusherDelay) {
  11. servoCoverPusher.write(90);
  12. delay(coverDelay);
  13. servoSwitchPusher.write(130 + SERVO_ALIGNMENT_OFFSET);
  14. delay(pusherDelay);
  15. handlePushBackException(); //Check if switch is properly pushed back
  16. servoCoverPusher.write(0);
  17. servoSwitchPusher.write(0);
  18. }
  19. //Push with a pause before the switch is pushed
  20. void pushWithHesitation() {
  21. servoCoverPusher.write(90);
  22. delay(1000);
  23. servoSwitchPusher.write(90 + SERVO_ALIGNMENT_OFFSET);
  24. delay(2000);
  25. servoSwitchPusher.write(130 + SERVO_ALIGNMENT_OFFSET);
  26. delay(1000);
  27. handlePushBackException(); //Check if switch is properly pushed back
  28. servoCoverPusher.write(0);
  29. servoSwitchPusher.write(0);
  30. }
  31. //Handle case where user hold the switch
  32. void handlePushBackException() {
  33. bool switchPushed = getSwitchState();
  34. int triedResetCount = 0;
  35. if (switchPushed) {
  36. //Switch position still not reset. Wait and try again
  37. while (triedResetCount < 5) {
  38. switchPushed = getSwitchState();
  39. if (!switchPushed) {
  40. //Switch reset
  41. return;
  42. }
  43. if (triedResetCount < 1) {
  44. //Light push retry
  45. servoSwitchPusher.write(90 + SERVO_ALIGNMENT_OFFSET);
  46. delay(300);
  47. servoSwitchPusher.write(130 + SERVO_ALIGNMENT_OFFSET);
  48. delay(1000);
  49. }else if (triedResetCount < 3) {
  50. //Light push retry
  51. servoSwitchPusher.write(90 + SERVO_ALIGNMENT_OFFSET);
  52. delay(500);
  53. servoSwitchPusher.write(130 + SERVO_ALIGNMENT_OFFSET);
  54. delay(2000);
  55. } else {
  56. //Hard push retry
  57. servoSwitchPusher.write(0);
  58. delay(500);
  59. servoSwitchPusher.write(130 + SERVO_ALIGNMENT_OFFSET);
  60. delay(2000);
  61. }
  62. triedResetCount++;
  63. }
  64. //Max retry reached
  65. switchPushed = getSwitchState();
  66. if (switchPushed){
  67. char originalAnicode = animation;
  68. setAnimationCode('v');
  69. delay(5000);
  70. setAnimationCode(originalAnicode); //Restore the animation charcode
  71. }
  72. return;
  73. }
  74. }
  75. //Set the pusher servo to arm installation position
  76. void setSwitchToInstallPosition() {
  77. servoSwitchPusher.write(125 + SERVO_ALIGNMENT_OFFSET);
  78. }