Making Semi Auto Mosfet For airsoft

Hi everyone,
Im currently working on a arduino controlled mosfet for my airsoftDMR. I have a cycle sensor cuttoff) that clicks everytime the gun cycles 1 time along with a trigger button and a relay to activate the mosfet.

I've been trying to make a program that looks for the trigger button to be pressed. As that happens, the relay must click 'ON' until the cycle sensor detects a full rotation of the gearbox. As soon as the firing cycle is completed, i want the arduino to turn OFF the relay even if the trigger button is still pressed. Once i release the trigger button and press it again, i want the cycle to fire once again

The trigger button gives a HIGH output along with the cycle detection button.

I've tried many diferent solutions and havent got to make it work

If you guys have any guidance that would be great!

const int triggerPin = 3;
const int cutoffPin = 5;// the number of the pushbutton pin
const int relayPin = 2;
const int ledPin = 13;// the number of the LED pin

// variables will change:
int triggerState = 0; // variable for reading the pushbutton status
int cutoffState = 0;
int lastTriggerState = 0;
int relayState = 0;

void setup() {
pinMode(relayPin, OUTPUT);
pinMode(triggerPin, INPUT);
pinMode(cutoffPin, INPUT);
}

void loop() {
triggerState = digitalRead(triggerPin);
cutoffState = digitalRead(cutoffPin);

if(triggerState != lastTriggerState){
if (triggerState == HIGH) {
digitalWrite(relayPin, HIGH);
}
if (cutoffState == HIGH) {
digitalWrite(relayPin, LOW);
delay(50);
lastTriggerState = triggerState;
}
}
}

g36.ino (1.37 KB)

triggerState never gets set to LOW.

You only ever check the cutoffState when triggerState changes. I think you should check it on every loop although it does something different depending if the trigger is currently held.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

How are you driving the relay?

Thanks.. Tom.. :slight_smile:

If it works can, you post the code.