adafruit_support_bill wrote:I don't see any obvious reason for a reset. I'd add some serial monitor diagnostic output to see what is actually going on in your loop.
I got an Uno and ran some serial data, here is the code.
Code: Select all
const int triggerPin = 7; // input form trigger switch on airsoft gun
const int rpmPin = 4; // input from hall switch in airsoft gun gearbox
const int motorPin = 2; // output to gearbox motor control MOSFET gate
int triggerState = 0; // current state of the trigger pin
int buttonPushCounter = 0; // number of gearbox realutions
int buttonState = 0; // current state of the rpm pin
int lastButtonState = 0; // previous state of the rpm pin
void setup()
{
pinMode(triggerPin, INPUT); // set trigger pin as input
digitalWrite(triggerPin, HIGH); // turn on internal pull up resistor for trigger pin
pinMode(rpmPin, INPUT); // set rpm pin as input
digitalWrite(rpmPin, HIGH); // turn on internal pull up resistor for rpm pin
pinMode(motorPin, OUTPUT); // set motor pin as output
Serial.begin(9600);
}
void loop() {
triggerState = digitalRead(triggerPin); // read the trigger pin input
buttonState = digitalRead(rpmPin); // read the rpm pin input
if (buttonState != lastButtonState) { // compare the buttonState to its previous state
// if it went from high to low than the than the state has changed
if (buttonState == LOW) { // if the state has changed
buttonPushCounter++; // increment the counter
Serial.println(buttonPushCounter);
}
}
lastButtonState = buttonState; // save the current state as the last state,
//for next time through the loop
if (triggerState == LOW && buttonPushCounter < 3) { // check if the trigger has been pulled and the counter is less than 3
digitalWrite(motorPin, HIGH); // if so write motor pin high switching on the MOSFET and powering the motor
Serial.println("on");
} else {
digitalWrite(motorPin, LOW); // if not write motor pin low
} // all the code above works fine and stops the gearbox after three rounds, but to reset I have to power cycle the computer
if (triggerState == HIGH); { // this code should reset the counter if the trigger is released
Serial.println("off");
buttonPushCounter = 0; // but it resets the counter with the trigger pressed and the gearbox keeps turning
Serial.println("reset");
} // what i need is a way to reset the counter when the trigger is released and only than
}
Aside from printing a lot of data. It did tell me that the reset is running with the trigger on or off. This does not make sense to me as it should not run with the trigger on.
I then checked just the triggerState with this code.
Code: Select all
const int triggerPin = 7; // input form trigger switch on airsoft gun
const int rpmPin = 4; // input from hall switch in airsoft gun gearbox
const int motorPin = 2; // output to gearbox motor control MOSFET gate
int triggerState = 0; // current state of the trigger pin
int buttonPushCounter = 0; // number of gearbox realutions
int buttonState = 0; // current state of the rpm pin
int lastButtonState = 0; // previous state of the rpm pin
void setup()
{
pinMode(triggerPin, INPUT); // set trigger pin as input
digitalWrite(triggerPin, HIGH); // turn on internal pull up resistor for trigger pin
pinMode(rpmPin, INPUT); // set rpm pin as input
digitalWrite(rpmPin, HIGH); // turn on internal pull up resistor for rpm pin
pinMode(motorPin, OUTPUT); // set motor pin as output
Serial.begin(9600);
}
void loop() {
triggerState = digitalRead(triggerPin); // read the trigger pin input
buttonState = digitalRead(rpmPin); // read the rpm pin input
if (buttonState != lastButtonState) { // compare the buttonState to its previous state
// if it went from high to low than the than the state has changed
if (buttonState == LOW) { // if the state has changed
buttonPushCounter++; // increment the counter
Serial.println(buttonPushCounter);
}
}
lastButtonState = buttonState; // save the current state as the last state,
//for next time through the loop
if (triggerState == LOW && buttonPushCounter < 3) { // check if the trigger has been pulled and the counter is less than 3
digitalWrite(motorPin, HIGH); // if so write motor pin high switching on the MOSFET and powering the motor
} else {
digitalWrite(motorPin, LOW); // if not write motor pin low
} // all the code above works fine and stops the gearbox after three rounds, but to reset I have to power cycle the computer
if (triggerState == HIGH); { // this code should reset the counter if the trigger is released
buttonPushCounter = 0; // but it resets the counter with the trigger pressed and the gearbox keeps turning
} // what i need is a way to reset the counter when the trigger is released and only than
Serial.println(triggerState);
}
I am getting a 1 with the trigger released and a 0 with the trigger pressed as it should be.
Let me know what you think. Thanks