Hey, I'm fairly new to Arduino and for my first project I'm making a laser tag gun, at the moment I'm laying the frame work. what I need help with is a button that can be pressed at any point in time, thanks.
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;
unsigned long previousMillis = 0;
const long interval = 1000;
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()
{
val = digitalRead(BUTTON); //if the button is pressed the variable will become HIGH
if (val == HIGH)
{
if(bullets == 0)
{
digitalWrite(OUT, HIGH); //if there is no bullets you will not be able to shoot for three seconds
delay(3000);
digitalWrite(OUT, LOW);
bullets = bullets + 6;
}
else
{
digitalWrite(LED, HIGH); //if the variable is HIGH the LED will turn on for 1/4 of a second and take away one bullet
delay(250);
digitalWrite(LED, LOW);
bullets = bullets - 1;
delay(1000);
}
if(bullets == 6)
{
digitalWrite(BULL, HIGH); //if there is six bullets six lights will be on
digitalWrite(B2, HIGH);
digitalWrite(B3, HIGH);
digitalWrite(B4, HIGH);
digitalWrite(B5, HIGH);
digitalWrite(B6, HIGH);
}
else
if(bullets == 5)
{
digitalWrite(BULL, LOW); //if there is five bullets five bullets will be on
digitalWrite(B2, HIGH);
digitalWrite(B3, HIGH);
digitalWrite(B4, HIGH);
digitalWrite(B5, HIGH);
digitalWrite(B6, HIGH);
}
else
if(bullets == 4)
{
digitalWrite(BULL, LOW); //if there is four bullets four lights will be on
digitalWrite(B2, LOW);
digitalWrite(B3, HIGH);
digitalWrite(B4, HIGH);
digitalWrite(B5, HIGH);
digitalWrite(B6, HIGH);
}
else
if(bullets == 3)
{
digitalWrite(BULL, LOW); //if there is three bullets three lights will be on
digitalWrite(B2, LOW);
digitalWrite(B3, LOW);
digitalWrite(B4, HIGH);
digitalWrite(B5, HIGH);
digitalWrite(B6, HIGH);
}
else
if(bullets == 2)
{
digitalWrite(BULL, LOW); //if there is two bullets two lights will be on
digitalWrite(B2, LOW);
digitalWrite(B3, LOW);
digitalWrite(B4, LOW);
digitalWrite(B5, HIGH);
digitalWrite(B6, HIGH);
}
else
if(bullets == 1)
{
digitalWrite(BULL, LOW); //if there is one bullet one light is on
digitalWrite(B2, LOW);
digitalWrite(B3, LOW);
digitalWrite(B4, LOW);
digitalWrite(B5, LOW);
digitalWrite(B6, HIGH);
}
else
if(bullets == 0)
{
digitalWrite(BULL, LOW); //if there is no bullets all lights are off
digitalWrite(B2, LOW);
digitalWrite(B3, LOW);
digitalWrite(B4, LOW);
digitalWrite(B5, LOW);
digitalWrite(B6, LOW);
}
unsigned long currentMillis =millis();
if(currentMillis - previousMillis >= interval);
{
previousMillis = currentMillis;
hit = digitalRead(HIT); //if the button is pressed the variable will become HIGH
if (hit == HIGH);
{
delay(10000);
}
}
}
}
[CODE]
[/code]
othersLASERtag.ino (2.96 KB)