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]
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
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.
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
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]
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
}