SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By danjr1
#193001
Hi all,

I'm new to working with programming and arduino having been involved with fighting robots for a long time. For my first project, I'm looking to make a simple IR blaster game based on the numerous tutorials you find online (double convex lens, high powered IR LED using some sort of transistor with matching receiver, etc). This wouldn't be laser tag - purely single shot from the rifle hitting a target which will either output noise, movements or both. The idea being that if I can get this basic system working, I can then expand it with further game functions.

Leading me onto my main question about the rifle code, all I want to do is when the trigger is depressed (and theoretically kept depressed indefinitely) is to illuminate the IR LED for given amount of time at a given frequency then deactivate and stay off until the trigger has been released. Essentially, a single shot process. I've written the code below as a 'starter for ten' based off the button example so I'd appreciate any comments :)

Cheers

Dan


// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
delay(1000); // wait for a second
digitalWrite(ledPin, LOW);
if (buttonState == HIGH);
digitalWrite(ledPin, LOW);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}