Maker Pro
Maker Pro

Dr Who's K9 circuit help...

trog

Oct 5, 2014
21
Joined
Oct 5, 2014
Messages
21
Hi Folks,

I have built a radio controlled K9 for my son, Not had too many problems but we now want to make the buttons on his back flash...
2016-09-30 11.03.55.jpg

The link takes you to a scratch program we wrote that shows you what he wants.

https://scratch.mit.edu/projects/122773234/

Basically we want a random pattern of 4 lights (from 12) to be selected every second...

Question is can this be done with "simple" logic chips? There are many twinkling light circuits etc out there but nothing like this! I am sure it could be done with an Arduino but we haven't progressed that far yet and now is not the time to make that step...

Any advice would be much appreciated.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
I have not at hand a circuit that matched your exact description, but this christmas tree circuit may give you a start. Disperse the LEDs randomly and you'll get a random pattern (not easy to find a pattern for an unknowing eye anyway). Hide unnecessary LEDs within the box or replace them by simple 1N4148 Diodes.
 

hevans1944

Hop - AC8NS
Jun 21, 2012
4,878
Joined
Jun 21, 2012
Messages
4,878
An old-fashioned EPROM could be programmed with a "pseudo-random" sequence and a simple counter would increment the address. These are (or were) available in various memory "depths" but usually with one byte (eight bits) outputs, so you need two of them to provide twelve outputs with four "spare" outputs doing nothing. Problem is, this is ancient technology and requires an EPROM programmer. And if you want to try different sequences, an ultra-violet eraser. But it is the simplest solution if you can implement it.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
An old-fashioned EPROM could be programmed with a "pseudo-random" sequence
If I remember correctly that's what is used here. A copy of a reprint of the article decribing the circuit is supposed to be included here (need to buy).
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
A shift register with appropriate feedback to generate a pseudo- random sequence, linked to the "buttons" in a random order could do the trick.
 

trog

Oct 5, 2014
21
Joined
Oct 5, 2014
Messages
21
OK all of these are way too complicated so I can assume that there is no easy way to get this pattern?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Well, none are really complex, although they might sound like it to you.

Using an arduino is probably the simplest in some respects, although if you need to drive a lot of LEDs you'll need to learn about charlieplexing.

Another option is to build a number of LED flashers, each at a slightly different speed.
 

hevans1944

Hop - AC8NS
Jun 21, 2012
4,878
Joined
Jun 21, 2012
Messages
4,878
A shift register with appropriate feedback to generate a pseudo- random sequence, linked to the "buttons" in a random order could do the trick.
The problem with shift registers is ensuring four and only four LEDs from a group of twelve are "randomly" selected. I can't think of a simple way to do that with shift registers, hence the "look up table" approach. I've got several UV-erasable EPROMs pulled from various equipment in the previous century, along with an EPROM programmer that runs on MS-DOS (if I could ever find the Chinese drivers for it) and a nice little UV lamp with conductive sponge tray for bulk-erasing the EPROMs. So the long pole in my tent is programming those suckers. Since FLASH memory has replaced EPROMs, I lack the incentive to dig up and grease that particular wheel. I hang onto 'em "just in case" something comes along where I can acquire the incentive to program them... again. :rolleyes:
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
They're are only 495 ways of selecting 4 from 12. This is completely doable from an eprom, and the number is actually large enough that of you ordered them randomly in the eprom you could go through them sequentially.

However it would probably be less effort to do it with an Arduino.

If the OP wanted to go down this route, I would recommend purchasing an Arduino pro mini clone (with USB) from eBay. I would be happy to talk him through the required coding and wiring required.
 
Last edited:

trog

Oct 5, 2014
21
Joined
Oct 5, 2014
Messages
21
Thanks Guys, So I think it looks like Arduino is the way to go! We have an UNO that we have just started experimenting with (up to traffic lights in the tutorial!). I think once we are up to speed a bit more we will try it. Couple of questions though:
  1. Can the Arduino pick random numbers or do we use a "pseudo random" sequence of numbers?
  2. Can I drive all 12 LED directly from the 12 outputs on the board or do we need to do something cleverer?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
The arduino has its own random function. Yes it's pseudo random.

All you need is a resistor in series with the LED.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
A simple way of choosing the 4 lights to turn on is to start with an array of 12 integers (they will have indexes 0 to 11) initialized to the numbers 0 to 11.

Each time you want another set of lights you loop through the first 4 elements of the array (0 to 3) and generate a random number in the range 0 to 11. The current element is swapped with the new random element. At the conclusion of this you use the numbers in the first 4 entries of the array to determine which lights are on.

This ensures selection without replacement, i.e. you *always* get 4 different lights without the need to check for duplication.
 

trog

Oct 5, 2014
21
Joined
Oct 5, 2014
Messages
21
Hi Steve,

So I didn't understand most (all) of what you described above but I had a go last night and I have something that works, it's only flashing 3 out of 5 LEDs at the moment as we had 5 LEDs set up on the bread board for my sons project and I didn't want to upset that!

I was surprised how easy it was in the end! Only one doh moment! Tonight I am going to extend it to 4 from 12 and try wiring it up to K9!
Sure there is a shorter way of doing this but here is what I coded!:

// K9 Lights

int LED12 = 12;

int LED11 = 11;

int LED10 = 10;

int LED09 = 9;

int LED08 = 8;

int Number1 = 0;

int Number2 = 0;

int Number3 = 0;


void setup() {

// Pinmode definition

pinMode(LED12, OUTPUT);

pinMode(LED11, OUTPUT);

pinMode(LED10, OUTPUT);

pinMode(LED09, OUTPUT);

pinMode(LED08, OUTPUT);

}


void loop() {

//Turn all LED's off


digitalWrite(LED12, LOW);

digitalWrite(LED11, LOW);

digitalWrite(LED10, LOW);

digitalWrite(LED09, LOW);

digitalWrite(LED08, LOW);


// Random Number

Number1 = random(8,13);

Number2 = random(8,13);


while (Number2 == Number1){

Number2 = random(8,13);

}

Number3 = random (9,13);


while (Number3 == Number1 || Number3 == Number2)

Number3 = random (9,13);


//Select LED to turn on


if (Number1 == 12)digitalWrite(LED12, HIGH);

if (Number1 == 11)digitalWrite(LED11, HIGH);

if (Number1 == 10)digitalWrite(LED10, HIGH);

if (Number1 == 9)digitalWrite(LED09, HIGH);

if (Number1 == 8)digitalWrite(LED08, HIGH);


if (Number2 == 12)digitalWrite(LED12, HIGH);

if (Number2 == 11)digitalWrite(LED11, HIGH);

if (Number2 == 10)digitalWrite(LED10, HIGH);

if (Number2 == 9)digitalWrite(LED09, HIGH);

if (Number2 == 8)digitalWrite(LED08, HIGH);


if (Number3 == 12)digitalWrite(LED12, HIGH);

if (Number3 == 11)digitalWrite(LED11, HIGH);

if (Number3 == 10)digitalWrite(LED10, HIGH);

if (Number3 == 9)digitalWrite(LED09, HIGH);

if (Number3 == 8)digitalWrite(LED08, HIGH);


delay (1000);
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
I'll try to write something for you.

If I don't post something in a couple of days, post again to remind me.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
This is going to be written off the top of my head. Expect syntax errors!

Code:
const int numLeds = 12; // there are 12 LEDs
cons int numOn = 4; // we want 4 of them to be on at any one time

// if the LEDS are not on pins 1 thru 12, change them here
int LEDS[numLeds] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

void setup() {
    for (int i = 0; i < numLeds; i++) {
        pinMode(LEDS[i], OUTPUT); // set pin to output
        digitalWrite(LEDS[i], HIGH); // start with them all on (as a lamp test
   }
}

void nextLights() {
    for (i = 0; i < numOn; i++) {  // we want to randomize the first 4 positions
        int j = random(0, numLeds);  // get a random position
        int temp = LEDS[i];  // now swap i and j
        LEDS[i] = LEDS[j];
        LEDS[j] = temp;
    }

    for (int i = 0; i < numLeds; i++) {  // for each of the LEDs
        digitalWrite(LEDS[i], i < numOn); // turn them on or off as required
    }
}

void loop() {
    delay(1000); // wait a second (first delay will have all the LEDs on)
    nextLights(); // turn on a random 4 lights
}

This code has a feature that all the LEDs are turned on for a second when the program starts. That way you can confirm all the LEDs are working without waiting to see if they eventually turn on after some random delay.
 

trog

Oct 5, 2014
21
Joined
Oct 5, 2014
Messages
21
*steve* Couple of syntax errors but works perfectly! Thanks very much for the help.

One other question, the current LEDs are not bright enough to be visible in bright sunlight etc. So I am going to but some brighter ones in that may draw more than 40 mA. Would be be better to switch the LED's with transistors or FETS?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Sorry about the syntax errors.

I'd use a small mosfet, but its not critical.

For daylight I'd probably use a 1W white LED and choose a current that gives you the required brightness.

If you are operating in vastly differing lighting conditioned you might want to have an adjustable current (not as easy as you might think) or an "indoor"/"outdoor" switch.
 

trog

Oct 5, 2014
21
Joined
Oct 5, 2014
Messages
21
Update: I now have a finished setup, I have changed the change rate to quarter of a second. I am using "superbright LEDS, which show up really well in the sun. With a 200 ohm resistor they draw 15 mA which is below the 20 mA max on the Arduino so at the moment they are driven directly. (They also have a theoretical duty cycle of only 25%.) I may add mosfets (on order) later as this will be on all day and I am concerned the Uno might get hot in an enclosed space?

Now of course we have to go further... We were playing about with an 8 by 8 LED matrix, it looks great behind the red perspex screens. Now I just need to work out how to make the text scroll........

Thanks Steve for the help.
 

trog

Oct 5, 2014
21
Joined
Oct 5, 2014
Messages
21
Steve,

Wondering if you may be able to give use a bit more help... We have now got two 8 by 8 LED matrices scrolling text etc. that will be behind the 2 red panels above the buttons, So 2 programs, your one for the buttons and one we poached off the internet and adapted. We have combined them both so they can run off the same arduino but we have an issue... The LED's scroll every 50 ms, the lights change every 250 ms. We can only get the two to work at the same speed...

I think what we need to do is add a count to the Matrix steps so the lights change every 5th step, but I have no idea where to add the count... If you were able to have a look at the code (attached) and suggest how we do it we would be very grateful...

Libraries for the code are here: https://www.dropbox.com/sh/g594dib0oubv3b0/AAAv_PdX0dr0azImj8IrL35Ma?dl=0
 

Attachments

  • Serial_LED_Working_K9_Combined_Lights_and_LED.ino
    11.5 KB · Views: 104

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
There are a few ways to do it.

Both involve having routines that do one step and immediately return without delay.

For the light panel, that is to choose another 4 LEDs and change the display to show them. For the character matrix that is to scroll the display one position to the left and display the next column.

The easy way is to have 5 50ms delays in a loop. The routine that should be called every 50 ms is called after each delay. The over requiring a 250ms delay is called after the fifth delay.

It would look something like:

Code:
While true do
{
    delay (50);
    call_routine_1();
    delay (50);
    call_routine_1();
    delay (50);
    call_routine_1();
    delay (50);
    call_routine_1();
    delay (50);
    call_routine_1();
    call_routine_2();
}

A more complex solution takes the current time and adds the delay between calls (50 or 250ms) to it. It then calls the routine. It then repeats this procedure whenever the current time is greater than or equal to the stored time value. This method has the advantage that the time taken inside the routines is compensated for, and it easily allows the arduino to do other stuff.

Code:
Int next_1 = 0;
Int next_2 = 0;

While true do
{
    if millis() >= next_1
    {
        next_1 = next_1 + 50;
        call_routine_1();
    }
    if millis() >= next_2
    {
        next_2 = next_2 + 250;
        call_routine_2();
    }
}

The second method is also better because the delays do not need to have any numerical relationship and may in fact change.

As an example, your K9 might want to have it's lights change faster when it is doing something and slower when it is "sleeping" but always scroll the text at a specific unvarying speed.
 
Top