- Thu Feb 23, 2012 9:39 pm
#140257
OK, I looked that your code and what was gnawing at me wasn't important (a redundant IF condition). For getting the gun to fire 1 cycle ... it should work. After all right now you're still shaking out the hardware side. I'd do a test where I held the trigger firmly pressed and see if it triples and then release the trigger and see if you get any ADs. Then I'd increase the DebounceDelay to something wild, perhaps 200 msec and see if that stops your tripling.
I've commented up your code (just the main loop) and got rid of the FP calcs (you'd need to redo the threshold number). See if you agree with the comments, as that'll tell me I've understood your thinking.
I've commented up your code (just the main loop) and got rid of the FP calcs (you'd need to redo the threshold number). See if you agree with the comments, as that'll tell me I've understood your thinking.
Code: Select all
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) //trigger is pressed
{
analogWrite(MotorPin, MotorSpeed); //turn on motor
digitalWrite(ledPin, HIGH); //light LED, indicate trigger pressed
do
{
IRead = analogRead(CurrentSensor); //read current sensor
//IRead = (IMain-511.5)*0.66; //delete
}
while (IRead > LowAmpTrigger); //need to set threshold in A/D counts
//stay in loop until current is low (after spring release)
if (IRead < LowAmpTrigger) //redundant, only way to get here is if current below threshold
{
do
{
buttonState = digitalRead(buttonPin); //read trigger state
//delay(50);
analogWrite(MotorPin, 0); //turn motor off as soon as amps low since 1 cycle only
}
while (buttonState == HIGH); //stay in loop until trigger is released
}
delay(DebounceDelay); //exit loop after trigger released, wait to read trigger again
digitalWrite(ledPin, LOW); //write LED indicator to trigger off state
}
else //trigger not pressed
{
digitalWrite(ledPin, LOW); //if trigger not pressed, LED = off
analogWrite(MotorPin, 0); //turn motor off whenever trigger not pressed, prior to next cycle
}
}
Last edited by Mee_n_Mac on Fri Feb 24, 2012 3:02 am, edited 2 times in total.