Friday, December 12, 2014

Last Post!

So, here we are, the day of the presentation and the launcher is complete. It's still got a few kinks but overall, it works.

Rather than 3D print the original part, we've opted to go the simpler route and use a PVC Wye joint to load the ping pong balls in from the top. A servo motor is mounted to the tube to only allow one ball in at a time. From there, a solenoid valve opens to release air pressure built up in a 2L bottle.

We got a little bored and coded in a shot counter, too. We're going to try to work out a few kinks before we head to the expo. Hopefully we'll have it completely functional when we get there!

#include <Servo.h>

Servo Bolt;
const int buttonPin = 2;  
const int valvePin = 4;
const int latchPin = 8;
const int clockPin = 12;
const int dataPin = 9;

int fireState = LOW;         
int buttonState;            
int previousButtonState = LOW; 
int ammo=9;

unsigned long lastDebounceTime = 0;  
unsigned long debounceDelay = 50;

byte digits[11]= {0x77, 0x44, 0x6B, 0x6E, 0x5C, 0x3E, 0x3F, 0x64, 0xFF, 0x7E, 0x00};

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  
  Bolt.attach(11);
  pinMode(buttonPin, INPUT);
  pinMode(valvePin, OUTPUT);
  
  digitalWrite(valvePin,LOW);

  reload();  
}

void loop() { 
  guage(ammo);
  
  int x = digitalRead(buttonPin); 
  
  if (x != previousButtonState) {
    lastDebounceTime = millis();
  } 
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (x != buttonState) {
      buttonState = x;
      if (buttonState == HIGH) {
        fireState = 1;
      }
    }
  }   
  if (fireState == 1){
    if (ammo == 0){
     for (int j=0; j<5; j++){
         guage (0);
         delay (250);
         guage (10);
         delay (250);
         fireState = 0;
     }
  }
  else {
      digitalWrite (valvePin, HIGH);
      delay (300);
      digitalWrite(valvePin,LOW);
      delay (1500);
      reload();
      delay (500);
      ammo--;
      fireState = 0;
      }
    }
  previousButtonState = x;
  }

void reload (){
  Bolt.write(0);
  delay(55);
  Bolt.write(20); 
}

void guage(int y){
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, digits[y] );
  digitalWrite(latchPin, HIGH); 
}

It's long. But so far, so good!

No comments:

Post a Comment