Ok, here is the code for my pair of relay-controlled stampedes.
I'll apologize now because it is both horribly commented and way more than you need for what you want to do.
This project had an LCD display that showed ammo for both stampedes, had configurable burst fire, semi, and fully automatic fire modes, and maintained preferences when powering on and off.
here is the bit that managed the relay and stampede fire cycle switch.void pullTrigger(int barrel) {//0 neither, 1 upper, 2 lower if(barrel == 1) { while(digitalRead(UpperSensor) == HIGH) {//check and see if the firing cycle has taken over Serial.println("firing interval"); digitalWrite(RelayUpper, LOW);//turn the relay on delay(FIRING_INTERVAL);//wait a little bit digitalWrite(RelayUpper, HIGH);//turn the relay off } } else if(barrel == 2) { while(digitalRead(LowerSensor) == HIGH) {//check and see if the firing cycle has taken over Serial.println("firing interval"); digitalWrite(RelayLower, LOW);//turn the relay on delay(FIRING_INTERVAL);//wait a little bit digitalWrite(RelayLower, HIGH);//turn the relay off } } }
Like i said, i had two stampedes, but there you can see how it handled firing via relay.
The only issue I have with that is that I would like my LED to be flashing even when firing. A delay wouldn't allow that. Now, adding a whole timing function is a lot more work, but I'd really rather not have the whole code stop for firing anyway.