Guest User

Proton Pistol 1.0

a guest
May 21st, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. //Proton Pistol control code.
  2. //Written by J.Teasdale - GBFans WindDrake
  3. //Dog simple code here, with room for expansion.
  4.  
  5.  
  6. //Inputs
  7. int TriggerSwitch = 0;
  8.  
  9. //Outputs
  10. int SoundTrigger = 4;
  11. int NFET = 2;
  12.  
  13. //Timer Variables
  14. unsigned long currentMillis = 0;
  15. unsigned long previousMillis = 0;
  16. long PlayTime = 2352; //Length of Sound File EXACTLY in mS.
  17. unsigned long PlayTimer = 0;
  18.  
  19. //LED Cycle Times
  20. long LEDOn = 25;
  21. long LEDOff = 50;
  22.  
  23. //State Variables (these must mirror states in Setup loop)
  24. int TriggerState = HIGH;
  25. int SoundState = HIGH;
  26. int NFETState = LOW;
  27.  
  28. //Boot Setup
  29. void setup() {
  30. pinMode(TriggerSwitch, INPUT_PULLUP);
  31. pinMode(SoundTrigger, OUTPUT);
  32. pinMode(NFET, OUTPUT);
  33. digitalWrite(SoundTrigger, TriggerState);
  34. digitalWrite(NFET, NFETState);
  35. }
  36.  
  37. //Main Loop
  38. void loop() {
  39. currentMillis = millis(); //update the timer for the LED
  40. PlayTimer = millis(); //update the timer for the sound playback
  41. TriggerState = digitalRead(TriggerSwitch); //Update the state of the trigger.
  42.  
  43. //Here's the LED driver code.
  44. if ((NFETState == HIGH) && (currentMillis - previousMillis >= LEDOn)) //If LED is on (Logic Level NFET gate HIGH) beyond time limit
  45. {
  46. NFETState = LOW; //Turn off FET
  47. previousMillis = currentMillis; //Update the timer.
  48. digitalWrite(NFET, LOW); //Actually turn off the FET pin.
  49. }
  50. else if ((NFETState == LOW) && (currentMillis - previousMillis >= LEDOff) && (TriggerState == LOW)) //If LED is OFF for too long and the trigger is held down..
  51. {
  52. NFETState = HIGH; //Turn it ON
  53. previousMillis = currentMillis; //Update the timer.
  54. digitalWrite(NFET, HIGH); //Turn ON the NFET gate.
  55. }
  56. //Here's the Sound driver code.
  57. if ((currentMillis - PlayTimer >= PlayTime) && (SoundState == LOW)) //If sound is playing..
  58. {
  59. SoundState = HIGH; //Release sound trigger line.
  60. PlayTimer = currentMillis; //Update the timer.
  61. digitalWrite(SoundTrigger, HIGH); //Release the sound trigger line to the WT588 sound chip.
  62. }
  63. else if ((SoundState == HIGH) && (TriggerState == LOW)) //If trigger is pushed and sound is currently not queued..
  64. {
  65. SoundState = LOW; //Activate the sound trigger line (State).
  66. PlayTimer = currentMillis; //Update the timer.
  67. digitalWrite(SoundTrigger, LOW); //Activate the sound trigger line.
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment