Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Proton Pistol control code.
- //Written by J.Teasdale - GBFans WindDrake
- //Dog simple code here, with room for expansion.
- //Inputs
- int TriggerSwitch = 0;
- //Outputs
- int SoundTrigger = 4;
- int NFET = 2;
- //Timer Variables
- unsigned long currentMillis = 0;
- unsigned long previousMillis = 0;
- long PlayTime = 2352; //Length of Sound File EXACTLY in mS.
- unsigned long PlayTimer = 0;
- //LED Cycle Times
- long LEDOn = 25;
- long LEDOff = 50;
- //State Variables (these must mirror states in Setup loop)
- int TriggerState = HIGH;
- int SoundState = HIGH;
- int NFETState = LOW;
- //Boot Setup
- void setup() {
- pinMode(TriggerSwitch, INPUT_PULLUP);
- pinMode(SoundTrigger, OUTPUT);
- pinMode(NFET, OUTPUT);
- digitalWrite(SoundTrigger, TriggerState);
- digitalWrite(NFET, NFETState);
- }
- //Main Loop
- void loop() {
- currentMillis = millis(); //update the timer for the LED
- PlayTimer = millis(); //update the timer for the sound playback
- TriggerState = digitalRead(TriggerSwitch); //Update the state of the trigger.
- //Here's the LED driver code.
- if ((NFETState == HIGH) && (currentMillis - previousMillis >= LEDOn)) //If LED is on (Logic Level NFET gate HIGH) beyond time limit
- {
- NFETState = LOW; //Turn off FET
- previousMillis = currentMillis; //Update the timer.
- digitalWrite(NFET, LOW); //Actually turn off the FET pin.
- }
- else if ((NFETState == LOW) && (currentMillis - previousMillis >= LEDOff) && (TriggerState == LOW)) //If LED is OFF for too long and the trigger is held down..
- {
- NFETState = HIGH; //Turn it ON
- previousMillis = currentMillis; //Update the timer.
- digitalWrite(NFET, HIGH); //Turn ON the NFET gate.
- }
- //Here's the Sound driver code.
- if ((currentMillis - PlayTimer >= PlayTime) && (SoundState == LOW)) //If sound is playing..
- {
- SoundState = HIGH; //Release sound trigger line.
- PlayTimer = currentMillis; //Update the timer.
- digitalWrite(SoundTrigger, HIGH); //Release the sound trigger line to the WT588 sound chip.
- }
- else if ((SoundState == HIGH) && (TriggerState == LOW)) //If trigger is pushed and sound is currently not queued..
- {
- SoundState = LOW; //Activate the sound trigger line (State).
- PlayTimer = currentMillis; //Update the timer.
- digitalWrite(SoundTrigger, LOW); //Activate the sound trigger line.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment