Hi, I'm hoping someone can help me with the code I have been working on for the past three months on and off working. I am trying to build an arduino controlled Nerf gun for my son but I've hit a couple of problems. The first of which I thought would be easy to get working but I cannot get it to behave as I would like.
Upon pulling the trigger, I want the blaster to begin turning the flywheels (which accelerate the dart) and then have a small pause while they get up to full speed before the pusher motor begins pushing the darts into the flywheels. If I use delay it causes the rest of the code to run slowly so I started looking into guides on using millis to handle the small delay.
The portion of the code which handles the control of the flywheel and pusher motors is shown below. You'll note that the blaster has four different firemodes; 0= Safety, 1 = Single Shot, 2 = Burst Fire and 3 = Full Auto. I'm trying to get this functionality working in the full auto section before I then try and get it to work in the single shot and burst fire modes.
void selectFire () {
triggerSwitch.read(); //read trigger
if (triggerSwitch.isPressed()) { //check if trigger is pressed
unsigned long revStart = millis();
if (fireMode == 0) { //check if in safety mode
digitalWrite(pusherGo, LOW);
digitalWrite(flywheelPin, LOW);
} else if (fireMode == 1 || fireMode == 2) { //if in burst fire or single shot mode
aDartIsFired = true; //allow for darts to be fired, handled elsewhere
} else if (fireMode == 3) { //if full auto turn on motors
analogWrite(flywheelPin, flywheelPower * 2.55); //PWM to flyhweel motor flywheel power pecentage x 2.55 to give PWM range from 0 to 255
if ((millis() - revStart) > 200) {
analogWrite(pusherGo, rateOfFire * 2.55);
} //PWM to pusher motor flywheel power pecentage x 2.55 to give PWM range from 0 to 255 //turn on pusher motor
dartsFired = 0;
dartsToFire = 0;
revStart = 0;
}
}
else if (!triggerSwitch.isPressed()) { //if trigger isn't pressed
if (fireMode == 3 || fireMode == 0) { //if firemode is fullauto or safety, turn off motor
digitalWrite(pusherGo, LOW);
digitalWrite(flywheelPin, LOW);
} else if ( !aDartIsFired //if all darts fired
&& (fireMode == 1 || fireMode == 2) ) { //and in burstfire
}
}
}
As the code is written above, the pusher motor never turns on after 200 milliseconds have elapsed. I'm at a loss as to why.
Any help would be much appreciated