GOT IT!!!!
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// this constant won't change:
const int buttonPin = 7; // the pin that the pushbutton is attached to
const int switchPin2 = 7;
const int switchPin = 13;
const int motorPin = 9;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int switchState = 0;
int previousSwitchState = 0;
int reply;
int buttons[2];
int buttons0 = 2;
int rounds;
int ammoLeft;
int shotsFired=0;
int switchStatet = 0;
int gunState;
int previousKeyVal;
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize serial communication:
Serial.begin(9600);
// initilize Trigger
pinMode(motorPin, OUTPUT);
pinMode(switchPin, INPUT);
//initialize LCD
lcd.begin(12, 2);
pinMode(switchPin2, INPUT);
lcd.print("VENMORT TECH");
lcd.setCursor(0, 1);
lcd.print("Onboard Computer");
delay(500);
lcd.clear();
lcd.print("Stampede ECS");
lcd.setCursor(0, 1);
lcd.print("Ammo Counter");
}
void loop() {
ammoLeft = (rounds - shotsFired);
// ClipModule
int keyVal = analogRead(A0);
//Serial.println(keyVal);
if (keyVal < 800) {
// Display
// Serial.println("Reloading");
rounds = 0;
gunState = 6;
shotsFired=0;
lcd.clear();
lcd.print("RELOADNG");
delay(500);
lcd.clear();
delay(500);
}
else if (keyVal>= 970 && keyVal <=990) {
//Serial.println(keyVal);
// Serial.println(rounds);
rounds = 36;
lcd.clear();
lcd.print("Ammo Left");
lcd.setCursor(0,1);
lcd.print(ammoLeft);
}
else if(keyVal >= 1000 && keyVal <=1005){
//Serial.println(keyVal);
// Serial.println(rounds);
rounds = 18;
}
else if(keyVal > 1005){
//Serial.println(keyVal);
// Serial.println(rounds);
rounds = 6;
}
// end ClipMode
// counter
if (ammoLeft >= 0) {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
shotsFired++;
lcd.clear();
lcd.print(gunState);
lcd.setCursor(0,1);
lcd.print(ammoLeft);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
//end ClipModule
switchState = digitalRead(switchPin);
if ((switchState == LOW) && (ammoLeft > 5)) {
digitalWrite(motorPin, HIGH);
gunState = 2;
}
else if ((switchState == LOW) && (ammoLeft < 4)){
digitalWrite(motorPin, HIGH);
delay (250);
digitalWrite(motorPin, LOW);
delay (250);
gunState = 5;
}
else {
digitalWrite(motorPin, LOW);
gunState=1;
}
Serial.println("gunState");
Serial.println(gunState);
Serial.println("ammoLeft");
Serial.println(ammoLeft);
Serial.println("Rounds");
Serial.println(rounds);
}
}