Code isn't working, no errors

Hey, I'm currently making a laser tag gun, I just switched over from delays to millis and I'm having some problems with my code. What is does is, if the button is pressed an LED is activated, this has an interval of 1 second, it can do this 6 times before needing to 'reload', then it pauses for 3 seconds. Lastly if the second button is hit you go 'out' and it causes a 10 second delay. Now at the moment it doesn't do anything

const int LED = 13;
const int BUTTON = 7;
const int BULL = 2;
const int B2 = 3;
const int B3 = 4;
const int B4 = 5;
const int B5 = 6;
const int B6 = 9;
const int OUT = 8;
const int HIT = 10;
int bullets = 6;
int val = 0;  //set pins and variables
int hit = LOW;
int reloading = 0;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
const long interval = 1000;
const long interval2 = 1250; 
const long interval3 = 3000;     
const long int1 = 1; 

void setup() 
{
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);   //set pinmodes
  pinMode(BULL, OUTPUT);
  pinMode(B2, OUTPUT);
  pinMode(B3, OUTPUT);
  pinMode(B4, OUTPUT);
  pinMode(B5, OUTPUT);
  pinMode(B6, OUTPUT);
  pinMode(OUT, OUTPUT);
  pinMode(HIT, INPUT);    
}

void loop()
{
  unsigned long currentMillis = millis();
  val = digitalRead(BUTTON);
  hit = digitalRead(HIT);
  if((currentMillis - previousMillis3 > int1) && (hit == HIGH))
  {
  delay(10000);
  }
  else
    if((bullets == 0) && (currentMillis - previousMillis2 > interval3)) 
    {
      reloading = reloading + 1;
        previousMillis2 = currentMillis;
        bullets = bullets + 6;
        reloading = reloading - 1;
      }
      else
  if(((currentMillis - previousMillis > interval) && (val == HIGH) && (reloading == 0)))
    {
    previousMillis = currentMillis;   
    digitalWrite(LED, HIGH);
    bullets = bullets - 1;
    }
    if(currentMillis - previousMillis > interval2)
    {
    previousMillis = currentMillis;
    digitalWrite(LED, LOW);
    }
    }
[CODE]

In your ifs

hit = HIGH Change to hit == HIGH

there are more to do also.

LarryD: In your ifs

hit = HIGH Change to hit == HIGH

there are more to do also.

Ok, I've fixed that, thanks. What else should I fix?

You should not make changes to your original post, make your changes in a current post.

What is the sketch not doing for you?

  • Please put some comments in your code.
  • Use "CTRL T" to format your code.

I am not sure about the logic flow but, make sure your "else" and "else if" are indeed what you want.

Why:

reloading = reloading + 1; //<<<<<< You add 1 here
previousMillis2 = currentMillis;
bullets = bullets + 6;
reloading = reloading - 1; //<<<<<< You then subtract 1

These should be const unsigned long const long interval = 1000; const long interval2 = 1250; const long interval3 = 3000; const long int1 = 1;

1 millisecond, are you sure?

const long int1 = 1; . . . if(hit == HIGH && (currentMillis - previousMillis3 > int1))

How are your switches wired?

You may need: pinMode(BUTTON, INPUT_PULLUP); pinMode(HIT, INPUT_PULLUP);

LarryD: You should not make changes to your original post, make your changes in a current post.

What is the sketch not doing for you?

  • Please put some comments in your code.
  • Use "CTRL T" to format your code.

I am not sure about the logic flow but, make sure your "else" and "else if" are indeed what you want.

It doesn't do anything

LarryD:
Why:

reloading = reloading + 1; //<<<<<< You add 1 here
previousMillis2 = currentMillis;
bullets = bullets + 6;
reloading = reloading - 1; //<<<<<< You then subtract 1

Because in some of my if statements I say reloading must be 0, when I finish the code this will be what keeps some functions from happening whilst it’s reloading

updated code

const int LED = 13;
const int BUTTON = 7;
const int BULL = 2;
const int B2 = 3;
const int B3 = 4;
const int B4 = 5;
const int B5 = 6;
const int B6 = 9;
const int OUT = 8;
const int HIT = 10;
int bullets = 6;
int val = 0;  //set pins and variables
int hit = LOW;
int reloading = 0;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long interval = 1000;
unsigned long interval2 = 1250;
unsigned long interval3 = 3000;
unsigned long int1 = 10;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);   //set pinmodes
  pinMode(BULL, OUTPUT);
  pinMode(B2, OUTPUT);
  pinMode(B3, OUTPUT);
  pinMode(B4, OUTPUT);
  pinMode(B5, OUTPUT);
  pinMode(B6, OUTPUT);
  pinMode(OUT, OUTPUT);
  pinMode(HIT, INPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  val = digitalRead(BUTTON);
  hit = digitalRead(HIT);
  if ((currentMillis - previousMillis3 > int1) && (hit == HIGH))
  {
    delay(10000);
  }
  else if ((bullets == 0) && (currentMillis - previousMillis2 > interval3))
  {
    reloading = reloading + 1;
    previousMillis2 = currentMillis;
    bullets = bullets + 6;
    reloading = reloading - 1;
  }
  else if (((currentMillis - previousMillis > interval) && (val == HIGH) && (reloading == 0)))
  {
    previousMillis = currentMillis;
    digitalWrite(LED, HIGH);
    bullets = bullets - 1;
  }
  if (currentMillis - previousMillis > interval2)
  {
    previousMillis = currentMillis;
    digitalWrite(LED, LOW);
  }
}
[CODE]

Angulo: Because in some of my if statements I say reloading must be 0, when I finish the code this will be what keeps some functions from happening whilst it's reloading

No that explanation makes no sense at all. Think again. Adding one and then just two statements later subtracting one is just very silly. Removing those two lines will not affect how the code runs.

How are your switches wired?

You may need: pinMode(BUTTON, INPUT_PULLUP); . . . pinMode(HIT, INPUT_PULLUP);

What is it not doing?

Add comments to your sketch .

Grumpy_Mike: No that explanation makes no sense at all. Think again. Adding one and then just two statements later subtracting one is just very silly. Removing those two lines will not affect how the code runs.

I'm going to be making it so there is a 3 second pause in between the two

I found a problem whilst I was adding comments, will re upload soon

Code with comments

const int LED = 13;
const int BUTTON = 7;
const int BULL = 2;
const int B2 = 3;
const int B3 = 4;
const int B4 = 5;
const int B5 = 6;
const int B6 = 9;
const int OUT = 8;
const int HIT = 10;
int bullets = 6;
int val = 0;  //set pins and variables
int hit = LOW;
int reloading = 0;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long interval = 1000;
unsigned long interval2 = 1250;
unsigned long interval3 = 3000;
unsigned long int1 = 10;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);   //set pinmodes
  pinMode(BULL, OUTPUT);
  pinMode(B2, OUTPUT);
  pinMode(B3, OUTPUT);
  pinMode(B4, OUTPUT);
  pinMode(B5, OUTPUT);
  pinMode(B6, OUTPUT);
  pinMode(OUT, OUTPUT);
  pinMode(HIT, INPUT);
}

void loop()
{
  unsigned long currentMillis = millis(); //set currentMillis to millis
  val = digitalRead(BUTTON); //set val to BUTTON
  hit = digitalRead(HIT); //set hit to HIT
  if ((currentMillis - previousMillis3 > int1) && (hit == HIGH)) // checkig if button is being pressed and whether 10ms has passed
    delay(10000); //delay the program for 10 seconds, this is how you go out
  }
  else if (bullets == 0) //otherwise check if there is no bullets
  {
    reloading = reloading + 1; //add 1 to reloading
       //need to add a 3 second delay here
    
    bullets = bullets + 6; //add 6 to bullets
    reloading = reloading - 1; //take 1 from reloading
  }
  else if (((currentMillis - previousMillis > interval) && (val == HIGH) && (reloading == 0))) //otherwise check if you're not reloading, button is pushed and 1 second has passed
  {
    previousMillis = currentMillis; //make currentMillis previousMillis
    digitalWrite(LED, HIGH); //turn on the LED
    bullets = bullets - 1; //take away 1 bullet
  }
  if (currentMillis - previousMillis > interval2) //wait 250 milliseconds (doesn't work)
  {
    previousMillis = currentMillis; //make currentMillis previousMillis
    digitalWrite(LED, LOW); //turn off the LED
  }
}
[CODE]

Three seconds delay between the increment and decrement is still not going to change anything.

Anyway glad you have it working. Comments are not just for others they are for you. Especially when you look at this code again in three months.

Grumpy_Mike:
Three seconds delay between the increment and decrement is still not going to change anything.

Anyway glad you have it working. Comments are not just for others they are for you. Especially when you look at this code again in three months.

It’s still not quite working, I don’t understand how to use millis to trigger a delay, so when I click the button it waits three seconds and then activates something

 if ((currentMillis - previousMillis3 > int1) && (hit == HIGH)) // checkig if button is being pressed and whether 10ms has passed
  delay(10000); //delay the program for 10 seconds, this is how you go out

}

I see a right bracket.....

Ok, now I need to find out how to have a a 3 second delay (using millis) that is activated when a variable hits 0

At the point X=0 you make millis3second = millis()

then you can:

if(X == 0 && millis() - millis3second < 3000)
{
return; // and/or some other stuff you need
}