Skip to content

Instantly share code, notes, and snippets.

@ahalekelly
Created August 15, 2018 07:01
Show Gist options
  • Save ahalekelly/1795498b2457367fff279122ba8b04ef to your computer and use it in GitHub Desktop.
Save ahalekelly/1795498b2457367fff279122ba8b04ef to your computer and use it in GitHub Desktop.
Nerf select fire pseudocode
const uint16_t pusherTimeout = 300; // ms
const uint16_t pusherDebounceTime = 50; // ms
const uint8_t pusherMotorPin;
const uint8_t ccSwitchPin;
uint32_t time = 0;
uint32_t pusherTimeOn = 0;
uint32_t pusherTimeOff = 0;
bool ccSwitchPressed;
bool pusherState = 0;
uint8_t shotsToFire = 0;
void loop() {
ccSwitchPressed = digitalRead(ccSwitchPin);
time = millis();
if (ccSwitchPressed == 1) { // if pusher is back
if (pusherState == 0 && shotsToFire > 0) { // and pusher is not running and it should be
digitalWrite(pusherMotorPin, 1);
pusherState = 1;
timePusherOn = time;
shotsToFire -= 1;
} else if (pusherState == 1 && time - timePusherOn > pusherDebounceTime) { // either pusher has finished its cycle or it failed to run. Stop
digitalWrite(pusherMotorPin, 0);
pusherState = 0;
timePusherOff = time;
}
} else { // if pusher is forward
if (pusherState == 0 && shotsToFire > 0) { // and pusher isn't on
digitalWrite(pusherMotorPin, 1); // turn pusher on
timePusherOn = time;
pusherState = 1;
} else if (time - timePusherOn > pusherTimeout) { // if pusher has been running for too long. probably stalled, should protect gearbox
digitalWrite(pusherMotorPin, 0);
pusherState = 0;
timePusherOff = time;
shotsToFire = 0;
// throw error or something?
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment