Monday, April 29, 2013

Stampede dart counter tutorial

Email questions/comments to munerfmods@gmail.com

Video here:

 
 
Sparkfun (is bestfun) parts list: http://sfe.io/w62306

Diagram (click to enlarge) This is non-interrupt reset pin model.




Code here:  (Note, you need to download the new soft serial library for this counter)  Also, this code hasn't been changed to the interrupt on the reset pin.  I've coded that but need to test it, so here the reset button is not an interrupt.

I will be working to better document this code so it's more clear.



#include <SoftwareSerial.h>
SoftwareSerial RpSerial(11, 10); // RX, TX
const int threshold = 10;   // minimum reading of the sensors that generates a note
//We use an array here because of how the serial display work.  You can't do int based math on them
int ammoarray1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3};
int ammoarray2[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7};
int countarray = 36;  //default dart counter number (I have this changed to 18 below, but you can change it here
int inPinReset = 6;  //the reset button pin 
int state = 0;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
int stateReset = HIGH;      // the current state of the output pin
int readingReset;           // the current reading from the input pin
int previousReset = LOW;    // the previous reading from the input pin
int totalCount = 0;  // Total Shots made while on
static unsigned long last_interrupt_time = 0;
int runonce = 1;

long time = 0;         // the last time the output pin was toggled
long debounce = 150;   // the debounce time, increase if the output flickers
long timeReset = 0;         // the last time the output pin was toggled
long debounceReset = 300;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(10,OUTPUT);  //this sets pin 10 for output for serial communication to the display
  RpSerial.begin(9600);
  pinMode(2, INPUT);  //light input pin //sets pin 2 and an interrupt pin (dart counter)
  attachInterrupt(0, CheckIfDartShot, RISING);  //delcares pin 2 as a interrupt pin on rising voltage (the normal step down resistor method
 
}

void loop()

   if (runonce == 1)  //runs this just when the gun startsup
   { 
   
delay(200);
    //this stuff sets the sparkfun LCD base settings (including brightness)
     RpSerial.print("v");
     RpSerial.print("w");
     RpSerial.write((byte)1110000);
     RpSerial.print("z");
     RpSerial.write((byte)00000001);    
     delay(1000);
     RpSerial.print("----");
   
    
  

    countarray = 18;  //static mag size
    myprint();  //myprint updates the display  We want the display to say 18
   
     runonce = 0;
    }
   
   
    CheckForReset();//checks to see if the reset switch was pressed
    Blink2(); //cool sounds while at different levels of darts
    Blink();  //the oh crap you're out of ammo blink.
  
}
void myprint()  //my print just updates the displays on our neat array
{      
  RpSerial.print("-");
  RpSerial.print("-");
  RpSerial.print(ammoarray1[countarray]);
  RpSerial.print(ammoarray2[countarray]); 
}
   
void CheckIfDartShot(){ 
  //this subroutine is called when our interrupt pin is hit.

  unsigned long interrupt_time = millis();  //debounce crap
 
  
  
   if (interrupt_time - last_interrupt_time > debounce){
  
    if (countarray >= 1){ //if we're not at 0 darts, take the dart count, and remove one
     
        countarray = countarray -1;
        myprint();  //call my print to update the display to relfect our new dart count
    }
    
           
     last_interrupt_time = interrupt_time;  //more debounce crap
}   
   
}


// Checks if the reset button has been pressed
void CheckForReset()
{
     readingReset = digitalRead(inPinReset);
    
      if (readingReset == HIGH && previousReset == LOW && millis() - timeReset > debounceReset)
      {
        timeReset = millis();
    
    
          
        if (readingReset == HIGH)
        tone(3, 440, 90);
        //Serial.println("reset is high");
        { 
          resetcount = resetcount + 1;
          if (resetcount == 2) // Drum
          {
            countarray = 18;  //set this to a different number of you want two presses of the reset button to change to a different size, like 1 press for 18, 2 presses for 35.  Right now have both set to 18
            resetcount = 0; 
           
          }
          else
          {
            countarray = 18;
          }
        }
        myprint();
      } // End of if statement that checks for button press.
      previousReset = readingReset;
     
   

  }


void Blink()  //you're out of ammo.  We do fun display stuff here.
{
  int temp = 0;
 
  while(countarray <= 0)
  {   
   
    if (temp == 0)
    {
      RpSerial.print("-");
      RpSerial.print("-");
      RpSerial.print("-");
      RpSerial.print("-");
     
      temp = 1;
    }
    else
    {
      RpSerial.print(1);
      RpSerial.print(1);
      RpSerial.print(1);
      RpSerial.print(1);
     
      temp = 0;
    }
   
    tone(3, 440, 90);
      
    delay(100);
    CheckForReset();
  
  
  }  // End of While
}
void Blink2()//sounds that play when you have specfici dart levels. 
{

 //tone format is tone (PIN, FREQ, DURRATION)

 if (countarray <= 13){
        if (countarray <= 3){
           tone(3, 410, 140);  //under 3
           delay(200);
        }else{
        if (countarray <= 6){
           tone(3, 390, 140);  //under 6
           delay(300);
        }else{
          if (countarray <= 10){
           tone(3,360,160);  //if under 10
           delay(500);
         
        }else{
           tone(3,300,200);  //if under 13 darts, play this
           delay(800);
        }
     }    
 }
}
}

4 comments:

  1. Came across your video on YoueTube and figured I could use this counter on my paintball gun. I've never used and Arduino board before, and I was wondering how do I get the code onto the board. I don't see any kind of connector (serial or USB). Am I missing something?. Please advise. Thanks

    Craig

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Great tutorial, your code is really nice. I made a NERF dart counter too, let me know what you think!

    ReplyDelete