I have to do a project and im struggling with. I want to do a random button when pressed it's going to light up 8 out of 16 LED, those LED's will light up random every time you press the button, and when LED’s will light up their binary number for them will be represented in a LCD Display 16X2
Do you mean that their decimal value will be shown on the LCD
If 8 out of 16 LEDs light up then how will you know which LED represents which bit in the number ?
More details of your assignment please
Please post schematics. This coloured bird nest looks like having issues.
Yes, sorry they binary number should be represented in the LCD, I haven't connected all the LED's because I was trying to get the 4 working and then scale-up with 16 LED's.
I haven't connected all the LED's yet, I was trying to get this done with 4 and then scale-up. What do you think that it's wrong with my schematics?
Does the LCD show the number in binary or decimal ?
If you have the LEDs arranged as you drew then how are the bits of the 16 bit number arranged in the drawing ? Where is bit 0, for instance ?
Welcome to the community. Programming involves struggling to exactly the extent that life itself does.
Neither has any particular need to involve struggling. Work, maybe, even hard work and time, perhaps more than you anticipated, will, however, be needed. For programming.
What code have you written so far, and where are you having to struggle?
What kinds of sketches have you written? What examples have you studied?
What you are attempting will be hard until it is easy. There is nothing especially hard about you project.
a7
In programming this is called "Sampling without replacement" and you can look it up... (You grab a random number, or name, or playing card, and you don't put it back so it can't be chosen again.)
I've done something like that (without the LCD) for a sound activated lighting effect... In my application a number of LEDs is chosen (you want 8) and then the pattern changes, always with the same number of LEDs on until the effect starts-over... .
Unfortunately, I don't have the code handy...
It might be a little confusing but if I remember correctly, it's something like this -
I start with a 16-bit integer initialized to zero. (The state of each bit represents the state of one LED.)
I initialize a "size" variable to 15. (There are 16 bits, but we start counting at "bit 0" and arrays start at "element 0".)
I make a 16-element array where each element contains it's own index value like this:
byte Array[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} ;
Next I set a random pointer between 0 and the "size".
Let's say I get "3"...
Bit 3 of the Integer is set to 1 (bitwise operations) so the 16-bit binary looks like this:
0000 0000 0000 0000 1000 (We're starting a bit 0 so bit-3 is the 4th bit from the right... everyday binary stuff.) At this point we'd have one LED on but we can display the LED pattern later after the pattern is done.
Now we shift (copy) the higher-values in the array to the left, starting at element 15 and overwriting the lower values, stopping after we get to element 3. So our array now looks like this:
{0,1,2,4,5,6,7,8,9,10,11,12,13,14,15,15}
3 is gone so we can't randomly choose 3 again. We can choose element 3 again, but now it contains the value "4".)
We decrement "size" to ignore the extra "15" at the end, start the loop over and find a new random number between 0 & 14, and continue.
After 8 loops, you'll have an integer with 8 random one's and 8 random zeros.
...Of course, I made a function to write each bit in the random integer to one I/O pin.
The advice you're getting may be beyond the problems you're having.
Your picture shows the LED protoboard wired extremely incorrectly - if the picture is accurate, you need to go back and review how the protoboards work...
According to the Fritzing diagram, no LEDs will ever light up. But then, those diagrams are always a waste of everyone's time.
Use a pencil and paper to draw out your wiring diagram, carefully labeling every part and every pin, and post a photo.
Put the numbers 0 through 15 into an array of size 16.
Then pick two random numbers between 0 and 15, and swap those elements of the array.
Several thousand times.
Then use the first 8 elements of the array as the 8 LED numbers.
You wouldn't even need to reinitialise the array for subsequent rounds. Just keep scrambling the scrambled array.
a7
Other duplicate topic CLOSED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.
Continued cross posting could result in a time out from the forum.
Could you also take a few moments to [url=https://forum.arduino.cc/index.php?topic=710766.0]Learn How To Use The Forum[/url].
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.
And a big concern if he is actually wiring up a breadboard!
As soon as you post a first
own attempt
how to code it
the real support will start.
With your initial post just describing the wanted funcionality and a fritzing-picture that seems to be thrown together within a few minutes. The wiring demonstrates that you did not take time to understand how the connections on a breadboard are made.
All in all you presented yourself in a way that this question arises:
Does he want to finish this project with small effort of 1 or to 2 hours
through somebody else writing the code for everything?
If you want to correct your impression read this
Be the change you want to see in the world
best regards Stefan
Thank you for letting me know this aspect, I've tried some things here and there, since I don't have that many outputs for Arduino 16LED, 16x2 display I'm gonna use an Teensy bc it has more ports. For moment I came with a random function but it shows me every single time the same LEDs on!
I started looking for arrays and get through with a shuffle function and then to pick from that shuffled array 8 values. When I'm done with this Im gonna instal the Liquid display and dismount some led just for proof of work. I'm simulating on tinkerCad the whole Thing!
int led_1 = 6;
int led_2 = 7;
int led_3 = 8;
int led_4 = 9;
int led_5 = 2;
int led_6 = 3;
int led_7 = 4;
int led_8 = 5;
int led_9 = 10;
int led_10 = 11;
int led_11 = 12;
/*
int led_12 = i;
int led_13 = i;
int led_14 = i;
int led_15 = i;
int led_16 = i;
*/
int buttonPin_1 = 13;
//int randNumber;
//int led =16;
void setup()
{
pinMode(buttonPin_1, INPUT_PULLUP);
pinMode(led_1, OUTPUT);
pinMode(led_2, OUTPUT);
pinMode(led_3, OUTPUT);
pinMode(led_4, OUTPUT);
pinMode(led_5, OUTPUT);
pinMode(led_6, OUTPUT);
pinMode(led_7, OUTPUT);
pinMode(led_8, OUTPUT);
pinMode(led_9, OUTPUT);
pinMode(led_10, OUTPUT);
pinMode(led_11, OUTPUT);
/*
pinMode(led_12, OUTPUT);
pinMode(led_13, OUTPUT);
pinMode(led_14, OUTPUT);
pinMode(led_15, OUTPUT);
pinMode(led_16, OUTPUT);
*/
}
void loop()
{
if(digitalRead(buttonPin_1) == HIGH)
{
digitalWrite(random(6,12), HIGH);
}
}
You are just switching LEDs on
This code runs through very fast so it takes 0,1 seconds until all numbers 6,7,8,9,10,11,12 have been choosen by the random function.
You should start to learn how to add serial output to your code for debugging purposes
Be the change you want to see in the world
best regards Stefan
but never off
That is the reason why all LEDs light up
I'm going to research on this! Thank you!
Take a look into this tutorial:
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.
Be the change you want to see in the world
best regards Stefan
Hello toolsos
To have a Q&D solution:
void loop() {
if(digitalRead(buttonPin_1) == HIGH) {
digitalWrite(random(6,12), random(0,2));
}
}
Have a nice day and enjoy coding in C++.
I do have a little question. So what have I done so far it's lighting up led, now 8, but my problem is that sometimes can light up 8 sometimes can light up 6 or whatever, I do think that random function choose sometimes the same value twice, how can I do If one its lighted up now to count for the other random function? Thank you!
int ledPins[] = {6, 7, 8, 9, 2, 3, 4, 5, 10, 11, 12};
int pinCount = 11; // additional ones would be added
int led_1 = 6;
int led_2 = 7;
int led_3 = 8;
int led_4 = 9;
int led_5 = 2;
int led_6 = 3;
int led_7 = 4;
int led_8 = 5;
int led_9 = 10;
int led_10 = 11;
int led_11 = 12;
/*
int led_12 = 9;
int led_13 = 6;
int led_14 = 7;
int led_15 = 8;
int led_16 = 9;
*/
int buttonPin_1 = 13;
void setup()
{
pinMode(buttonPin_1, INPUT_PULLUP);
pinMode(led_1, OUTPUT);
pinMode(led_2, OUTPUT);
pinMode(led_3, OUTPUT);
pinMode(led_4, OUTPUT);
pinMode(led_5, OUTPUT);
pinMode(led_6, OUTPUT);
pinMode(led_7, OUTPUT);
pinMode(led_8, OUTPUT);
pinMode(led_9, OUTPUT);
pinMode(led_10, OUTPUT);
pinMode(led_11, OUTPUT);
/*
pinMode(led_12, OUTPUT);
pinMode(led_13, OUTPUT);
pinMode(led_14, OUTPUT);
pinMode(led_15, OUTPUT);
pinMode(led_16, OUTPUT);
*/
}
void loop()
{
if(digitalRead(buttonPin_1) == HIGH)
{
digitalWrite(random(1,12),HIGH);
delay(1);
digitalWrite(random(1,12),HIGH);
delay(1);
digitalWrite(random(1,12),HIGH);
delay(1);
digitalWrite(random(1,12),HIGH);
delay(1);
digitalWrite(random(1,12),HIGH);
delay(1);
digitalWrite(random(1,12),HIGH);
delay(1);
digitalWrite(random(1,12),HIGH);
delay(1);
digitalWrite(random(1,12),HIGH);
delay(1);
}
}