Connect with us

Assorted Gubbins on toy tank

Discussion in 'General Electronics Discussion' started by Crystal Wizard, Feb 19, 2016.

Scroll to continue with content
  1. Crystal Wizard

    Crystal Wizard

    Feb 10, 2016
    While I'm waiting for my bits to arrive, I have resurrected an old idea I had.
    I play wargames (WH40k, if you know it), and was wondering about adding some electronics to one of the vehicles I am working on.

    Microswitch underneath so that when it is picked up to move, it makes "engine" sounds (unless Immobilized by enemy fire, at which point it grumbles and chokes, or says "Sorry, Dave, I can't do that")
    Button on turret so when you fire the gun it flashes LED and goes "BOOM!"
    Display to note what State it is in (in the game, it is important whether it moved Fast or Slow each Turn, and can be "Shaken", or Immobilized by incoming fire).
    A "New Turn" button to reset Variables.
    Power it with a 9V battery (replacable)

    There are a few details I need to look up, and some results will interact with each other (damage can destroy guns, or immobilize, driving speed affects how many weapons may fire), so I'll have to decide how complex to make it.

    I'm thinking of using an Arduino (or similar) to run the processing/holding variables, unless there is a simpler way.

    Just looking for quick advise so far - the basics sound feasible, but how much more complex will it be to get all the parts to interact?
    (Details of Game can be provided)
    KeithM likes this.
  2. Gryd3


    Jun 25, 2014
    I think you should take a look at a couple things first and foremost:

    Re: Playing Audio... You can do this directly, or with a shield.

    The shake portion can be done with an accelerometer, but it might be simpler to use a simple tilt switch, or a spring positioned around a metal post. (Spring moves when shaken and contacts the metal post)
    Your call here. Multiple inputs can be multi-plexed together. It sounds like fun to be honest.
  3. Crystal Wizard

    Crystal Wizard

    Feb 10, 2016
    An audio 'shield' sounds useful. I can just hold some audio files on the Arduino, and send them to the shield as needed (button push, timer etc)?

    I'll have to look into how to put all the signals together. I'm thinking most inputs will be button/switch. Either Selectors (Stop/Slow/Fast for speed this turn), or Incrementers (Tank can take a set number of Hits before being Wrecked - a row of LEDs will track this), or on/off (Press to record "Incoming fire has Stunned the crew for this round" status - switch STUN LED on).
    I'll need an End of Turn button to clear some status (like "stun"), and an over-all Power ON/OFF, making sure all Status are Default when you switch on.
  4. Gryd3


    Jun 25, 2014
    This is up to your imagination, but I will point out some things that may limit your features:

    Microcontrollers and CPU's don't actually multi-task unless they are a multi-core or multi-cpu design. They 'fake' multi-tasking by doing a little bit of each task one at a time. Progress of each task goes a little slower.
    So... your device will basically be running in a loop hundreds / thousands of times a second. Check for buttons, change outputs, play audio...
    Note, that checking for buttons and changing outputs is incredibly fast, you don't need to multi-task for this... for audio you do. A shield helps because the shield will store the audio and play the audio, it simply waits to be told 'play x' and it does it.
    If you want animated lights, you will have to multi-task or 'interrupt' the animation or additional buttons presses during the animation will be ignored until it is done. This is up to you on how responsive you want it to be.
    So, with that in mind, the best thing to do is write out a flowchart or draft of the various inputs and outputs you want. Then you can begin to work out pseudo-code and a circuit diagram before investing in components.
  5. Crystal Wizard

    Crystal Wizard

    Feb 10, 2016
    I've had a quick look at multi-tasking Arduino. Not sure I've got my head round it yet.
    A lot of it will be setting Status Lights (non-animated).
    Audio, yes, I can see how off-loading that to a shield will help.

    Most of the time I don't need it to be super-responsive.

    I'll see about scrawling a chart of some kind, see what I actually need to happen! :)
  6. Gryd3


    Jun 25, 2014
    If you are offloading audio, you don't need to multi-task... at most, you could use an 'interrupt' to immediately take care of a task if it's in the middle of doing something else. For the most part though, it will be a simple loop:

    Clear button variables.
    Set initial variables.
    Set initial outputs.

    Begin Loop
    Compare Real buttons to button variables
    If button state changed
    Change variables
    Change outputs
    End If
    Save button state in variables
    End Loop

    This is a very very rough mock-up. You can go all out and do some crazy things if you wanted to.
  7. Crystal Wizard

    Crystal Wizard

    Feb 10, 2016

    I've just written something very similar! (In my own special flavour of pseudo-code!)
    I hadn't got variables for all status (just relying on LEDs on/off), but can see how they would help.
    (Notes: HP = Hull Points (Strength of Hull. When all are gone, Tank is wrecked). There may be more than one GUN, each with separate gubbins. STUN is one of several possible effects. Games may last a few hours. )


    Set LEDs:
    All HP LED ON
    Speed STOP
    All others OFF
    Set Immobilised=FALSE

    Start Turn
    Loop {
    If PICKED UP {
    If Immobilised=TRUE, play Put Me Down! Immobile!
    Else play Engine
    } Until Put down.
    If Gun pressed, Play BOOM, flash Gun LED
    If HP pressed, switch off next HP LED
    If last HP goes OFF, flash ALL HP, play WRECK
    if Stun pressed, switch Stun LED ON
    if Immobile pressed, switch Immobilised LED ON, set Immobilised = TRUE
    on Speed-Selector, light Speed LED (STOP, SLOW, or FAST)

    If END TURN pressed {
    switch STUN LED OFF
    light speed LED STOP (switch others off).
    End Loop.


    Goto Start Turn.

    There are a few secondary bits I could put in (e.g. with multiple guns, Speed determines how many may fire. Each Gun may only fire Once per turn. I could keep track of this), but maybe that is for later.
  8. Gryd3


    Jun 25, 2014
    Right on. It may not be proper code, but it gets you going and helps clarify things.
    Micro-switches, hall sensors (magnetic sensor) and tilt switches are easy to work with... Heck, the 'switch' on the bottom could be a light sensor... Dark = placed, light = lifted.
    Lots of ways to do sensors, it only requires you to plan out how many and where you want them.

    I'll give you another tid-bit here as well which isn't really required till later, but it's good to know anyway. De-bounce your buttons!
    This program can run thousands of times a second... if you push a button and the contacts chatter, the program may thing you pressed it more than once. You can 'de-bounce' with a capacitor and resistor, or in code. Code is basically 'did it get pressed? Yes. OK, wait X amount of ms. Is it still pressed? Yes. OK, button is pressed!'
    KeithM and Crystal Wizard like this.
  9. purj


    Feb 14, 2016
    Couldn't the code-based 'de-bounce' also be:

    when ButtonIsPressed() {
    set buttonTimer = 0;​

    Meanwhile back in your loop, you could wrap all your button input code in an IF statement, like

    if (buttonTimer >= buttonWaitTime) {
    <button checking code>​
    } else {
    buttonTimer = buttonTimer + 1;​

    Then you just set buttonWaitTime to whatever you want it to be when initializing?
    Gryd3 likes this.
  10. Gryd3


    Jun 25, 2014
    Always more than one way to do things. Of course.
    There are 'blocking' methods and 'non-blocking' methods.
    I'm going to use this incorrectly, but the simple method I mentioned earlier would be an example of a blocking method... that 'wait' I used would prevent other commands from being triggered. Using a variable and/or timer like this lets you set a flag, and periodically re-check the button as you do other things until you 'know' for sure the button is pressed.
    purj likes this.
  11. Crystal Wizard

    Crystal Wizard

    Feb 10, 2016
    Thanks for the input. Much appreciated.

    As always: TIMTOWTDI :)

    Actual implementation is a little way off yet, but it seems viable, so I'll be working on bits as I go along.

    I've found a guy who did similar, but just with LEDs as headlights for the tank, no programming. Some very useful info for physically putting it all together.

    More later!
  12. Crystal Wizard

    Crystal Wizard

    Feb 10, 2016
    OK, a little more research, and I think this is what I need:

    Guns - 3, each with "Fire" button and flashing LED
    Hull Points - 4 LED (green to red). 2 buttons (+/- 1 HP)
    Speed - 3 LED (Stop, slow, fast). 1 Selector Switch
    Status - 3 LED (Stunned, Shaken, Immobilised). 1 button EACH.
    Movement switch - 1 (underneath, to detect being picked up)
    End of Turn Button - 1
    Power Switch - 1

    Gun 1, Gun 2, Gun 3 (is button pressed?)
    Hull Points - (Value 0-4)
    Speed (value 0-2)
    Stunned, Shaken, Immobile (each True/False)
    PickedUp (true/false)
    EndOfTurn (true/False)

    So, how to attach all of these to the limited Pins on an Arduino Micro?
    Oh, I also need to attach the Audio Shield (various functions will have "play effect.wav" commands)

    The only LEDs that are "combined" are the Hull Points. HP variable (0-4) will light an appropriate number of LEDs - I can do some of this in hardware, just outputting HP to one (analogue or PWM) pin?
    All the others are independent of each other, so need 1 pin each? plus 1 pin for each switch?

    I've heard the term "multiplex". And there ends my knowledge of multiplexing. :(
  13. Crystal Wizard

    Crystal Wizard

    Feb 10, 2016
    Been looking at prices.
    I can pick up an Arduino clone for about £4. Wires, LEDs, switches, another few £££. Might need a few other components. No problem.
    Audio Shield: £20+ ?!?
    Is there a cheaper way of getting an Audio Shield? Or other solution?
  14. Gryd3


    Jun 25, 2014
    Well... It sounds like a lots of things are going on which will make programming a little tricky, but still very doable depending on the Arduino you pick.
    You have listed 13 outputs, and at least 12 inputs.

    So... I will discuss the two methods commonly used with these kinds of projects.
    - Direct Pin control... each pin is responsible for a switch or LED. Very easy to program but requires lots of Digital / Analogue I/Os
    - Multiplexing... I'll sum it up and let you think of examples as you seem more than capable to do so. Think of an X and Y grid. A combination of two pins is used at a time to light an LED to check the status of a button. The limitation here is that at any instantaneous time it is impossible to light up all the LEDs, or check all the buttons, however, by rapidly cycling through the columns or rows it seems like it's all happening at the same time. (LCD clocks use this method, the slight flicker on some displays is caused by slow cycling times)
    ** There is one more method called Charliplexing which is a little more complicated and relies on 3-states instead of two like multiplexing. It's possible to control more devices this way, but the pins must be able to toggle between on/off and high impedance.

    You can of course use a mix of any of these, your 12 inputs can be handled by 7 pins. (3x4) and even if the cycle is slow, a human pressing a button will always get picked up.
    This leaves more pins for LEDs... of course, you can control the LEDs the same way so depending on how you want to do things, your microcontroller can control things with as little as 12 I/Os if you can be creative and patient enough, or as much as 25 I/Os if you want to do it simple.

    Yes and no... Remember you can use an arduino directly to do audio, so you could always buy a clone to make your own shield... Simplest result would provide audio quality similar to a Gameboy or Gameboy Color.

    ** Special Note on Arduino Clones and the FTDI 'FT232' used in most Arduino Products!
    Arduino sources official components, but some of the clones use cheap components and sometimes clones of other required components. Because of this kind of market, FTDI has release an update to their Drivers that can cause some odd behaviour with this component. I strongly suggest that you locate and roll-back your driver for this and Disable/Hide the windows update KB responsible. (They did the same thing last year that actually 'bricked' devices and got a lot of flack for it...)
    Crystal Wizard likes this.
  15. Crystal Wizard

    Crystal Wizard

    Feb 10, 2016
    OK, I think I need to sort out Multiplexing.
    I'll take that to another thread. Look for "How do I multiplex lots of switches/LEDs with my Arduino"

    Audio - looks like I need to do Direct Audio. The sounds I want are not long-lasting, and generally nothing else will be happening while they play.

    Bricking/Drivers. Eeeep! OK, research needed here.
  16. Gryd3


    Jun 25, 2014
    Well.. The drivers that 'brick' the fake FTDI chips used in clone Arduinos has been pulled... way too much flack happened what that came out.
    The newest drivers will just complain about the device and won't actually damage it allowing you to roll-back to the original driver to continue to use it. This is only an issue *if* you get an Arduino with a clone FTDI chip on it though... it's hard to tell, just figured I'd warn you if you were looking at 'clones'.
  17. Crystal Wizard

    Crystal Wizard

    Feb 10, 2016
    My bits are scheduled to arrive Wednesday, so in the meantime, I've written some example code.
    I've not done any audio, and scaled a few bits down, to fit on 15 total pins.
    I've also changed some buttons to switches.
    There is a lot of expansion for the "final" code.

    Using 5kb, it defines variables, defines pin allocations (for readability), initiates variables, and then loops:
    read pins and allocate to variables
    do some calculations and send outputs to LEDs.
    (Code available on request! I WILL be asking for advice on it, once tested, even if it works! :) )

    Once the Bits arrive, I'll put it together on a breadboard and see what happens ...
    Watch this space!

    PS: is there a virtual arduino/breadboard/pcb I can test on anywhere?
    Gryd3 likes this.
  18. Gryd3


    Jun 25, 2014
    There are virtual breadboard apps, but I am not familiar with any Arduino Emulators at the moment..
  19. Crystal Wizard

    Crystal Wizard

    Feb 10, 2016
    My first bag of bits has arrived (courtesy of ) but no Arduino yet (tomorrow, hopefully!).
    Tonight is "build some basic circuits on breadboard",
    Tomorrow night is "program the Arduino".
    Thursday is "breadboad the Tank circuit, re-write Arduino code"
    Friday is "shout and scream at it not working, throw it all in the bin and go to the pub". :)
    Saturday: Fish it all out of the bin and try again.

    Don't touch that dial!
    Gryd3 likes this.
  20. Crystal Wizard

    Crystal Wizard

    Feb 10, 2016
    Hmmm ... "minor" issue I've spotted:
    Driving multiple LEDs via the I/O pins. Arduino does not like supplying over 200mA. At 20mA per LED, I'm limited to 10 LEDs.

    Would some kind of transistor, using low-current from the I/O pins to switch on LEDs be a solution?

    Or do I try to keep each LED below 20mA, maybe half that, and accept they won't be so bright?

    Or is there a different solution?
    (There is always a different solution! Is there a better solution? :) )
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day