loading

2 sequences with arduino?

I am a beginner and done a few of the beginner projects and have learned a little. My question is can you run 2 sequences at the same time with an arduino? Here is a example, I would like to run 6 leds random and the other 6 in a sequence both going on at the same time. Is that possible?
John

sort by: active | newest | oldest
This code works

/*

The circuit:
* LEDs from pins 2 through 13 to ground
*/


// these constants won't change:
const int ledCount = 12; // the number of LEDs in the bar graph

int ledpins[] = {
2, 3, 4, 5, 6, 7,8,9,10,11,12,13 }; // an array of pin numbers to which LEDs are attached
//First 6 pins are the steady sequence pins
//second six are the random number pins
int Ledsequence[] = {1,2,4,8,16,32,32,16,8,4,2,1}; //This is the flashing sequence
int Randomnumber =0;
int Tseq = 0;


void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
   pinMode(ledpins[thisLed], OUTPUT);
   digitalWrite(ledpins[thisLed],LOW);
  }
  
randomSeed (analogRead(0));
}

void loop() {

for (int ctr=0;ctr<ledCount;ctr++)
{
Tseq = Ledsequence[ctr];

if ((Tseq & 1)==1) {digitalWrite(ledpins[0], HIGH);} else {digitalWrite(ledpins[0],LOW);}
if ((Tseq & 2)==2) {digitalWrite(ledpins[1],HIGH);} else {digitalWrite(ledpins[1] ,LOW);}
if ((Tseq & 4)==4) {digitalWrite(ledpins[2],HIGH);} else {digitalWrite(ledpins[2] ,LOW);}
if ((Tseq & 8)==8) {digitalWrite(ledpins[3],HIGH);} else {digitalWrite(ledpins[3] ,LOW);}
if ((Tseq & 16)==16) {digitalWrite(ledpins[4],HIGH);} else {digitalWrite(ledpins[4] ,LOW);}
if ((Tseq & 32)==32) {digitalWrite(ledpins[5],HIGH);} else {digitalWrite(ledpins[5] ,LOW);}
Randomnumber = random (33);

if ((Randomnumber & 1)==1) {digitalWrite(ledpins[6] ,HIGH);} else {digitalWrite(ledpins[6] ,LOW);}
if ((Randomnumber & 2)==2) {digitalWrite(ledpins[7] ,HIGH);} else {digitalWrite(ledpins[7] ,LOW);}
if ((Randomnumber & 4)==4) {digitalWrite(ledpins[8] ,HIGH);} else {digitalWrite(ledpins[8] ,LOW);}
if ((Randomnumber & 8)==8) {digitalWrite(ledpins[9] ,HIGH);} else {digitalWrite(ledpins[9] ,LOW);}
if ((Randomnumber & 16)==16) {digitalWrite(ledpins[10] ,HIGH);} else {digitalWrite(ledpins[10] ,LOW);}
if ((Randomnumber & 32)==32) {digitalWrite(ledpins[11] ,HIGH);} else {digitalWrite(ledpins[11] ,LOW);}
delay(100);
}

}

/*

The circuit:
* LEDs from pins 2 through 13 to ground
*/


// these constants won't change:
const int ledCount = 10; // the number of LEDs in the bar graph

int ledpins[] = {
2, 3, 4, 5, 6, 7,8,9,10,11,12,13 }; // an array of pin numbers to which LEDs are attached
//First 6 pins are the steady sequence pins
//second six are the random number pins
int Ledsequence[] = {1,2,4,8,16,32,32,16,8,4,2,1}; //This is the flashing sequence
int Randomnumber =0;
int Tseq = 0;
int ctr =0 ;

void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledpins[thisLed], OUTPUT);
}
randomSeed (analogRead(0));
}

void loop() {
for (ctr=0;ctr<11;ctr++);
{
Tseq = Ledsequence[ctr];
if (Tseq & 1==1) {digitalWrite(ledpins[0], HIGH);} else {digitalWrite(ledpins[0],LOW);}
if (Tseq & 2==2) {digitalWrite(ledpins[1],HIGH);} else {digitalWrite(ledpins[1] ,LOW);}
if (Tseq & 4==4) {digitalWrite(ledpins[2],HIGH);} else {digitalWrite(ledpins[2] ,LOW);}
if (Tseq & 8==8) {digitalWrite(ledpins[3],HIGH);} else {digitalWrite(ledpins[3] ,LOW);}
if (Tseq & 16==16) {digitalWrite(ledpins[4],HIGH);} else {digitalWrite(ledpins[4] ,LOW);}
if (Tseq & 32==32) {digitalWrite(ledpins[5],HIGH);} else {digitalWrite(ledpins[5] ,LOW);}
Randomnumber = random (32);
if (Randomnumber & 1==1) {digitalWrite(ledpins[6] ,HIGH);} else {digitalWrite(ledpins[6] ,LOW);}
if (Randomnumber & 2==2) {digitalWrite(ledpins[7] ,HIGH);} else {digitalWrite(ledpins[7] ,LOW);}
if (Randomnumber & 4==4) {digitalWrite(ledpins[8] ,HIGH);} else {digitalWrite(ledpins[8] ,LOW);}
if (Randomnumber & 8==8) {digitalWrite(ledpins[9] ,HIGH);} else {digitalWrite(ledpins[9] ,LOW);}
if (Randomnumber & 16==16) {digitalWrite(ledpins[10] ,HIGH);} else {digitalWrite(ledpins[10] ,LOW);}
if (Randomnumber & 32==32) {digitalWrite(ledpins[11] ,HIGH);} else {digitalWrite(ledpins[11] ,LOW);}
delay(1000);
}

}
Yes.
slowguy (author)  steveastrouk4 years ago
Hi steve,
Could you write me a program to run 2, 3, 4, 5, 6, 7 in a sequence and 8, 9, 10, 11, 12, 13 as random. I am making a xmas gift for my wife and I have limit time. I would very much appreciate it as I have to construct the object the lights are going in.
Thanks,
John
What sequence ? Random I can handle. I'll take a look at it, but I am very busy at work at the moment.
slowguy (author)  steveastrouk4 years ago
a sequence of flash 2 then 3 then 4 then 5 then 6 then 7 and 6 and 5 and 4 and 3 and 2 and then repeat.
Thanks
10000
01000
00100
00010
00001
00010
00100
01000
10000
??
slowguy (author)  steveastrouk4 years ago
that is correct, then repeat
What kind of speed are you looking for ?
One change a second ? Or what ?
Steve
slowguy (author)  steveastrouk4 years ago
yes, that will be good.
Is this all it needs to do ?
slowguy (author)  steveastrouk4 years ago
yes, while 6 leds, 8 through 11 outputs blink randomly, the other 6 leds , 2 though 7 outputs blink in sequence, 2 through 7, then 7 through 2 and repeat continuiously.
Thanks
Which Arduino version ?
slowguy (author)  steveastrouk4 years ago
Nano, V3
frollard4 years ago
Since at a beginner level you cannot have multiple processes or timers to increment your random values, you need something a bit more complex to do 'independent' actions. There are a couple methods for this,
You need to use ARRAYS -- learn them. An array is a series of variables that share a name, and each individual element can be addressed with an index number.
for example
int leds[6] = {3,4,5,6,7,8};

This will declare an array of integers called leds, with 6 elements, and their initial values will be 3, 4, 5, 6, 7, 8. Those values can be any integers. These would make good pin numbers (with leds attached)

Later, if I use the variable
leds[x] in a function, it will pull the VALUE of that number, the indexes start counting at zero.
i.e.
leds[3] == 6.
If they update at the same time, its easy, you just loop through your leds with something like
(pseudocode explaining thought process)
for i = 0, 0 <7, i++
turn all leds off;
IncrementalLed[i] = on //list of leds in order
RandomLed[random(0,5)] = on //list of leds that don't matter what order
delay(100)
end for i