Announcement

Collapse
No announcement yet.

Arduino programming - 'single shot' code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Arduino programming - 'single shot' code

    Evening all,

    So I'm attempting to make an IR based 'tin can alley' rifle game and therefore trying to learn arduino programming at the same time. I'm pretty confident that I can get the IR LED to flash when a button is pressed but what I can't get my head round is how I would write the code so that if the trigger button is continually depressed for any amount of time, it would only ever flash once and only allows the system to loop round again once the trigger has been released? I wrote the code below as a total 'starter for ten' off the button example but I'm highly doubtful it will work. Any help is greatly appreciated!

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

  • #2
    Never done any programming with arduinos but off the top of my head I'll break down how I would do it.

    I would create a number of seperate loops for each scenario. So to try and break it down in some kind of clear fashion you would have:

    Loop 1
    Check if button is pressed.
    If button is pressed move to Loop 2
    Else return to Loop 1

    Loop 2
    Switch on LED
    Wait for 1 second
    Switch off LED
    Move to Loop 3

    Loop 3
    Check if button is held down
    If button is held down return to Loop 3
    Else move to Loop 1

    So to start with it checks for an input from the button, if it doesn't receive it then it returns to the start of Loop 1 and continues checking. Once it receives that input it moves to Loop 2. Loop 2 flashes the LED once and then moves to Loop 3. Loop 3 checks for an input from the button, if it receives it (indicating that the button is still being held down) it returns to the start of Loop 3 and continues to check for that input. Once it no longer detects that input it moves back to Loop 1 and the cycle starts again.

    Comment


    • #3
      Hi,

      the easiest thing to do is to just concern yourself with when the trigger is pressed in.

      So at the end of each iteration of the loop you save the value of the trigger.

      If at the start of the loop, the trigger is now pressed in and was previously not pressed in then you know that you need to flash the LED.

      If they hold the trigger in then the last value will be the same as the current value so it will not flash the LED


      // variables will change:
      int buttonState = 0; // variable for reading the pushbutton status
      int buttonStateLast = 0; // variable for recording last button state

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

      void loop()
      {

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

      // Determine when button pressed . . .
      if (buttonState == HIGH and buttonState <> buttonStateLast) {

      // turn LED on:
      digitalWrite(ledPin, HIGH);
      delay(1000); // wait for a second
      digitalWrite(ledPin, LOW);

      }

      buttonStateLast = buttonState;

      }

      Comment


      • #4
        FYI The website https://www.mysensors.org/ is a good resource for sample Arduino code relating to working with various sensors.

        Comment


        • #5
          Thanks dude! I'll give it a go!

          Comment


          • #6
            Are you essentially trying to build a laser-tag style tin-can alley....

            If so - it's harder than it looks. One of the several companies I work for does laser tag events for kids parties etc - and you'd never believe how hard it was to get a reliable set of laser tag kit that works outdoors!

            If not - please ignore me!

            Comment


            • #7
              yeah, pretty much! What issues do they have? What I'm planning is purely 'tin can alley' - no scoring or anything like that. Targets will make a noise/ light up/ fall down. Thanks to Alex, I have a code which will run the gun but I'm going to add debounce, IR frequency details, sound and maybe some code to run a recoil solenoid. Although, this is all dependant on breadboarding the lot up first and checking it works.

              Comment


              • #8
                So......Most of the laser tag kit you see out and about using a PWM-based signal at 38, 40, or 56kHz (choose your flavour wisely - it *will* affect availability of parts). 38kHz is most common for UK sourcing.

                If you are just using a IR LED and not transmitting any form of modulating signal (just on or off), you *will* get stray signals from the Sun, your nearest fluorescent tube, etc, etc. I've been there and tried this sort of stuff before.

                I do this sort of stuff (partly) for a living: including looking after a fleet of 24 laser tag guns and sensor systems, and fixing laser tag kit for other sites as well.

                Happy to help if you need me to - just ask.

                Comment


                • #9
                  Thanks for the offer. I'm planning on using 38khz as all the tutorials I've found suggest that as the LEDs and receivers are easily obtainable.

                  Comment


                  • #10
                    As a note - generally, the LEDs don't have a 38kHz modulator.

                    The "Industry standard" is to use TSAL6100 LEDs with the microcontroller generating the 38kHz PWM signal.

                    Edit: Note that you'll probably want a MOSFET to drive the LED as well - as the currents involved are quite high.
                    Last edited by cmalton; 23 January 2017, 07:13. Reason: Added MOSFET note.

                    Comment


                    • #11
                      Sorry, wasn't clear in my previous post. I'd get the arduino to control the 38Khz output.

                      I'll have to do a bit of research on MOSFETS - I know what they are but building circuits and choosing the correct components is all new to me really!

                      Comment


                      • #12
                        As I say, if you need some some help - please just ask. I know a few people who've had a go at this sort of thing before and failed due to making silly errors.

                        I encourage reading http://lasertagparts.com/mtcore.htm as this may help you out with a lot of answers to questions as well.

                        For the FETs, the IRFD110 seems to have become a bit of a standard as well - readily available in the UK from Farnell.

                        Comment


                        • #13
                          Thanks mate. Already got those pages added to my favourites! Their page on optics has helped formed most of my shopping list.

                          Comment


                          • #14
                            Originally posted by DoubleTrouble View Post

                            // Determine when button pressed . . .
                            if (buttonState == HIGH and buttonState <> buttonStateLast) {
                            For some reason, when i verify the code it doesn't like the '<>' bit saying:

                            exit status 1
                            expected primary-expression before '>' token

                            I'm guessing the '>' is required here as you would need the button state to be larger than the last state to indicate the trigger had previously been released?
                            Last edited by Danjr1; 25 January 2017, 19:22.

                            Comment


                            • #15
                              Not equals is != on the Arduino and not <>

                              should have said said I wrote the code off the top of my head

                              Comment

                              Working...
                              X