Close

Current Code

A project log for Coil Gun/Gaus Cannon

A device that uses electricity to propel a projectile at a significant speed.

jeffballard86jeff.ballard.86 04/29/2014 at 16:330 Comments

Now if you are looking at this remember I am a code noob, constructive criticism is highly encouraged.

Code:

#include <LiquidCrystal.h>

#include <EEPROM.h>

//variables

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

float time = 0;

unsigned long StartTime = 0;

unsigned long StopTime = 0;

float timer;

float gateD = 3.6; //distance between gates in inches

float gate = 0;

float fps = 0;

float seconds = 0;

int FireDelay; //second SCR fire Delay

int FireDelaySave = 0;

float Debounce = 50; //button debounce time

//pin states

int UpButtonState;

int DownButtonState;

int FireState;

int SaveButtonState;

int FireButtonState;

//pin labeling

const int Up = 30;

const int Down = 32;

const int Save = 36;

const int FireOne = 9;

const int FireTwo = 8;

const int FireButton = 31;

const int SecondCoilGate = 38;

const int SpeedTrapOne = 48;

const int SpeedTrapTwo = 50;

//EEPROM saving variables

int FireDelayAddress = 1;

int FireDelayMultiplierAddress = 2;

int FireDelayMultiplier;

void setup()

{

Serial.begin(9600);

lcd.begin(16, 2);

pinMode(FireTwo, OUTPUT); //Second SCR fire mosfet

pinMode(FireOne, OUTPUT); //First SCR fire mosfet

//digitalWrite(FireOne, HIGH);

//digitalWrite(FireTwo, LOW);

FireDelay = EEPROM.read(FireDelayAddress); //read FireDelay from eeprom

FireDelayMultiplier = EEPROM.read(FireDelayMultiplierAddress); //read multiplier from eeprom

if (FireDelayMultiplier != 1 | FireDelayMultiplier != 2 | FireDelayMultiplier != 3){ //1st boot up sequence if mulitiplier is not equal to 1,2, or 3, set to 1 and set default fire delay

FireDelayMultiplier = 1;

FireDelay = 500;}

if (FireDelayMultiplier == 1){

FireDelay = FireDelay;}

if (FireDelayMultiplier == 2){

FireDelay = FireDelay * 10;}

if (FireDelayMultiplier == 3){

FireDelay = ((FireDelay * 2)*10);}

}

void loop()

{

digitalWrite(FireOne, LOW);

digitalWrite(FireTwo, LOW);

if (digitalRead(Up) != UpButtonState) //Up Button Code

{

UpButtonState = digitalRead(Up);

if (UpButtonState == HIGH)

{

FireDelay = FireDelay + 20;

lcd.clear();

lcd.setCursor(0,1);

lcd.print("Fire Delay");

lcd.setCursor(11,1);

lcd.print(FireDelay);

lcd.setCursor(14,1);

lcd.print("uS");

delay(Debounce);

}

}

if (digitalRead(Down) != DownButtonState) //Down Button Code

{

DownButtonState = digitalRead(Down);

if (DownButtonState == HIGH)

{

FireDelay = FireDelay - 20;

if (FireDelay < 0) {

FireDelay = 0;

}

lcd.clear();

lcd.setCursor(0,1);

lcd.print("Fire Delay");

lcd.setCursor(11,1);

lcd.print(FireDelay);

lcd.setCursor(14,1);

lcd.print("uS");

delay(Debounce);

}

}

if (digitalRead(Save) != SaveButtonState) //save Button Code

{

SaveButtonState = digitalRead(Save);

if (SaveButtonState == HIGH)

{

if (FireDelay < 260) {

FireDelayMultiplier = 1;

FireDelaySave = FireDelay;

}

if (FireDelay <= 2500 && FireDelay >= 260) {

FireDelayMultiplier = 2;

FireDelaySave = FireDelay / 10;

}

if (FireDelay <= 5000 && FireDelay > 2500) {

FireDelayMultiplier = 3;

FireDelaySave = ((FireDelay/10)/2);

}

EEPROM.write(FireDelayAddress, FireDelaySave);

EEPROM.write(FireDelayMultiplierAddress, FireDelayMultiplier);

delay(100);

lcd.clear();

lcd.setCursor(0,1);

lcd.print("Saved");

delay(1500);

lcd.clear();

lcd.setCursor(0,1);

lcd.print("Fire Delay");

lcd.setCursor(11,1);

lcd.print(FireDelay);

lcd.setCursor(14,1);

lcd.print("uS");

}

}

if (digitalRead(FireButton) != FireButtonState) //Fire Sequence

{

FireButtonState = digitalRead(FireButton);

if (FireButtonState == HIGH)

{

//delay(Debounce);

digitalWrite(FireOne, HIGH); //fire first SCR

while (digitalRead(SecondCoilGate) == LOW) {

} //wait until projectile trips gate

StartTime = micros(); //FPS measurment start

digitalWrite(FireOne, LOW); //turn first SCR off

delayMicroseconds(FireDelay); //delay until projectile is in the perfect spot

digitalWrite(FireTwo, HIGH); //Fire Second SCR

delay(1); //delay to give secoond scr to start up

digitalWrite(FireTwo, LOW); //turn second SCR off

while(digitalRead(SpeedTrapTwo) == LOW) {

} //First sensor was tripped, now wait for second sensor to trip

StopTime = micros(); //second part of FPS measurment

time = StopTime - StartTime; //time elapsed between gates

seconds = time / 1000000; //find feet per second, math

gate = gateD / 12;

fps = gate / seconds;

//Serial.println(time);

//Serial.println(seconds);

//Serial.println(fps);

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Feet Per Second");

lcd.setCursor(0,1);

lcd.print(fps);

}

}

//delay(1000);

}

Discussions