newbie needs some coding help for leds please

hi, i'm totally new to arduino and i have a coding question...

(step 1 of project)
i have 20 leds, 20 resistors and a button.. my question is how do i make them randomly shuffle on and stay on? i would like the shuffle to be the same all the time.

thank you

oh, can each led have a value assigned to it like "a,b,c" or numeric like "1,2,3"? i'll be using those later if possible

thank you

my question is how do i make them randomly shuffle on and stay on? i would like the shuffle to be the same all the time.

Ok thats easy, there is are functions called random() and randomSeed(). Set a value for randomSeed and it initilizes the random function to give random outcomes, but the same random outcomes every time. (Its random but not random, if that makes any sense)

See Here

thank you for the link! :slight_smile:

so.. changing the values as i did, would be correct?

long randNumber;

void setup(){
  Serial.begin(9600);

  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
  randomSeed(analogRead(0));
}

void loop() {
  // print a random number from 1 to 20
  randNumber = random(20);
  Serial.println(randNumber);  

  // print a random number from 1 to 20
  randNumber = random(1, 20);
  Serial.println(randNumber);

  delay(50);
}

not sure what " Serial.begin(9600);" is or does, can you explain please?

Serial.begin(9600) allows you to use the serial monitor. It is essential for debugging code. Serial.begin initializes the use of the serial monitor at 9600 baud or rate at which the data is transferred.

With your arduino plugged into your computer, you can press Ctrl + Shift + M, or in the top right corner of the arduino IDE, there is a box with a little magnifying glass. Or, simply go to the Tool menu and click Serial Monitor.

i see, thank you. one last question for (step 1 of project) if i may.. does this random code stop duplicate numbers from being drawn or is there a way or something to add to stop that from happening? sometimes i may want to allow duplicate numbers, other times i may not.

thank you

lol, that was said in the movie HACKERS starring angelina jolie.. XD

"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne

Not sure if it does output duplicate number or not, but why not just do a test to see if you get duplicate numbers from that example sketch?

Also, you are correct, that quote is from the movie HACKERS, and that's where I got it from. And if I ever meet Mathew Lillard, I want to say "HACK THE PLANET!" and see what his reaction is. XD

XD

If you don't want the random numbers to be duplicates, then use this sketch I made.

long randNumber;
int Count = 20
int list[Count];
int i=0;
boolean found = false;

void setup(){
  for(int j =0; j<Count;j++)
    list[j] = 0; // set all the array blocks to 0

  Serial.begin(9600);
  randomSeed(1);
  delay(50);

  while(i < Count) // will cycle through until the array is filled
   {
    randNumber = random(1, Count); // lowest value is 1, and highest value is 20
    for(int L=0;L < i;L++) // will cycle through the list array and check the new number too
    {
      found = false;
      if(list[L] == randNumber) // if the random number IS found, then its a duplicate and discard it
      {
        found = true; // number has a duplicate, no good
        break; 
      }
    }

    if( !found )// if the random number does NOT have a duplicate, add it to the list array
    { 
        list[i] = randNumber;
        Serial.println(list[i]);
        i++;
    }
  }
}

void loop()
{
  //empty
}

Output with no duplicates:

12
8
9
18
10
14
4
19
17
11
13
1
7
6
3
2
16
15
5

i shall give that a try tomorrow, it's getting late and i must get up early..

thank you

i'm sorry for the late reply, i have been very busy x-mas shopping and getting ready for thanksgiving..

Well, it worked nice, i have also started building an enclosure for the unit, it's not as easy as i originally thought. i want to change from 20 leds to 25, i haven't used all 20 as of yet so i'm thinking i'm going to need a chip of some sort.. to control all these leds individually, i'll have to look into that - so many different kinds of chips. i say 25, but i want to keep adding leds but it will always be an odd number because i want a starting point in the middle. my 2 girls are into pixel art, so i'm sure you can see where this project is going. yes, i thought about getting a lite-brite, but the loose/lost pegs will be a nightmare and to constantly buy the paper sheets.

here is what i got for ideas so far, trying to keep it simple.

4 directional buttons, 1 set on button - same button to be used as a clear/off button. eventually go from single color leds to rgb ones, but they are expensive - this project gonna cost me more than i expected. but then i could add a color picker button. omg, this is starting to sound over whelming! :cold_sweat:

It sounds like a fun little project! If you ever get it working, maybe you can patent it as a new toy.

lol, that would be neat! but i am just starting off and this is all uncharted territory for me, i'm almost anybody here could beat me to it. as far patenting it, too much money for that and i'm almost positive they'll already have something coming out soon similar soon if they don't already have it out in stores somewhere.

was thinking, is there a way to keep the art in a memory chip and/or print the designs they create? more ideas means more money to spend lol, but the worst part is having to take the arduino everywhere for them to use it i don't want an accident to happen and it breaks. kids will be kids afterall.

thank you for the encouragement, i'm glad you like the idea. :slight_smile:

There are SD card modules and external EEPROM modules that you could maybe use to store the images. That might be good to look into.

hmm, what i want to do for starters is have 5 leds, the center one either lit or blink just to let me know it's at the starting point, then i have 3 buttons, with 2 of the buttons i want the led to light to move either right or left depending on which button i press and light it up blinking to let me know it where i wanted it to go, the third button actually keeps that current led on (or turns it off), so when i move to another led the next one is lit or blinking to show me it is where i wanted it to go.

i honestly hope that makes sense if not feel free to let me know, but can somebody please tell me how to code that?

please keep in mind i am only doing 5 leds for now, but more later and that's when i'll have to get into using chips but i'll look into that later.

thank you

I just threw this together, try it out. A few tweaks will be needed to get it just how you want it, but its a start.

int LEDs[5] = { 
  3,4,5,6,8};
int  i=0,Lcursor = 2;
long Ptime = 0;

byte ButtonState[3];  // use array for the multiple button states
byte lastState[3] = {
  LOW}; //-------------------------
unsigned long oldTime[3] = {
  0};

void setup()
{
  Serial.begin(9600);
  for(int L = 0 ;L < 5; L++){
    pinMode(LEDs[L], OUTPUT);
  }  
}

void loop() 
{
  ButtonState[0] = digitalRead(A0);//right
  ButtonState[1] = digitalRead(A1);//select
  ButtonState[2] = digitalRead(A2);//left

  if(ButtonState[0]&& ButtonState[0] != lastState[0]){
    Lcursor >= 4? Lcursor = 4:Lcursor++;
  }
  lastState[0] = ButtonState[0];
  
  if(ButtonState[1]&& ButtonState[1] != lastState[1]){
    Select =! Select;
  }
  
  if(ButtonState[2] && ButtonState[2] != lastState[2]){
    Lcursor <= 0?  Lcursor = 0:Lcursor--;
  }
  lastState[2] = ButtonState[2];
  
  blink(Lcursor);
  i > 2? i=0:i++;
}

void blink(int C){
  Serial.println(C);
  if(millis() - Ptime > 1000)
  {
    Ptime = millis();
    digitalWrite(LEDs[C], !digitalRead(LEDs[C]));   
  }
}

whoa! :astonished:

all that for 5 leds? sheesh.. i can imagine what it will look like after i decide how many leds i want in the end.

but i just gotta try this! but first i already have plans to make stuffies :slight_smile: so it will have to be later tonight

thank you HazardsMind!

Its actually not a lot, it just looks like it. All you really need to do is change a few variables and the Led array. Also as of right now, the "select" button is not being used, but Im sure you'll find a use for it.

i may only have 5 leds going in a straight line at the moment, does this code support up and down as well or is that something else that needs to be added in?

thank you Hazardsmind :slight_smile:

You would need to change the current array to a 2D array and add two more buttons. Simply copy and paste the ButtonState[0] and change the numbers. Also don't forget to change the global initializer too.