random light

hi all i am new to all of this but i am after some advice,i have put together my circuit board and lights etc for my project, i have the script written out as below

int led1 = 4; int led2 = 5; int led3 = 6; int led4 = 7; int led5 = 8; int led6 = 9; int led7 = 10; int led8 = 12;

void setup() { // put your setup code here, to run once: pinMode(led1,OUTPUT); pinMode(led2,OUTPUT); pinMode(led3,OUTPUT); pinMode(led4,OUTPUT); pinMode(led5,OUTPUT); pinMode(led6,OUTPUT); pinMode(led7,OUTPUT); pinMode(led8,OUTPUT); }

void loop() { // put your main code here, to run repeatedly: digitalWrite(random(3,13),HIGH); delay(250); digitalWrite(led1,LOW); digitalWrite(led2,LOW); digitalWrite(led3,LOW); digitalWrite(led4,LOW); digitalWrite(led5,LOW); digitalWrite(led6,LOW); digitalWrite(led7,LOW); digitalWrite(led8,LOW); }

this works well, however my goal is for a random 4 lights out of the 8 to light up with 250ms between each one which i have done but i want the programme to then pause/delay for 1000ms after the last light has come on before continuing with another random 4 lights coming on, i had looked on internet but its a mine field,like i said im new to the but confident in following instructions etc, any help would be much appreciated.

Have you tried putting a delay function at the end of the loop?

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(random(3, 13), HIGH);
  delay(250);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  digitalWrite(led8, LOW);
  delay(1000);
}

Also, Please read “Read this before posting a programming question” at the top of the forum. And use CRTL-T when in the IDE.

First you need to learn a little bit about using arrays. You can put all your pins in an array and next you can iterate of the array and set the pinMode.

// the led pins
uint8_t ledPins[] = {4, 5, 6, 7, 8, 9, 10, 12};

void setup()
{
  Serial.begin(57600);
  while (!Serial);

  // set led pins as output
  for (uint8_t cnt = 0; cnt < sizeof(ledPins); cnt++)
  {
    pinMode(ledPins[cnt], OUTPUT);
  }
}

void loop()
{
}

Next you can create an array to hold the status of each led (HIGH or LOW); that array can be filled later randomly.

// the led pins
uint8_t ledPins[] = {4, 5, 6, 7, 8, 9, 10, 12};
// array to hold status of the led pins; will be filled randomly with HIGH; this array has the same size as the ledPins array
uint8_t ledStatus[sizeof(ledPins)];

void setup()
{
  Serial.begin(57600);
  while (!Serial);

  // set led pins as output
  for (uint8_t cnt = 0; cnt < sizeof(ledPins); cnt++)
  {
    pinMode(ledPins[cnt], OUTPUT);
  }
}

void loop()
{
}

Next you can write a function to fill the ledStatus array with a random HIGH and LOW

/*
  fill ledStatus array randomly with 4 HIGH values
  In:
    number of leds to switch on (set HIGH)
*/
void fillRandom(uint8_t numLeds)
{
  // clear ledStatus
  memset(ledStatus, LOW, sizeof(ledStatus));
  // counter
  uint8_t cnt = 0;

  if (numLeds > sizeof(ledStatus))
  {
    Serial.println("fool");
    for (;;);
  }

  // indicate which leds must be HIGH
  do
  {
    // get a random number
    int rv = random(0, sizeof(ledStatus));
    // if not already set
    if (ledStatus[rv] != HIGH)
    {
      // set to high
      ledStatus[rv] = HIGH;
      // increment counter
      cnt++;
    }
  } while (cnt != numLeds); // till numLeds elements are HIGH
}

With the comments, this should be self-explaining.

Lastly you can use this function in loop() to fill the array and set the leds based on the value in the array; the below code does not do that (something for you to figure out) but display the status for each led in the serial monitor.

void loop()
{
  // switch all leds off
  for (uint8_t cnt = 0; cnt < sizeof(ledPins); cnt++)
  {
    digitalWrite(ledPins[cnt], LOW);
  }

  // fill ledStatus array with N high values
  fillRandom(-1);

  // display result
  for (uint8_t cnt = 0; cnt < sizeof(ledStatus); cnt++)
  {
    Serial.print(ledStatus[cnt]);
    Serial.print(" ");

    // set ledPin[cnt]; for you to do

    // delay if ledStatus[cnt] equals HIGH; for you to implement the condition
    delay(250);
  }
  Serial.println();
  Serial.println("================");
  delay(5000);
}

Notes
1)
There is a one-on-one relation ship between the elements in the ledPins array and the ledStatus array.
2)
Please read https://forum.arduino.cc/index.php?topic=148850.0, specifically point #7 about posting code. Your code will than be shown as in this post.

hi stevemann, i tried what you suggested, by putting the delay of 1000 at the end, it seems by doing this it would light one light then wait for 1000 before lighting the next light!

hi sterretje
i am playing around with what you have put, i did come across something mentioned on the internet,which was millis() would i be wrong in saying this could be used in my script, as i understand its kind of like a delay but the rest of the script can still run, im not sure where it would go or how i would write it though! would this work!
so i would have the millis() activated to run for 1000ms while that going the 4 random lights wil come on with 250ms in between each then repeat the process.
again thanks in advance for any help its much appreciated.

i did come across something mentioned on the internet,which was millis() would i be wrong in saying this could be used in my script, as i understand its kind of like a delay but the rest of the script can still run

millis() is certainly the way to go if you want to implment a "delay" in your code without blocking its free running

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

hi ukhelibob may sound like a stupid question, how would you write this in the script, the script i have, parts were copied off the internet and pieced together which worked, im not used to all the terminology of these programmes or how to write/put them together, its not that i dont appreciate other comments snd guidance, its just alot to take in and try to work through. many thanks

The first thing you need to decide is whether you actually need to use millis() at all or will delay() do after all. Using millis() allows you to write code that does not block the operation of other code so that you can, for instance, turn an LED on for a period then turn it off whilst still being able to read the state of an input whilst the timed on period occurs. You could not do that using delay()

Your program, however, does nothing but turn LEDs on and off or wait a while but as an exercise you could choose to use millis() for your 1 second "delay" so let's do that

Declare a global boolean variable, name it waiting and set it to false Declare global variables of type unsigned long named currentTime, startTime and period and set period to 1000. The value of the others does not matter but the compiler will set them to zero anyway. Wrap all of your code apart from the final delay(1000) in an if like this

if (!waiting)
  {
     //your LED code goes here
     //add the lines below
     waiting = true;
     startTime = millis();
  }

Now let's add code to do the 1 second "delay" Still in loop() add this

else
  {
    currentTime = millis();
    if (currentTime - startTime >= period)  //the waiting is over
    {
       waiting = false;
       //put code here to turn off all of the LEDs
    }
  }

Note that this is untested and written off the top of my head

i will try it, much appreciated

hi all again as i stated before i am new to all this programming stuff, i am still trying to get my head around all the terminology etc, it can be a bit overwhelming with all the scripts and jargon etc, again i do appreciate all the help put forward, i was given the following script to try, which does give me more of the desired effect i am after,i get flashing lights but they are sporadic, as in it will light up anything between 1 light and 4 lights in one loop, it then does however give me my delay of 1500ms which is good, as before i want 4 random lights to light up with 250ms gap in between each one, then a delay of 15000ms, is there anything i can change, bearing in mind im a newbie and still getting to grips with all the script writing etc, again many thanks for help so far and any further help provided. much appreciated

*code int led1 = 4; int led2 = 5; int led3 = 6; int led4 = 7; int led5 = 8; int led6 = 9; int led7 = 10; int led8 = 12;

void setup() { // put your setup code here, to run once: pinMode(led1,OUTPUT); pinMode(led2,OUTPUT); pinMode(led3,OUTPUT); pinMode(led4,OUTPUT); pinMode(led5,OUTPUT); pinMode(led6,OUTPUT); pinMode(led7,OUTPUT); pinMode(led8,OUTPUT); }

void loop() { // put your main code here, to run repeatedly: digitalWrite(random(3,13),HIGH); delay(200); digitalWrite(led1,LOW); digitalWrite(led2,LOW); digitalWrite(led3,LOW); digitalWrite(led4,LOW); digitalWrite(led5,LOW); digitalWrite(led6,LOW); digitalWrite(led7,LOW); digitalWrite(led8,LOW); digitalWrite(random(3,13),HIGH); delay(200); digitalWrite(led1,LOW); digitalWrite(led2,LOW); digitalWrite(led3,LOW); digitalWrite(led4,LOW); digitalWrite(led5,LOW); digitalWrite(led6,LOW); digitalWrite(led7,LOW); digitalWrite(led8,LOW); digitalWrite(random(3,13),HIGH); delay(200); digitalWrite(led1,LOW); digitalWrite(led2,LOW); digitalWrite(led3,LOW); digitalWrite(led4,LOW); digitalWrite(led5,LOW); digitalWrite(led6,LOW); digitalWrite(led7,LOW); digitalWrite(led8,LOW); digitalWrite(random(3,13),HIGH); delay(200); digitalWrite(led1,LOW); digitalWrite(led2,LOW); digitalWrite(led3,LOW); digitalWrite(led4,LOW); digitalWrite(led5,LOW); digitalWrite(led6,LOW); digitalWrite(led7,LOW); digitalWrite(led8,LOW); digitalWrite(random(3,13),HIGH); delay(200); digitalWrite(led1,LOW); digitalWrite(led2,LOW); digitalWrite(led3,LOW); digitalWrite(led4,LOW); digitalWrite(led5,LOW); digitalWrite(led6,LOW); digitalWrite(led7,LOW); digitalWrite(led8,LOW); delay(1500); }

*code

Please replace your first *code by [code] and your second one by [/code]

Did you try the code that I gave earlier.

i did try to follow it as best i could, but was getting myself confused, might sound daft, but to someone who doesnt really understand it its alot to get to grips with, it may sound lazy but i could do with someone writing it out complete for me, i appreciate thats not what everyone on here is for and shouldnt be expected to do so, i respect that, im just frustrated with myself for not being able to take it in and understand it all.
kind regards

If you don't understand something, you should ask for a further explanation. And do a little research on terms that are thrown at you.

I tried to break down my code in such a way that you could deduct how to do the last step. I obviously failed which is possibly just as frustrating.

i dont put it down to you, i guess its just as hard for you guys to try to understand what people are asking you for, and for you to try to explain to a newbie cant be easy, i can try to upload a file/video of the light sequence i have if thats any more help (not sure how though),dont put yourself down, all you guys are doing a brilliant job,as all this comes out of your own time, your not obliged to help anyone but you do and its appreciated, i will have another crack at the code to get my head around it. much appreciated