Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* BLACK BETTY HPA NERF BLASTER Electronic Fire Control Group source code V0.1
- Original code by DaXLR 2018/11
- This code is free to use and modify under an Attribution/Non-commercial CC license
- As I commented the code I noticed a bunch of pretty bad practices and sub-optimal stuff, but I'm too lazy to fix it for now. This code works and is (mostly) readable. Might change it later
- Be safe and have fun!
- */
- int solenoid = 2; //Output pin for solenoid
- int trigger = 11; //Input pin for trigger microswitch
- int safety = 9; //Input pin for saftey logic signal
- int fullauto = 7; //Input pin for full auto logic signal
- int dwell = 75; //Time in ms that the system is going to keep air going to the piston to push the BCG forward, more dwell time means more reliable cycling but less time to charge the reservoir between shots (so less power)
- int cooldown = 200; //Time in ms between shots. Used for both full and semi-auto. Less cooldown means faster fire rate, but less time to charge the reservoir between shots (so less power)
- int antibounce = 15; //Time in ms that the trigger needs to be held down to register as a shot. Used to prevent accidental triggers from the blaster cycling.
- unsigned long currentmillis = 0; //Used for global timings (see the BlinkWithoutDelay tutorial on Arduino to understand how this works)
- unsigned long previouscooldown = 0; //Notes the last time a cooldown period has started
- unsigned long previousdwell = 0; //Notes the last time a dwell period has started
- int shotqueue = 0; //Keeps track of if the system should fire or not
- int firemode = 0; //Keeps track of the firemode, 0 is safe, 1 is semi, 2 is full
- int waitforrelease = 0; //Flag to wait for the trigger release in semi-auto
- void setup() {
- Serial.begin(9600);
- //Setting pinmodes
- pinMode(safety, INPUT);
- pinMode(fullauto, INPUT);
- pinMode(solenoid, OUTPUT);
- pinMode(trigger, INPUT);
- pinMode(13, OUTPUT);
- }
- void loop() {
- //Looking for current fire selector position
- if (digitalRead(safety)) {
- firemode = 0;
- }
- else if (digitalRead(fullauto)) {
- firemode = 2;
- }
- else {
- firemode = 1; //If the fire selector is broken or absent, this is the default firemode that<s going to be used
- }
- //Semi auto mode
- if (firemode == 1) {
- if (digitalRead(trigger) && waitforrelease == 0) { //Wait for release is only equals to 0 as the trigger is released. This means that this code is only ever going to run once per trigger press since the flag is set to true within it.
- delay(antibounce);
- if (digitalRead(trigger)) {
- waitforrelease = 1; //Sets the flag to ignore the trigger until released
- shotqueue = 1; //Sets the flag for the shooting code to actually fire
- }
- }
- else if (!digitalRead(trigger)) {
- waitforrelease = 0; //Resets the flag if trigger is released
- }
- //Firing code
- currentmillis = millis();
- if (currentmillis >= previouscooldown + cooldown) { //The code is only ever going to chech for the firing flag if the system is outside of its cooldown period
- if (shotqueue >= 1) {
- Serial.println("Shot Fired");
- digitalWrite(solenoid, HIGH);
- digitalWrite(13, HIGH); //Pin 13 on the metro mini is the onboard LED, useful for a quick indicator.
- previousdwell = millis(); //Tells the code to start counting dwell time from here
- previouscooldown = millis(); //Tells the code to start counting cooldown from here
- shotqueue = 0; //Resets the firing flag
- }
- }
- // Code to check if dwell time has elapsed
- currentmillis = millis();
- if (millis() >= previousdwell + dwell) {
- digitalWrite(solenoid, LOW);
- digitalWrite(13, LOW);
- }
- }
- //Full auto, same code as semi-auto, except you dont need the waitforrelease flag.
- if (firemode == 2) {
- if (shotqueue <= 0) {
- shotqueue = 0;
- }
- if (shotqueue >= 1) {
- shotqueue = 1;
- }
- if (digitalRead(trigger)) {
- shotqueue = 1;
- }
- else if (!digitalRead(trigger)) {
- shotqueue = 0;
- }
- currentmillis = millis();
- if (currentmillis >= previouscooldown + cooldown) {
- if (shotqueue >= 1) {
- Serial.println("Shot Fired");
- digitalWrite(solenoid, HIGH);
- digitalWrite(13, HIGH);
- previousdwell = millis();
- previouscooldown = millis();
- shotqueue = 0;
- }
- }
- currentmillis = millis();
- if (millis() >= previousdwell + dwell) {
- digitalWrite(solenoid, LOW);
- digitalWrite(13, LOW);
- }
- }
- }
Add Comment
Please, Sign In to add comment