Split up arrays for leds - Page 1
Forum section: electronics / beginners

author: c3d20
author_rank: Contributor
timestamp: August 14, 2012, 06:47:42 pm
content: How do I get these two arrays to run independently as they are running together, 1st light comes on of 1st array and so does the 1st of 2nd array 2nd light comes on after 2nd array has finsh the 5 pin leds. Im also going to be adding a button to 2nd array that interacts with 1st array using if. SimplesCode:void loop() {for(count=0;count<3;count++) {digitalWrite(pinArray, HIGH);digitalWrite(pinArray, HIGH);digitalWrite(pinArray, HIGH);delay(200);for(count2=0;count2<5;count2++) {digitalWrite(pinArray2, HIGH);delay(700);digitalWrite(pinArray2, LOW);delay(100);}}}

author: michaelym
author_rank: Regular Contributor
timestamp: August 15, 2012, 04:37:02 am
content: Did you use Google translate? I can't figure out what you're saying.I would love to help you

author: c3d20
author_rank: Contributor
timestamp: August 15, 2012, 11:15:57 am
content: How do I get pinArray first led on, second led on, 3rd led on, then all off, then after 6 seconds start again,while PinArray2 is going through its led display unaffected by pinArray. At the moment they are not a seperate led display pinArray and PinArray2. What happens is the first led of pinArray stays on then pinArray2 does a loop through the 5 pins then second led of pinArray and so forth.

author: Psi
author_rank: Super Contributor
timestamp: August 15, 2012, 11:32:22 am
content: Only one instruction can happen at a time. You need to stop using the delay function.Using the delay function stops the CPU from doing anything while it's waiting.Instead of using "delay()" let all of your main loop repeat as fast as it can and use one of the hardware timers for the delay.Enable the hardware timer so it starts counting in the background, then you can do other things and also keep rechecking the timer to see where its up to.Once it's up to a point that equals 6 seconds you can do extra code.

author: Things
author_rank: Regular Contributor
timestamp: August 15, 2012, 11:39:20 am
content: Grab the value of millis(), store it to a variable, then compare it to a number with your required time added instead of delay. There is an example if this here:http://arduino.cc/en/Tutorial/BlinkWithoutDelayYou can also use hardware timers/interrupts, but probably overkill for your application as it'll also disable things like PWM on some pins. Then again we don't know your application, so can't make a comment on the best way to do it.

author: c3d20
author_rank: Contributor
timestamp: August 15, 2012, 11:57:06 am
content: Thanks. Yes I need to us millis() instead of delay. I read the BlinkwithoutDelay last night but couldnt get it work in my project.

author: c3d20
author_rank: Contributor
timestamp: August 15, 2012, 12:31:18 pm
content: Code:int pinArray[] = {3,5,6};int count = 3;int pinArray2[] = {9,10,11,7,12};int count2 = 5;int timer = 1000;void setup() {int count;for (int count = 0; count < 3; count++) {pinMode(pinArray, OUTPUT);int count2;for (int count2 = 0; count2 < 5; count2++) {pinMode(pinArray2, OUTPUT);}}}void loop() {for(count=0;count<3;count++) {digitalWrite(pinArray, HIGH);digitalWrite(pinArray, HIGH);digitalWrite(pinArray, HIGH);delay(200);}for(count2=0;count2<5;count2++) {digitalWrite(pinArray2, HIGH);delay(700);digitalWrite(pinArray2, LOW);delay(100);}}

author: dda
author_rank: Contributor
timestamp: August 15, 2012, 01:04:29 pm
content: Maybe...?Code:void loop() {for(count=0;count<3;count++) {if(millis()-timer >= count*1000 && millis()-timer < (count+1)*1000){digitalWrite(pinArray, HIGH);}else {pinArray, LOW);}}if(millis()-timer>6000){timer=millis();}for(count2=0;count2<5;count2++) {if(millis()-timer2 >= count2*1000 && millis()-timer2 < (count2+1)*1000){digitalWrite(pinArray2, HIGH);}else {pinArray2, LOW);}}if(millis()-timer2>10000){timer2=millis();}}

author: c3d20
author_rank: Contributor
timestamp: August 15, 2012, 01:14:38 pm
content: Can you Include the full code as Im having trouble compiling into sketch. Thank you. I understand how it works I just cant get it working.

author: dda
author_rank: Contributor
timestamp: August 15, 2012, 01:40:05 pm
content: Quote from: c3d20 on August 15, 2012, 01:14:38 pmChanged for loops count and count2 shouldnt change. This verifies, but i dont have a circuit to test it.Code:int pinArray[] = {3,5,6};int count = 3;int pinArray2[] = {9,10,11,7,12};int count2 = 5;int timer;int timer2;void setup() {for (int i = 0; i < count; i++) {pinMode(pinArray, OUTPUT);}for (int i = 0; i < count2; i++) {pinMode(pinArray2, OUTPUT);}}void loop() {for(int i=0;i<count;i++) {if(millis()-timer >= i*1000 && millis()-timer < (i+1)*1000){digitalWrite(pinArray, HIGH);}else {digitalWrite(pinArray, LOW);}}if(millis()-timer>6000){timer=millis();}for(int i=0;i<count2;i++) {if(millis()-timer2 >= i*1000 && millis()-timer2 < (i+1)*1000){digitalWrite(pinArray2, HIGH);}else {digitalWrite(pinArray2, LOW);}}if(millis()-timer2>10000){timer2=millis();}}

author: c3d20
author_rank: Contributor
timestamp: August 15, 2012, 06:54:30 pm
content: This works great. Im also trying TimerOne.h script