Project question

New to Arduino.
I've been working on a project for my cosplay.
Trying to run multiple LED's with several different patterns.
LED 1, 2, 3, and 4 will be in a square. I'd like them to chase each other in a circle pattern. Each would turn on for one second before turning off as next turns out.
LED 5,6,7, and 8 will be in a square as well. Led 5 would turn on 1 second each on seconds 2 and 6. 6 and 7 would stay on accept on second 7. 8 would blink 1 second on 1 off.
A 9th LED will stay on and turn off for 1 second on second 1 and 5.

My question is...
Is it possible to start a millisecond counter that loops every 8 seconds?

Then use "if" to control each LED. Example:
If ((x >= 0000) && (x <= 999))
digitalWrite(Led1, HIGH);
If ((x >= 1000) && (x <= 1999))
digitalWrite(Led2, HIGH);
If ((x >= 2000) && (x <= 2999))
digitalWrite(Led3, HIGH);
If ((x >= 3000) && (x <= 3999))
digitalWrite(Led4, HIGH);

ETC.

I found a tutorial that almost works for LED 5 to 9. But it only allows me to start the PIN either HIGH or LOW and enter only 1 value each for HIGH duration and LOW duration. But doesn't use the delay variable so it runs all the led patterns (effectively) simultaneously.

I've spent hours reading tutorials, looking at YouTube videos and read half a book. And haven't been able to figure this out.

I tried using 4 pins with the tutorial sketch, each providing half the power to 2 leds each in different patterns:

PIN1: provide half power to LED1 & LED3 HIGH a 1000 milliseconds, 1000 milliseconds LOW starting HIGH.
PIN2: half power to LED2 & LED4, 1000 LOW, 1000 HIGH, starting LOW.
PIN3: half power to LED1 & LED2, 2000 HIGH, 2000 LOW, starting HIGH.
PIN4: half power to LED3 & LED4, 2000 LOW, 2000 HIGH, starting LOW.
This should have caused LED1 to receive full power (half from PIN1, half from PIN3) while LED2 and LED4 received half power each. Then PIN1 would turn LOW and PIN2 would switch HIGH GIVING full power to LED2 (half from PIN2, half from PIN3) while LED1 and LED4 received half power. Etc.
The LEDs claim they need 2.0 to 2.4v and 20mA. Since the arduino outputs 5v, I tried 470ohm resistors. But 3 LEDs light at all times with some brighter then others and the one off, switches between 2 and 3. (470 ohm should cut the 5v down to about 1v, 10mA which shouldn't be enough to light them.)

I'm not asking any one to write the sketch for me.
Just, is it possible to create a millisecond timer that loops every 8 seconds (from 0000 milliseconds to 7999 milliseconds), and use the "if" variable to switch Leds on at specific millisecond windows?

If it is, could you point me in the right direction of what functions to research?

I appreciate any and all help!

Just, is it possible to create a millisecond timer that loops every 8 second

No.
But it is possible to achieve the same effect using the millis() function.
Their are lots of examples of how to do this. One is on the IDE called blink without delay.
Basically you copy the start time into a variable and use that as the referance point. Then subtract this from the current time to give you how far into your 8 second loop you are. Then use this value in your if statements to control what you do.

This whole technique is called a state machine. Again lots of examples on line here is one http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

You could put this at the start of loop(), before your code above:

unsigned long x = millis() % 8000;

Also I can't figure out what you mean by

This should have caused LED1 to receive full power (half from PIN1, half from PIN3) while LED2 and LED4 received half power each.

You will have to draw a diagram of that for us to understand what you mean.

Thank you Grumpy.
Ill look up State Machine examples!

Grumpy_Mike:
No.
But it is possible to achieve the same effect using the millis() function.
Their are lots of examples of how to do this. One is on the IDE called blink without delay.
Basically you copy the start time into a variable and use that as the referance point. Then subtract this from the current time to give you how far into your 8 second loop you are. Then use this value in your if statements to control what you do.

This whole technique is called a state machine. Again lots of examples on line here is one http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

Thanks Paul.
Gives me a good place to start!

Second part.
Its kinda hard to describe. And my tablet doesn't have a good drawing app.
Ill see if this makes sense.

PINS A, B, C, D. used to power LED 1,2,3,4.
PIN LED connected. (~ = 470 ohm resistor)
A----~---1
--~---3

B----~---2
--~---4

C----~---1
--~---2

D----~---3
--~---4
**I'm using jumpers to crisscross these connections on the breadboard so there's no shorts.

PIN. SECONDs: 1,2,3,4 (1 is HIGH, 0 is LOW)
A: 1,01,0
B: 0,1,0,1
C: 1,1,0,0
D: 0,0,1,1
So, on second 1, both PIN A and PIN C will be set to HIGH.
Referencing the first chart, we see that PIN A should be providing 1v 10mA to LED 1 and LED 3. At the same time PIN C will be providing LED 1 and LED 2 each 1v 10mA. So LED 1 should be getting 2v 20mA, and be the only LED on. (1v 10mA each from PIN A and C.) LED 2 is getting 1v 10mA from PIN C. LED 3 is getting 1v 10mA from PIN A.

On second 2, PIN A is switched to LOW. PIN C remains set to HIGH.
PIN B switches to HIGH. PIN B gives 1v 10mA to LED 2 and LED 4. PIN C still giving 1v 10mA to LED 1 and LED 2. So that's 1v 10mA to LED 1, 2V 20mA to LED 2 and 1v 10mA to LED 4. So LED 2 should be on.

At second 3, PIN B and C turn LOW, PIN A and D turn HIGH.
LED 3 should receive 2v 20mA and turn on. LED 1 and LED 4 are getting 1v 10mA each.
At Second 4, PIN A turns LOW, and PIN B turns HIGH. PIN D is still set HIGH.
LED 4 should now get 2v 20mA and turn on. LED 2 and LED 3 are each getting 1v 10mA.

That make sense?

PaulRB:
You could put this at the start of loop(), before your code above:

unsigned long x = millis() % 8000;

Also I can't figure out what you mean byYou will have to draw a diagram of that for us to understand what you mean.

Hmm... If I have understood you correctly,

  • DON'T DO THIS YOU WILL DAMAGE YOUR ARDUINO. You will be connecting output pins together and setting some high and some low, causing short-circuits
  • I don't know why you want to do this. 4 pins, 4 LEDs. Why the complicated connections?

After hitting the wall trying to come up with a sketch that worked, I tried to come up with a physical workaround.
I've been trying to figure this out for 2 weeks now.
Once all the workarounds failed, I was frustrated enough to swallow my pride and ask for a little help here.

I've done similar workarounds with just switches and they worked. But there must be something different with the power output of the arduino. The fact people can power an LED with a 1k resistor should have been my first clue that it must be doing something weird. 2v 20mA LED shouldn't work with a 1k resistor between it and a 5v source.

PaulRB:
Hmm... If I have understood you correctly,

  • DON'T DO THIS YOU WILL DAMAGE YOUR ARDUINO. You will be connecting output pins together and setting some high and some low, causing short-circuits
  • I don't know why you want to do this. 4 pins, 4 LEDs. Why the complicated connections?

I didn't understand most of that post. You're talking about stuff you haven't told us about, and without schematics & code it's impossible to say what you might have done wrong.

For the patterns you talked about in your first post, just connect each led to one pin (with a series resistor of course). Try that and if you can't make the patterns you want, post your best attempt at the code and explain what it is doing and what you want it to do.

Yes, a 2V led will work with a 1K series resistor, not sure what point you are trying to make there. It won't get 20mA but it will still light. The current will be (5V - 2V) / 1000 = 0.003A = 3mA

Please stop quoting other posts in their entirety when you reply. That adds nothing. If you want to refer to something specific, just quote that part.

Thank you for your help guys!

With the point in the right direction, I was able to come up with this sketch and it works so far as is. Ill need to experiment adding the additional LED'S.
I know I can optimize the sketch more by removing the ranges on the LOW to "if it equals" like I did with the 2nd iteration for LED4. I could probably compress the code a little by integrating state machines for each LED as well.

int led1 = 19;
int led2 = 18;
int led3 = 12;
int led4 = 11;

void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}

void loop()
{
unsigned long x = millis() % 8000;
if ((x >= 0) && (x <= 999))
{
 digitalWrite(led1, HIGH);   //turns led1 on
}
  if ((x >= 1000) && (x <= 1999))
  {
   digitalWrite(led1, LOW);   //turn led1 off
  }

if ((x >= 1000) && (x <= 1999))
{
 digitalWrite(led2, HIGH);
}
  if ((x >= 2000) && (x <= 2999))
  {
   digitalWrite(led2, LOW);
  }

if ((x >= 2000) && (x <= 2999))
{
 digitalWrite(led3, HIGH);
}
  if ((x >= 3000) && (x <= 3999))
  {
   digitalWrite(led3, LOW);
  }

if ((x >= 3000) && (x <= 3999))
{
 digitalWrite(led4, HIGH);
}
  if ((x >= 4000) && (x <= 4999))
  {
   digitalWrite(led4, LOW);
  }

if ((x >= 4000) && (x <= 4999))
{
 digitalWrite(led1, HIGH);
}
  if ((x >= 5000) && (x <= 5999))
  {
   digitalWrite(led1, LOW);
  }

if ((x >= 5000) && (x <= 5999))
{
 digitalWrite(led2, HIGH);
}
  if ((x >= 6000) && (x <= 6999))
  {
   digitalWrite(led2, LOW);
  }

if ((x >= 6000) && (x <= 6999))
{
 digitalWrite(led3, HIGH);
}
  if ((x >= 7000) && (x <= 8000))
  {
   digitalWrite(led3, LOW);
  }

if ((x >= 7000) && (x <= 7999))
{
 digitalWrite(led4, HIGH);
}
  if (x == 8000)
  {
   digitalWrite(led4, LOW);
  }
}

Three things wrong with your code:

  1. It's not in code tags. That's how you are supposed to post code on this forum. You should edit your above post and put them in, before the forum moderators find it. Read #7 of the forum guidelines to find out how.

  2. This line:

if (x = 8000)

When you code in Arduino/C/C++, a single "=" means "set x to this value". Double "==" means "compare x to this value".

  1. x will never be 8000. After 7999 it will go back to 0.

Thank you again Paul.
I was a little too excited and forgot the code tags.
And can't believe I missed the second "="! Good catch!

Have you thought about using a 3x3 matrix as a solution, which would use 9 pins on your Arduino, and you can then write the code to make them do as you require. You wire them and code them as a 3x3, but you can physically put the 9 LEDs in any pattern you wish, as per your post.

Another suggestion is multiplexing a 3x3 matrix, which would use 6 pins on your Arduino 3 for the rows and 3 for the columns.

Or my third suggestion is using a 74HC595 Shift Register to multiplex LEDs 1 to 8 in a 4x2 matrix using 3 pins on the Arduino, and LED 9 can be connected to another pin and programmed separately to the other 8. So your main program would be multiplexing a 4x2 matrix and also controlling a 9th LED.