Author Topic: Split up arrays for leds  (Read 3630 times)

0 Members and 1 Guest are viewing this topic.

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Split up arrays for leds
« on: August 14, 2012, 06:47:42 pm »
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. Simples


Code: [Select]
void loop() {

  for(count=0;count<3;count++) {
    digitalWrite(pinArray[count], HIGH);

    digitalWrite(pinArray[count], HIGH);

    digitalWrite(pinArray[count], HIGH);
    delay(200);



    for(count2=0;count2<5;count2++) {
      digitalWrite(pinArray2[count2], HIGH);
      delay(700);
      digitalWrite(pinArray2[count2], LOW);
delay(100);

    }
  }
}
 

Offline michaelym

  • Regular Contributor
  • *
  • Posts: 65
Re: Split up arrays for leds
« Reply #1 on: August 15, 2012, 04:37:02 am »
Did you use Google translate? I can't figure out what you're saying.

I would love to help you
 

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Re: Split up arrays for leds
« Reply #2 on: August 15, 2012, 11:15:57 am »
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.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10239
  • Country: nz
Re: Split up arrays for leds
« Reply #3 on: August 15, 2012, 11:32:22 am »
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.
« Last Edit: August 15, 2012, 11:37:07 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Things

  • Regular Contributor
  • *
  • Posts: 224
  • Country: au
  • Laser Geek
    • NQLasers
Re: Split up arrays for leds
« Reply #4 on: August 15, 2012, 11:39:20 am »
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/BlinkWithoutDelay

You 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.
 

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Re: Split up arrays for leds
« Reply #5 on: August 15, 2012, 11:57:06 am »
Thanks. Yes I need to us millis() instead of delay. I read the BlinkwithoutDelay last night but couldnt get it work in my project.
 

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Re: Split up arrays for leds
« Reply #6 on: August 15, 2012, 12:31:18 pm »
Code: [Select]
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[count], OUTPUT);

    int count2;

    for (int count2 = 0; count2 < 5; count2++)  {
      pinMode(pinArray2[count2], OUTPUT);


    }
  }
}
void loop() {




  for(count=0;count<3;count++) {
    digitalWrite(pinArray[count], HIGH);

    digitalWrite(pinArray[count], HIGH);

    digitalWrite(pinArray[count], HIGH);
    delay(200);

}

    for(count2=0;count2<5;count2++) {
      digitalWrite(pinArray2[count2], HIGH);
      delay(700);
      digitalWrite(pinArray2[count2], LOW);
delay(100);
     





   
  }
}
 

Offline dda

  • Contributor
  • Posts: 46
  • Country: au
Re: Split up arrays for leds
« Reply #7 on: August 15, 2012, 01:04:29 pm »
Maybe...?

Code: [Select]
void loop() {

for(count=0;count<3;count++) {
  if(millis()-timer >= count*1000 && millis()-timer < (count+1)*1000){
    digitalWrite(pinArray[count], HIGH);
  }
  else {
     pinArray[count], 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[count2], HIGH);
  }
  else {
    pinArray2[count2], LOW);
  }
}

if(millis()-timer2>10000){
  timer2=millis();
}

}
« Last Edit: August 15, 2012, 01:06:03 pm by dda »
 

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Re: Split up arrays for leds
« Reply #8 on: August 15, 2012, 01:14:38 pm »
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.
 

Offline dda

  • Contributor
  • Posts: 46
  • Country: au
Re: Split up arrays for leds
« Reply #9 on: August 15, 2012, 01:40:05 pm »
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.

Changed for loops count and count2 shouldnt change. This verifies, but i dont have a circuit to test it.

Code: [Select]
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[i], OUTPUT);
  }


  for (int i = 0; i < count2; i++)  {
    pinMode(pinArray2[i], OUTPUT);


  }
}

void loop() {

  for(int i=0;i<count;i++) {
    if(millis()-timer >= i*1000 && millis()-timer < (i+1)*1000){
      digitalWrite(pinArray[i], HIGH);
    }
    else {
      digitalWrite(pinArray[i], 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[i], HIGH);
    }
    else {
      digitalWrite(pinArray2[i], LOW);
    }
  }

  if(millis()-timer2>10000){
    timer2=millis();
  }
}
 

Offline c3d20Topic starter

  • Contributor
  • Posts: 18
Re: Split up arrays for leds
« Reply #10 on: August 15, 2012, 06:54:30 pm »
This works great. Im also trying TimerOne.h script
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf