1291Views31Replies

Author Options:

2 sequences with arduino? Answered

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

Tags:arduino

32 Replies

user
steveastroukBest Answer (author)2012-12-23
This code works

/*The circuit:* LEDs from pins 2 through 13 to ground*/// these constants won't change:constint ledCount = 12; // the number of LEDs in the bar graphint 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 pinsint Ledsequence[] = {1,2,4,8,16,32,32,16,8,4,2,1}; //This is the flashing sequenceint Randomnumber =0;
int Tseq = 0;


voidsetup() {
// 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));
}

voidloop() {

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);
}

}

Select as Best AnswerUndo Best Answer

user
slowguy (author)2012-12-18

it loaded but nothing happened

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)slowguy2012-12-19

Moral: check your simulator really is doing what you think it is.

Try the NEW version above.

Steve

Select as Best AnswerUndo Best Answer

user
slowguy (author)steveastrouk2012-12-19

13 through 8 just flash together at a random rate and 9 is off, does nothing. 7 through 2 are on solid.

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)slowguy2012-12-19

Works on my hardware. Check your wiring, write a piece of test code that chases all the leds in sequence.

Select as Best AnswerUndo Best Answer

user
slowguy (author)steveastrouk2012-12-20

one last request. could you please resend the complete code to me one more time. I would appreciate it. Thanks

Select as Best AnswerUndo Best Answer

user
slowguy (author)steveastrouk2012-12-19

I miss ed 2 other things you tolf me to do, change the count to 12 and change the loop. Now the leds flash.

Select as Best AnswerUndo Best Answer

user
slowguy (author)steveastrouk2012-12-19

I do not know what to say. I have loaded my 12 output randonizer and 12 output sequencer and they both work on either the nano and uno.

Select as Best AnswerUndo Best Answer

user
slowguy (author)steveastrouk2012-12-19

sorry it is 12 instead of 9 and it is on dim

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)slowguy2012-12-19

Also, there is an extra ; in the for loop here.
for (ctr=0;ctr<11;ctr++);

delete the last ;

Either change the 11 to 12 or make it
for (ctr=0;ctr

Select as Best AnswerUndo Best Answer

user

Corrupted by the website

for (ctr=0;ctr < ledCount; ctr++)

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)slowguy2012-12-19
user
steveastrouk (author)slowguy2012-12-19

Try commenting out the line randomseed(analogread(0) ) - I added that after iI tested it.

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)2012-12-19

/*

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);
}

}

Select as Best AnswerUndo Best Answer

user
slowguy (author)steveastrouk2012-12-16

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

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)slowguy2012-12-16

What sequence ? Random I can handle. I'll take a look at it, but I am very busy at work at the moment.

Select as Best AnswerUndo Best Answer

user
slowguy (author)steveastrouk2012-12-16

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

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)slowguy2012-12-17

10000
01000
00100
00010
00001
00010
00100
01000
10000
??

Select as Best AnswerUndo Best Answer

user
slowguy (author)steveastrouk2012-12-17

that is correct, then repeat

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)slowguy2012-12-18

What kind of speed are you looking for ?
One change a second ? Or what ?
Steve

Select as Best AnswerUndo Best Answer

user
slowguy (author)steveastrouk2012-12-18
user
steveastrouk (author)slowguy2012-12-18

Is this all it needs to do ?

Select as Best AnswerUndo Best Answer

user
slowguy (author)steveastrouk2012-12-18

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

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)slowguy2012-12-18
user
frollard (author)2012-12-16

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

Select as Best AnswerUndo Best Answer