Analise this sketch please

Willsome kind person tell me what sequence I can expect from the following Arduino Uno sketch, that is, how many lights on at one time, duration of sequence and so on, as I need to test the circuits before turning the layout upright. (82 years old, can’t crawl around the nether regions). Any help appreciated.

Ron from down under.

#define numleds 12

byte ledpins = { 0,3,4,5,6,7,8,9,10,11,12,13 } ;

void setup( ) {

for ( int i=1; i <= numleds; i++ ) {

pinMode ( ledpins [ i ], OUTPUT) ;

&

Did you try it on a breadboard first??

It depends on Pin 2. If Pin 2 is high, there is about a 60% chance any given pin will turn on. It’s possible that ALL of them could end up on, it’s also possible that ALL of them could end up off, regardless of the state of Pin 2. If Pin 2 is low, then whatever random pin is selected in the loop will be turned off.

Many times through the loop, nothing will happen - a pin that is already on will be selected at random and set to on, or a pin that is already off will get set off again. The loop pauses 4 seconds each time through.

–Randy

the ledpins starts at index 0 and ends at 1 less than the size (i.e. 11). The for loop in setup() should be

for ( int i=0; i < numleds; i++ )

likewise in loop(), the range of random should be limited to numleds-1 not numleds+1

i’m not sure about the need for digitalWrite(2, HIGH). Instead of pinMode(2,INPUT), i think pinMode(2, INPUT_PULLUP) is what you want. (see pinMode)

if pin-2 is made low, it will take loop() 48 sec = 12 * 4 to possibly affect each LED. But since loop() picks the LED randomly, it may take much longer for all the LEDs to turn off have turned on just once. Likewise, it may take just as long for any one LED to come on and much longer for all of the LEDs to have come on just once turn off.

only one LED has a chance of changing state every 4 sec. No two LEDs can change state at the same time.

I can see how this might be used to simulate building lights at night (pin-2 on) and day (pin-2 off). But there’s only a chance of any one light turning on or off every 4 seconds. But the delays when pin-2 changes would make sense for dusk and dawn.

What they said. I know that’s not very helpful from my end. But their reverse analysis is correct.

Ron that sketch looks like my original 12 LED random sketch, it is totally random, never the same. Changing the two delays will change the duration of the random pick as well as the on-off delay.

I have a 14 port sketch that is slightly modified, at the time I didn’t know I could use the TX RX pins (1 & 2) for outputs.

+++++++++++++++++++++++++++++++++++++

#define numleds 14
byte ledpins = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13 } ;
void setup( ) {
fo

Hey Mel, it looks like I had better start this from scratch. I have nine pins connected to outputs because I was advised that the Arduino couldn’t cope with twelve. No LED’s are connected yet. #1 to #9 pins. I have a switch to turn chip power on during the evening. All I need is a sketch to randomely switch these LEDs say five or six at a time. I have a completely separate circuit for “constantly on” street lights and so on. Just something realistic would do. But I don’t understand sketches at all so all the replies are lost on me.

Ron from down under.

More detail (it may help if you look at the initial tutorials which will walk you through the basics required in every sketch and introduce just one concept at a time instead of diving into a more complex program). Also, this sketch as shown takes advantage of some language features that make it more efficient, which has the downside of being slightly less readable for a newcomer. I’m not saying the sketch is wrong, what I saying is this is not how someone knew to the whole thing would write it, the way it is indicates a greater experience level with the language.

Anyway… (basing off your initial post version of the code)

First line - that means any time the word ‘numleds’ appears in the program, the value 12 is substituted.

Next line - ledpins is an array listing the Arduino pin numbers that are being used. Because of the way the code works later, there needs to be at LEAST 12 items listed - matching the previous line.

3rd line - all Arduino Sketches need a setup function. For the purposes of sketches like this, just do it as shown. It’s a more advanced topic to know what the ‘void’ means.

4th line - a FOR loop simply counts, the three parameters inside the () tell it how to count. In this case, variable i is used for a counter. It starts at 1. It keeps counting as lon as i is less than or equal to numleds (12), and after each loop it adds 1 to the value of i. So first time, it is 1, then 2, then 3, up until 12. Then it adds 1 to i and makes it 13, and since 13 is not less than or equal to 12, the loop ends.

Inside the loop are two lines. The first one is pinMode, this is the command to tell the Arduino what you are going to use a specific pin for - input or output. In our case, we want to make the pins for the LEDs outputs. Note the reference to the ledpins defined back at the beginning. This returns the i’th item in the ledpins array - when i is 1, it returns the fiurst value, which is 0, when i is 2, it returns the second va

I found a very basic sketch for a random generator online and played around with it until I got it to dowhat I wanted.

I changed it over and over to get it to the point it is now. For my wants it works very well now. I was using 12 position DIP switches to manually turn on and off lights in two multi level houses and the first 12 light random generator plugged in and filled the bill. As time went on I added the two final ports to my controller then added another light and a flickering fireplace to the houses.

Because of the max current limitations of the Arduinos I ran my test LEDs at 2ma per port and it will at some time have all 14 LEDs on without damage to the UNO. I’ve had three UNOs operating as random controllers for several months many times running them 10 hours at a time while working on my layout. Because I use incandescent lighting I went wit high current 7 channel driver chips, not one problem.

Check your IM Ron

Mel

Wow! That’s amazing guys. Thanks for the great info. One question; I have a LED connected to pin 2, it seems I need to disconnect that. But how do I ground pin 2?

Ron from down under.

Hook a wire between pin 2 and the GND on the Arduino. But you probably would want to put a toggle switch in line with that so you can turn the lights off without modifying the circuit each time. Plain SPST toggle, hook one side to pin 2, the other side to a GND pin.

–Randy

are you saying that every once in a while (msec?) you want 5 random LEDs to change state (turn on if off and turn off if on)?

Yes Greg, but more like a minute or two would be more realistic.

Ron from down under.

What’s my IM Mel?

Click on “Messages” on the right side of the page below the green bar with your name in it.

I think this is what you’re asking for.

list the LEDs you want in ledpins . Near the bottom, change N_LED to the number of LEDs you want to change each period. You mentioned 5 or 6. Change N_SEC to the number of seconds you want to wait before changing. You mentioned 2 minutes or 120 sec. They are at small values so that I could test the code.

I left serial debugging in. You can remove it.

The setup routine sequences thru the LEDS, turning one on each second until they are all on, and then turns them off one at a time every second. This allows you to verify the LEDs being controlled and that they work whenever you power up the circuit.

toggleN() repeatedly and sequentially goes thru the list of LEDs until the number requested to change has changed. ledIdx keeps track of the next LED to check each iteration. This means each LED has the same probability of being changed, not the ones at the beginning of the list

delay() has a limit of 65535. So there’s a loop to wait as many seconds as you’d like.

yes, i like to code

// toggle N_LED LEDs every N_SEC seconds

byte ledpins [] = { 11, 10, 9, 13 };

#define NumLeds sizeof(ledpins)

// -----------------------------------------------------
// configure each LED pin as output
void setup()
{
    unsigned int ledIdx;

    Serial.begin(9600);

    for (ledIdx = 0; ledIdx < NumLeds; ledIdx++)
        pinMode (ledpins[ledIdx], OUTPUT);

    // sequentially turn each LED on and then off
    for (ledIdx = 0; ledIdx &

Great code Greg!

This looks like the way to go. Just a matter of removing the chip from the layout and reprogramming it on the computer unless I can find a cable about ten yards long. As to the serial debug, I’d like to use it but it means installing and removing the chip to cut the debug. Decisions, decisions…

Ron from down under

won’t affect operation if there isn’t a usb terminal listening. suggest you leave it

Greg,

I’ve been off line while replacing my hard drive.

I tried ti run your sketch and got an error message:

"A function-definition is not allowed here before ‘{’ token

That’s the line after the last ‘void’

Can you tell me how to fix it?

Ron from down under.