if not working

Hi guys.

Why is this not working? The code works only when (ammoLeft >4). The rest of the code is ok, i'm just showing the part that is not working.

switchState = digitalRead(switchPin);

if (switchState == LOW){
if (ammoLeft > 5) {
digitalWrite(motorPin, HIGH);
gunState = 2;
}
if (ammoLeft < 4) {
digitalWrite(motorPin, HIGH);
delay (250);
digitalWrite(motorPin, LOW);
delay (250);
gunState = 5;
}

if (ammoLeft < 0){
digitalWrite(motorPin,LOW);
gunState = 0;
}

else {
digitalWrite(motorPin, LOW);
gunState=1;
}
}

Thanks

batty

Tell us a bit more of the what you are trying to do here?

it's the controler for a nerf gun.

If there less then 4 darts left, i want the gun to start firing in burst mode. if not, full fire.

There's gonna be more functions than that, but I figured if i can get the first 2 too work together, i will be able to figure out the rest.

batty

Well some things I can recommend trying are using the Serial Monitor and placing a Serial.print("SOMETHING"); and see if it prints. If not try to make it print like you want and work from there.

By looking at it. If your ammo gets to 5, 4, and 0 it will not fire. Try >=, <=, and <= in each if statement instead.

I already have a bunch of serial prints, and i know the gunState stays at 5. (LowAmmo, fire with delays) It's something in the way of writing the IF's.

Here's the full code as it is right now. Everything else works just fine.:

#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){
if (ammoLeft > 5) {
digitalWrite(motorPin, HIGH);
gunState = 2;
}
if (ammoLeft < 4) {
digitalWrite(motorPin, HIGH);
delay (250);
digitalWrite(motorPin, LOW);
delay (250);
gunState = 5;
}

if (ammoLeft < 0){
digitalWrite(motorPin,LOW);
gunState = 0;
}

else {
digitalWrite(motorPin, LOW);
gunState=1;
}
}

}

Serial.println("gunState");
Serial.println(gunState);
Serial.println("ammoLeft");
Serial.println(ammoLeft);

}

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);

}
}

Hi, good to see you had success with your project, now can you repost your sketch after reading this link please.

http://forum.arduino.cc/index.php?topic=149014.0

Please post using # code tags.

Thank you .Tom..... :slight_smile: