Maker Pro
Maker Pro

Assorted Gubbins on toy tank

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
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? :) )
There was mention of a couple additional ICs you could use in your multi-plexing thread.
I think that's an ideal solution. Otherwise you can put a larger resistor in series with the LEDs to limit the current, or use a transistor for each LED. (I think the transistor method is a little clunky though, but is more than good enough for the direct Pin > LED approach during testing phases.)
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
There was mention of a couple additional ICs you could use in your multi-plexing thread.
I think that's an ideal solution. Otherwise you can put a larger resistor in series with the LEDs to limit the current, or use a transistor for each LED. (I think the transistor method is a little clunky though, but is more than good enough for the direct Pin > LED approach during testing phases.)
Aha! Yes, I am thinking of using those ICs! And they will draw power NOT from the Arduino? That would work just fine! :)
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Aha! Yes, I am thinking of using those ICs! And they will draw power NOT from the Arduino? That would work just fine! :)
They will draw minute current from the Arduino on it's signalling 'input' pins.
The bulk of current draw will be on the Vcc pin which should be connected to the same 5V supply as the Arduino.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
This does bring up the question of how I will be powering everything ... I can't be trailing a lead across the battle-table!
I was thinking of a 9V battery, but presume I will need to drop the voltage.
Or I can use a few AA or AAA batteries.
They need to be replaceable, and not take up too much space.
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
This does bring up the question of how I will be powering everything ... I can't be trailing a lead across the battle-table!
I was thinking of a 9V battery, but presume I will need to drop the voltage.
Or I can use a few AA or AAA batteries.
They need to be replaceable, and not take up too much space.
The common method that seems to be taking a lot of modders by storm lately is a little lithium based battery. The downside is that you would need to plug in your tank to charge it.
Otherwise, grab some AA or AAA. You are right with the 9V, it will need to be regulated, and dropping it from 9V to 5V with a linear regulator is not ideal as it will waste power. A switch-mode DC-DC buck converter will be much more efficient but costs more.
During the development phase, it will certainly be worth experimenting with 'sleep' cycles and interrupts. The more time your microcontroller is doing absolutely nothing the better. Being in a constant loop of checking buttons will cause increased power draw... so by sleeping and only checking every couple hundred ms you can save a lot of juice. An 'interrupt' will allow a button to forcefully wake up the tank without waiting for it's little power-nap to be over and can be used creatively to extend the length of it's sleep cycles and still be responsive when you hit a button.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Plugging in between fights should be OK. Or make the battery removable?

If I'm using rechargables, I'm not too bothered about efficiency, so long as there is not too much heat, and it lasts the fight.

I can look at sleep/interrupt. More things to learn ...

For now, a linear regulator (just built one on breadboard!) will have to do.

Arduino was supposed to arrive today. No show! :(
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Plugging in between fights should be OK. Or make the battery removable?

If I'm using rechargables, I'm not too bothered about efficiency, so long as there is not too much heat, and it lasts the fight.

I can look at sleep/interrupt. More things to learn ...

For now, a linear regulator (just built one on breadboard!) will have to do.

Arduino was supposed to arrive today. No show! :(
With a linear regulator, a 9V source is stepped down to 5V, and when you draw 150mA, you end up drawing 150mA from the battery...
Well, with a switching regulator, if you use a 9V source and step it down to 5V, then when you happen to want 150mA, the regulator will pull 85-90mA from the battery. You can run almost 50% longer with a switch mode regulator than a linear one!
So... if you are stepping down a much higher voltage, it pays to go switching regulator. If you are using 6V and step it down to 5V, then it's not really worth it in most cases.

Well, you have lots to read and look up then while your Arduino travels to you.
How long is a fight?
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Not looked at switching regulators yet. I built the linear 9v-5v as a simple "can I remember breadboards" project as I have easy access to 9v "square" batteries.

Yeah, lots of reading still! Prefer to intersperse reading with doing!

Each fight is usually 2-3 hours*. We normally only have one per evening, but Tournaments last all day, or even Fri-Sun, with several fights per day. (Would definitely want to have better power for a Tournament)

*I did have one fight that took longer to set up than to play! My main strike-force tried to teleport behind enemy lines, messed up and were lost! Then the enemy opened fire on my remaining forces and got lucky, took out all my Dreadnoughts, and left me down to about 1/3 of my army within 10 minutes, with his untouched! :(
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Not looked at switching regulators yet. I built the linear 9v-5v as a simple "can I remember breadboards" project as I have easy access to 9v "square" batteries.

Yeah, lots of reading still! Prefer to intersperse reading with doing!

Each fight is usually 2-3 hours*. We normally only have one per evening, but Tournaments last all day, or even Fri-Sun, with several fights per day. (Would definitely want to have better power for a Tournament)

*I did have one fight that took longer to set up than to play! My main strike-force tried to teleport behind enemy lines, messed up and were lost! Then the enemy opened fire on my remaining forces and got lucky, took out all my Dreadnoughts, and left me down to about 1/3 of my army within 10 minutes, with his untouched! :(
haha, well. Live and learn.
Anyway, regarding power battery life. The best thing to do, is to sum up the current use of all the LEDs you plan to use. They will draw a fair bit. Tack on a little extra for pull-up or pull-down resistors, and some for the Microcontroller itself. We can get more exact later, but this can give you some immediate expectations.

A 9V battery typically has 400-500mAh capacity.
If you are using 20mA LEDs, then you can determine a best case scenario of battery life based on the LEDs.
For example sake, 10 LEDs always being on will draw 200mA and alone will drain the battery in a couple hours. Of course, a switch-mode regulator will help you here.
So you may need to size up the battery, or be creative with how many LEDs or the current draw of each LED. (You can always run at partial brightness).
As far as the Lithium batteries are concerned, you can find package deals with a battery and charge controller. They are typically 3.7V. NiMh and NiCd AA or AAA can be used as well and charged with almost any charger you get your hands on with the added benefit of letting you use regular AA or AAA.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
For design/testing, I'll probably stick with the linear regulator.
A 9v 400-500mAh should just about do for 1 fight, keep a couple of spares, recharge overnight.

Will dig deeper into power once I've got the rest working. Quite a bit to take in.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Progress Report:
My Arduino (Nano-Clone) has arrived! :) :) :)

I have successfully plugged it into my PC, installed Drivers, programmed a Sketch onto the Arduino. Altered the Sketch and re-programmed. Moved Arduino onto breadboard, powered it with the 9v -> 5v Linear regulator I built myself, and run the sketch.

Proof of Concept success! :)

Next: I've got a Sketch of lots of switches and LEDs similar to the Tank layout. Build the circuit, program the Arduino. Debug!

One switch chooses from 3 "velocity" LEDs. 2 switches are More/Less (Hull Point) LEDs. Status: Stunned and Immobile switch/LED combos.
If i've programmed it right, Immobile Status will also force Velocity = 0, no matter the position of Velocity switch!

Probably be the weekend before I do much of this. Wish me luck! :)
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Progress Report:
My Arduino (Nano-Clone) has arrived! :) :) :)

I have successfully plugged it into my PC, installed Drivers, programmed a Sketch onto the Arduino. Altered the Sketch and re-programmed. Moved Arduino onto breadboard, powered it with the 9v -> 5v Linear regulator I built myself, and run the sketch.

Proof of Concept success! :)

Next: I've got a Sketch of lots of switches and LEDs similar to the Tank layout. Build the circuit, program the Arduino. Debug!

One switch chooses from 3 "velocity" LEDs. 2 switches are More/Less (Hull Point) LEDs. Status: Stunned and Immobile switch/LED combos.
If i've programmed it right, Immobile Status will also force Velocity = 0, no matter the position of Velocity switch!

Probably be the weekend before I do much of this. Wish me luck! :)
Good to hear!
Will you be using mostly switches or buttons?
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Test will probably use more switches. Final build I'm not sure. Probably buttons.
Your switches can be 'polled' (tested) much much more slowly than a button. Buttons are also usually of the momentary variety, where switches are usually latched in their position and require modifications to the program. From the looks of things so far though, you will be making good progress regardless of the route.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
I appreciate the optimism! :)
I think momentary buttons will be better for how I want most sections to work.
I'm very much focusing on the end-use experience, and how to make it happen.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
More Progress!
LED #13 flashes.
If button is pressed while LED off, it swaps whether LED #11 or #12 is on!

Right, I'm gaining confidence with breadboard and sketches.

I've written the main test-sketch for 7 LEDs, 7 switch/button inputs.
Now I need to buy some buttons and switches, and wire it up!

Question re: code -
Reading a digital pin and setting a variable. This looks to work:

if (digitalRead(immobileswitchPin)==HIGH){
immoblie = 1;
} else {
immoblie = 0;
}


but can I do:
immobile = digitalRead(immobileswitchPin);
Will this set immobile to 0/1?

I'm not too happy about pushing the arduino into the breadboard - scared of bending all the pins when I pull it out! What's the recommended method?
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
More Progress!
LED #13 flashes.
If button is pressed while LED off, it swaps whether LED #11 or #12 is on!

Right, I'm gaining confidence with breadboard and sketches.

I've written the main test-sketch for 7 LEDs, 7 switch/button inputs.
Now I need to buy some buttons and switches, and wire it up!

Question re: code -
Reading a digital pin and setting a variable. This looks to work:

if (digitalRead(immobileswitchPin)==HIGH){
immoblie = 1;
} else {
immoblie = 0;
}


but can I do:
immobile = digitalRead(immobileswitchPin);
Will this set immobile to 0/1?

I'm not too happy about pushing the arduino into the breadboard - scared of bending all the pins when I pull it out! What's the recommended method?
haha, that's typically the method. You can use a small flathead tool to pry up one end at a time so your Arduino or other chips don't end up looking like one of these : http://roboshop.lt/wp-content/uploads/2013/01/Hexbug-Nano-robotas-žaislas.jpg

Anyway... it's unfortunately been a while since I've dig into the Arduino IDE...
digitalRead is a function and I can't remember if it can be called there, but if you look into it, you can actually read a group of pins as if it were a variable.
Take a quick gander at 'Port manipulation' for the Arduino... this is how most microcontrollers do it. Arduino simply added a bunch of extras on top like the 'digitalRead' function to make it easier to work with.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Progress Report:
Speed-selector is working!
Set a switch selecting Stop (Red), Slow (Yellow) or Fast (Red), and the appropriate LED lights up.
UNLESS the Immobile switch is set, when the 'Immobile' LED lights up (inbuilt pin13 LED), and the Stop LED lights up, no matter what speed is selected!

I don't actually have any switches, so I'm just moving jump-cables between pins, with pull-down resistors to avoid 'floating' values for disconnected cables.

Next:
Hull Points.
Start with both (red and yellow) LEDs lit. Incoming damage may reduce HP (button to switch them off one by one) and may be repaired (button to light them back up 1 by 1). Unless all HP are gone, when tank is Wrecked! (Flash all HP LEDs a few times, then switch off everything*)

As the loop runs so fast, I don't want "one" press of a button to remove multiple HP, so I think I need a "has button been released" system.
Use a ButtonPressed variable?

<code>
if (HPbutton = 1) {
... if (pressed = 0){
........hp = hp -1
........pressed = 1
...} else {
...Print "You're still holding the button! Let go!" //do not decrease HP
...}
}

if (HPbutton = 0) {
pressed = 0
}
</code>
Seem OK?

*Is there a command to switch all outputs to LOW? I can loop, but either have to remember all my pins, or accidentally address "wrong" pins. If I send digitalWrite(5, LOW) when pin 5 is an INPUT, does the Magic Smoke get out?
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Progress Report:
Speed-selector is working!
Set a switch selecting Stop (Red), Slow (Yellow) or Fast (Red), and the appropriate LED lights up.
UNLESS the Immobile switch is set, when the 'Immobile' LED lights up (inbuilt pin13 LED), and the Stop LED lights up, no matter what speed is selected!

I don't actually have any switches, so I'm just moving jump-cables between pins, with pull-down resistors to avoid 'floating' values for disconnected cables.

Next:
Hull Points.
Start with both (red and yellow) LEDs lit. Incoming damage may reduce HP (button to switch them off one by one) and may be repaired (button to light them back up 1 by 1). Unless all HP are gone, when tank is Wrecked! (Flash all HP LEDs a few times, then switch off everything*)

As the loop runs so fast, I don't want "one" press of a button to remove multiple HP, so I think I need a "has button been released" system.
Use a ButtonPressed variable?

<code>
if (HPbutton = 1) {
... if (pressed = 0){
........hp = hp -1
........pressed = 1
...} else {
...Print "You're still holding the button! Let go!" //do not decrease HP
...}
}

if (HPbutton = 0) {
pressed = 0
}
</code>
Seem OK?

*Is there a command to switch all outputs to LOW? I can loop, but either have to remember all my pins, or accidentally address "wrong" pins. If I send digitalWrite(5, LOW) when pin 5 is an INPUT, does the Magic Smoke get out?
That's where switch Debounce is required. You could most certainly try an activate on release, but the speed of the microcontroller may accidentally see a 'bounce' as a push and release.

As far as setting groups of pins, you most certainly can! Easily and in two or 3 lines:
https://www.arduino.cc/en/Reference/PortManipulation
Instead of using digitalread or write, you can use the commands in the link above.

A digitalwrite low should fail if the pin is set as an input. By default, I think all pins are configured in a high impedance mode (input) instead of pre-defined output modes to prevent you from accidentally letting the smoke out during initialization.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
OK, Debounce. Ugh.
And checking Release as well?
This means I need to check button is not going on and off (bounce), but also check button IS going on and off (press and release to only adjust HP once per press).

OR use a Latching button (toggle?), and adjust HP each time it changes state (after debouncing)? (see picture)bounce.jpg
 
Top