Advertisement
Guest User

ghost_trap_v3

a guest
Sep 10th, 2018
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.52 KB | None | 0 0
  1. /***************************************************
  2.  * Coded by TragicManner
  3.  * version 1.3 - Now with Coroutines/LED support!
  4.  * utilizes the Couroutines library by renaudbedard
  5.  * https://github.com/renaudbedard/littlebits-arduino/tree/master/Libraries/Coroutines
  6.  ***************************************************/
  7.  
  8. #include <Coroutines.h>
  9. #include <Servo.h>
  10.  
  11. // --trap variables
  12. int trapState = 0; // trap states are 0 for idle, 1 for pedal press waiting for doors open, 2 for doors open and audio/LED show active, 3 for doors open and audio/LED show done, 4 for doors open and waiting to close
  13. const int trapIdle = 0;
  14. const int trapOpening = 1;
  15. const int trapOpenShow = 2;
  16. const int trapOpenIdle = 3;
  17. const int trapClosing = 4;
  18.  
  19. // --door variables: servo1--  SERVO OPEN AND CLOSED VALUES NEED TO BE ADJUSTED FOR YOUR SERVOS!
  20. Servo servo1;
  21. const int servo1Pin = 9;
  22. const int servo1OpenPosition = 68;
  23. const int servo1ClosedPosition = 23;
  24.  
  25. // --door variables: servo_2--
  26. Servo servo2;
  27. const int servo2Pin = 10;
  28. const int servo2OpenPosition = 15;
  29. const int servo2ClosedPosition = 63;
  30.  
  31. // --door variables: state--
  32. unsigned long doorTimer = 0;
  33. const int doorWaitTime = 300; // If your doors don't have enough time to close, make this longer
  34. const unsigned long doorOpenTime = 15000; // This is how long the audio and LEDs are active after the doors are first opened. This time is also used to know when to auto-close doors.
  35. const bool autoCloseDoor = true; // If this is true, then after doorOpenTime has elapsed after the doors open, the doors will automatically close.
  36.  
  37. // --Pedal variables--
  38. const int pedalPin = 2;
  39. // pedalState tracks whether or not the pedal is on or off
  40. int pedalState = 0;
  41. int pedalPress = false;
  42.  
  43. // -- General LED variables--
  44. int PWMLEDs[4] = {3,5,6,11}; // the PWM pins that you want to run your LEDs with
  45. int nonPWMLEDs[2] = {7,8}; // the pins that you want to run non-PWM LEDs with -- These are LEDs that just turn on and off
  46.  
  47. // --PWM LED Variables--
  48. int pwmTime = 12000; // How long in ms the PWM LEDs will flash for until they are turned off
  49. int brightness[4] = {0, 255, 100, 255}; // The starting brightness for each PWM pin. 0 is off, 255 is full brightness
  50. int fadeAmount[4] = {100,50,50,50}; // The higher this amount, the faster the LED will fade between on and off
  51. int fadeStep = 30; // This controls how smooth the LED fades will be
  52.  
  53.  
  54. // -- Coroutine Stuff --
  55. Coroutines<3> coroutines;
  56.  
  57. int tempState = 0;
  58.  
  59. void setup() {
  60.   Serial.begin(9600);
  61.   // Set up the pin that watches for the pedal button
  62.   pinMode(pedalPin, INPUT);
  63.   for(int i = 0; i < sizeof(PWMLEDs)/sizeof(int); i++){
  64.     pinMode(PWMLEDs[i], OUTPUT);
  65.   }
  66.   for(int k = 0; k < sizeof(nonPWMLEDs)/sizeof(int); k++){
  67.     pinMode(nonPWMLEDs[k], OUTPUT);
  68.   }
  69.   coroutines.start(CloseDoors);
  70. }
  71.  
  72. void loop() {
  73.   // First, read the state of the pedal
  74.   pedalState = digitalRead(pedalPin);
  75.   if(tempState != trapState) {
  76.     Serial.print("Trap State: ");
  77.     Serial.print(trapState);
  78.     Serial.print('\n');
  79.     tempState = trapState;
  80.   }
  81.  
  82.  
  83.   // Update the LEDs and coroutines
  84.   coroutines.update();
  85.   //FadeLed::update();
  86.  
  87.   // This block detects if a button has been newly pressed (button down) and the trap is in a state to accept pedal presses
  88.   if (pedalState == HIGH && trapState != trapOpening && trapState != trapClosing) {
  89.     Serial.print("Pedal Press Accepted\n");
  90.     pedalPress = true;
  91.  
  92.     if (trapState == trapIdle) {
  93.       doorTimer = millis() + doorOpenTime;
  94.       coroutines.start(OpenDoors);
  95.     }
  96.     else if ((trapState == trapOpenShow || trapState == trapOpenIdle )) {
  97.       coroutines.start(CloseDoors);
  98.     }
  99.   }
  100.   // This checks if the doors have been open for a certain amount of time
  101.   else if(trapState == trapOpenShow && millis() >= doorTimer) {
  102.     trapState = trapOpenIdle;
  103.     if(autoCloseDoor){
  104.       coroutines.start(CloseDoors);
  105.     }
  106.   }
  107.   // This detects if the button is let go (button up), but only if the pedal press was accepted (or done in a state where pedal press is tracked)
  108.   else if (pedalState == LOW && pedalPress == true) {
  109.     pedalPress = false;
  110.     Serial.print("Pedal Release\n");
  111.   }
  112. }
  113.  
  114. void OpenDoors(COROUTINE_CONTEXT(coroutine)) {
  115.   BEGIN_COROUTINE;
  116.   trapState = trapOpening;
  117.   AttachServos();
  118.   MoveServos(servo1OpenPosition, servo2OpenPosition);
  119.   coroutine.wait(doorWaitTime);
  120.   COROUTINE_YIELD;
  121.   TurnOnNonPWMLEDs();
  122.   DetachServos();
  123.   trapState = trapOpenShow;
  124.   coroutines.start(FadeLED);
  125.   END_COROUTINE;
  126. }
  127.  
  128. void CloseDoors(COROUTINE_CONTEXT(coroutine)) {
  129.   BEGIN_COROUTINE;
  130.   trapState = trapClosing;
  131.   AttachServos();
  132.   MoveServos(servo1ClosedPosition, servo2ClosedPosition);
  133.   coroutine.wait(doorWaitTime);
  134.   COROUTINE_YIELD;
  135.   TurnOffNonPWMLEDs();
  136.   DetachServos();
  137.   trapState = trapIdle;
  138.   //coroutines.start(FadeLED);
  139.   END_COROUTINE;
  140. }
  141.  
  142. void FadeLED(COROUTINE_CONTEXT(coroutine)) {
  143.   COROUTINE_LOCAL(unsigned long, finishTime);
  144.  
  145.   BEGIN_COROUTINE;
  146.  
  147.   finishTime = millis() + pwmTime;
  148.  
  149.   while(finishTime > millis() && trapState == trapOpenShow) {
  150.     for (int i = 0; i < sizeof(PWMLEDs)/sizeof(int); i++)
  151.     {
  152.       analogWrite(PWMLEDs[i], brightness[i]);
  153.      
  154.       // change the brightness for next time through the loop:
  155.       brightness[i] = brightness[i] + fadeAmount[i];
  156.  
  157.       if (brightness[i] > 255){
  158.         brightness[i] = 255;
  159.       }
  160.  
  161.       if (brightness[i] < 0) {
  162.         brightness[i] = 0;
  163.       }
  164.    
  165.       // reverse the direction of the fading at the ends of the fade:
  166.       if (brightness[i] <= 0 || brightness[i] >= 255) {
  167.         fadeAmount[i] = -fadeAmount[i];
  168.       }
  169.     }
  170.     coroutine.wait(fadeStep);
  171.     COROUTINE_YIELD;
  172.   }
  173.  
  174.   TurnOffPWMLEDs();
  175.   END_COROUTINE;
  176. }
  177.  
  178. void TurnOnPWMLEDs(){
  179.   for(int i = 0; i < sizeof(PWMLEDs)/sizeof(int); i++)
  180.   {
  181.     analogWrite(PWMLEDs[i], 255);
  182.   }
  183. }
  184.  
  185. void TurnOffPWMLEDs(){
  186.   for(int i = 0; i < sizeof(PWMLEDs)/sizeof(int); i++)
  187.   {
  188.     analogWrite(PWMLEDs[i], 0);
  189.   }
  190. }
  191.  
  192. void TurnOnNonPWMLEDs(){
  193.   for(int i = 0; i < sizeof(nonPWMLEDs)/sizeof(int); i++)
  194.   {
  195.     digitalWrite(nonPWMLEDs[i], HIGH);
  196.   }
  197. }
  198.  
  199. void TurnOffNonPWMLEDs(){
  200.   for(int i = 0; i < sizeof(nonPWMLEDs)/sizeof(int); i++)
  201.   {
  202.     digitalWrite(nonPWMLEDs[i], LOW);
  203.   }
  204. }
  205.  
  206. void AttachServos() {
  207.   servo1.attach(servo1Pin);
  208.   servo2.attach(servo2Pin);
  209. }
  210.  
  211. void MoveServos(int servo1Position, int servo2Position) {
  212.   servo1.write(servo1Position);
  213.   servo2.write(servo2Position);
  214. }
  215.  
  216. void DetachServos() {
  217.   servo1.detach();
  218.   servo2.detach();
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement