Hello i was wondering how i can do a 8x8 single layer 64 leds to randomly turn on and off I'm currently using the arduino mega. I only know how to turn off 3 to 4 of them in loop with high and low. But how can i do all 64 without putting without writting all 64 high and low? can someone help me out and point me in the right direction please?
A single MAX7219 chip can control 64 LEDs.
You could also maybe get one of these 8x8 dot matrix modules, that includes the chip and a 8x8 led matrix.
Hello Hazardsmind thank you for the reply but i was trying to do it without having to have something extrea on it. i have it all wired up i can turn on and off all the leds i just did a basic high and low in the sketch. I have a mosfet on all the anodes side for the extrea power boos i can turn them all on at the same time the cathode side of the leds goes to 220ohms resistors and from the other side of the resistor goes to the the mega pins. but Trying to figure out a way to randomly in sketch code how to generate a random pattern of some kind.
let me rephrase that on the mosfet side i have a mosfet follow up with a 2n2222 transistor for the anodes side for the extrea powersource coming in is a 5v 2a supply more then enough.
You could make a simple code that makes 8 random bytes and then goes through each bit of the 8 bytes, that will then turn on or off the respective LEDs.
What does the wiring look like, pin wise?
Well all the anodes are together and they are connected to the mosfets and transistor to pull in the 5v power from external source common ground to the arduino to be safe. all the cathodes 64 of them pins are going to 64 of the 220ohms resistors on a breadboard and from the other side of the resistors go to the digital pins on the arduino mega and A0 - A3 on the mega. i can power them up all at once or off and on like a blinking but that's all.
I do not know how to makes a 8 random bytes pattern i never did one before.
Are your pins in an array? if so, then good. If not then please do so, by either row or column.
Actually not needed.
Next you will need another 8 element byte array. This is what you will use to make the random patterns (random() ) per row or column.
Finally, all you would need is 2 for loops and the bitread function, to read the bits in the 8 random bytes as well as of course the digitalWrite function.
Quick example:
int myLEDs[8][8] = {
{ ... }, // row 1
{ ... }, // row 2
{ ... },
{ ... },
{ ... },
{ ... },
{ ... },
{ ... } // row 8
};
byte Pattern;
void setup() {
// put your setup code here, to run once:
/* 2 for loops to set the pinModes */
}
void loop() {
// put your main code here, to run repeatedly:
for (byte Row = 0; Row < 8; Row++)
{
Pattern = random(255);
for (byte P = 0; P < 8; P++)
{
digitalWrite(myLEDs[Row][P], bitRead(Pattern, P) );
}
}
}
NOTE: You may want to add a delay to see the patterns.
LEt me explain how i made it like this. i was making the 8x8x8 but i found out i didn't have enough leds. so i made a few 4x4x4 but it left me with one of the 8x8x8 layers 64 leds all anodes soldier together and each of the cathodes was open so it can go to the resistors and to shift registers problem was i didn't have enough for shift registers so i only got 2 of them and i use them on another project of mine. so at the moment all is wired up to the mega.
Thats fine, I understand what you have.
I found this Sketch online to do 8 leds but i tried it and not really random it just goes 1 then 3 then back to 1 then 4 then 1 and then 5 like that. or something along them lines
int timer = 100; // The higher the number, the slower the timing.
int ledPins[] = {
2, 7, 4, 6, 5, 3, 8, 8 }; // an array of pin numbers to which LEDs are attached
int pinCount = 8; // the number of pins (i.e. the length of the array)
void setup() {
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}
I found this Sketch online to do 8 leds but i tried it and not really random
Have you tried my example code to see what it does?
Hello sorry i fell a sleep took my meds and passed out i did try yours and this is what i got
sketch_dec21a.ino:2:5: error: expected primary-expression before '...' token
sketch_dec21a.ino:3:5: error: expected primary-expression before '...' token
sketch_dec21a.ino:4:5: error: expected primary-expression before '...' token
sketch_dec21a.ino:5:5: error: expected primary-expression before '...' token
sketch_dec21a.ino:6:5: error: expected primary-expression before '...' token
sketch_dec21a.ino:7:5: error: expected primary-expression before '...' token
sketch_dec21a.ino:8:5: error: expected primary-expression before '...' token
sketch_dec21a.ino:9:5: error: expected primary-expression before '...' token
Error compiling.
You were supposed to put your pin numbers into those arrays, either by row or column. As well as set up the pinModes.
Hello i did this
int myLEDs[8][8] = {
{ 0,1,2,3,4,5,6,7 }, // row 1
{ 8,9,10,11,12,13,14,15 }, // row 2
{ 16,17,18,19,20,21,22,23 },
{ 24,25,26,27,28,29,30,31 },
{ 32,33,34,35,36,37,38,39 },
{ 40,41,42,43,44,45,46,47 },
{ 48,49,50,51,52,53,54,55 },
{ 57,58,59,60,61,62,63,64 } // row 8
};
byte Pattern;
void setup() {
// put your setup code here, to run once:
/* 2 for loops to set the pinModes */
}
void loop() {
// put your main code here, to run repeatedly:
for (byte Row = 0; Row < 8; Row++)
{
Pattern = random(255);
for (byte P = 0; P < 8; P++)
{
digitalWrite(myLEDs[Row][P], bitRead(Pattern, P) );
}
}
}
and i did compile it and it did compile with no problem. i will try in a hour putting up christmas tree now thank you i will keep update in here.
Double check your last row - missing #56 (unless you want it that way).
Hello dlloyd you are correct thank you i haven't notice that. i will try that after i finish the christmas tree thanks
You also didn't put in the pinModes for the LEDs either. If they are all sequential then all you need is one FOR loop.
Add this:
for(byte i = 0; i < 64; i++)
pinMode(i, OUTPUT);
Hello all sorry i been so busy with the holidays here and family and wife i did try the Sketch the leds did come on made random pattern looks awesome thank you i did the trick.