DC motor control with a hall switch as feed back need help

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Balancedbadger
 
Posts: 6
Joined: Thu Jan 16, 2014 6:14 pm

DC motor control with a hall switch as feed back need help

Post by Balancedbadger »

What I am trying to do is have my airsoft gun fire three round burst.I am using the Trinket as the controler and a hall effect switch as a feed back sensor in the gear box. The problem I am having is with programming. I can get it to fire three rounds and stop but when I add code to reset the round counter it fire on full auto. So what I need is code to reset the round counter only when the trigger is released. Here is the code with comments. Any help would be great, thanks

Code: Select all

const int  triggerPin = 0; // input form trigger switch on airsoft gun
const int  rpmPin = 2; // input from hall switch in airsoft gun gearbox
const int  motorPin = 1; // 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
}

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
    }
  }
  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
}

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: DC motor control with a hall switch as feed back need he

Post by adafruit_support_mike »

Try moving the reset up into your "button state changed" conditional:

Code: Select all

  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
    } else {
      buttonPushCounter = 0;  // trigger released, reset counter
    }
  }

Balancedbadger
 
Posts: 6
Joined: Thu Jan 16, 2014 6:14 pm

Re: DC motor control with a hall switch as feed back need he

Post by Balancedbadger »

I moved the reset code as suggested and the result is the same. One thing I note is when "buttonPushCounter = 0" is anywhere in the loop It is being run each time though. It does not seem to matter what if statement I put before it. any farther suggestion would be great, thanks.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: DC motor control with a hall switch as feed back need he

Post by adafruit_support_bill »

How is your trigger wired? Do you have a pullup resistor?

Balancedbadger
 
Posts: 6
Joined: Thu Jan 16, 2014 6:14 pm

Re: DC motor control with a hall switch as feed back need he

Post by Balancedbadger »

adafruit_support_bill wrote:How is your trigger wired? Do you have a pullup resistor?
I have the trigger wired to pin 0 and to ground. The trigger grounds pin 0 when closed. I am using the internal pull-up resister.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: DC motor control with a hall switch as feed back need he

Post by adafruit_support_bill »

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.

Balancedbadger
 
Posts: 6
Joined: Thu Jan 16, 2014 6:14 pm

Re: DC motor control with a hall switch as feed back need he

Post by Balancedbadger »

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 would love to be able to run serial monitor but the Trinket does not make that easy. I just attempted to set up Trinket Fake Serial Monitor but keep getting a time out error when running example 2. I am starting to think getting a full sized Arduino for prototyping may be the way to go.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: DC motor control with a hall switch as feed back need he

Post by adafruit_support_bill »

Not a bad plan. It's a great Trinket-sized project. But sometimes you really need the diagnostic tools.

Balancedbadger
 
Posts: 6
Joined: Thu Jan 16, 2014 6:14 pm

Re: DC motor control with a hall switch as feed back need he

Post by Balancedbadger »

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

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: DC motor control with a hall switch as feed back need he

Post by adafruit_support_bill »

The problem is right here:

Code: Select all

if (triggerState == HIGH); { 
The semicolon terminates your 'if' statement, so the next statements get executed unconditionally.

Balancedbadger
 
Posts: 6
Joined: Thu Jan 16, 2014 6:14 pm

Re: DC motor control with a hall switch as feed back need he

Post by Balancedbadger »

That did the trick, thank you so much.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”