blinling LEDS IN A SEQUENCE

how can I do this pattern to blink the LEDs.

Here's OP's pic.

How to attach an image, OP.

The simplest but ugliest and least elegant solution would be to just:

  • digitalRead() dipswitch2 (dipswitch1 being redundant, unless you have more patterns up your sleeve?)
  • If dipswitch is OFF:
  • digitalWrite() the leds for pattern1 state 0
  • delay a bit
  • digitalWrite() the leds for pattern1 state 1
  • repeat until state 5*
  • If dipswitch is ON:
  • digitalWrite() the leds for pattern2 state 0
  • delay a bit
  • digitalWrite() the leds for pattern2 state 1
  • repeat until state 5*

*what's supposed to happen then?- stop or go back to the top?

There are other more elegant ways, but less simple or intuitive to a neophyte programmer.

How responsive does this need to be to a change in the dipswitch mid-pattern?

What have you tried? How are the patterns supposed to blink?

EDIT: The penny dropped.. :wink:

thank you I'll try to checkout

Danois90:
EDIT: The penny dropped.. :wink:

Yeah presumably a pause between each state and the next... instructions unclear though :wink:

FEBaily:
dipswitch1 being redundant

Actually it was wrong of me to say that: presumably if dipswitch1 is ON there's no blinking at all.

You would eventually need to know (ie decide or find out from whomever set what looks like an assignment :wink: ) what happens if dipswitch1 changes along the way so that blinking stops. Do the lights "stall" where they are mid-pattern or eg continue to cycle until the next state0 where they're all off, or jump to state0 immediately.

Or are the switches both only read at power up / reset in setup()?

In a moment of weekend boredom, when I wasn't required to do any heavy lifting in the garden, I wrote a sketch to do that where there's only one line doing any writing of the leds:

digitalWrite(ledPins[led], patterns[currentPattern][currentState][led]);

The leds themselves are in a 1D array ledPins, and patterns is a 3D array of pattern / state / led.

The current pattern is decided by switch2 (I left switch1 out when I thought it was redundant, but have since realised it's an on/off), the current state is determined by a counter in a blink-without-delay blinker, and the led is determined by a very short quick for loop.

Retro-adding switch1 will be simple: just set current state to 0 (no leds on) regardless of pattern, write the leds, and don't do any blinking.

void setup()
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(5, INPUT);
pinMode(4, INPUT);
}
void loop()
{
boolean DIPSWITCH1 = digitalRead(5);
boolean DIPSWITCH2 = digitalRead(4);

if(DIPSWITCH1==LOW && DIPSWITCH2==LOW)
{

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
delay(400);
}
else
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
}
if(DIPSWITCH1==LOW && DIPSWITCH2==HIGH)
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
delay(400);

digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
delay(100);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
delay(400);
}
else
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
}
if(DIPSWITCH1==HIGH && DIPSWITCH2==LOW)
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(400);

digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(1000);
}
else
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
}
}

Please read the forum guide in the sticky post, then modify your post above and fix the way you posted that code. Thanks

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:

If you are interested in doing things correctly. Don’t use delay().

Read this:

Are we to understand this is simply a cross posting from your original?

yes

Here is the code working fine but I am failing to minimize it into loops please I need help on this one again.
void setup()
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(5, INPUT);
pinMode(4, INPUT);
}
void loop()
{
boolean DIPSWITCH1 = digitalRead(5);
boolean DIPSWITCH2 = digitalRead(4);

if(DIPSWITCH1==LOW && DIPSWITCH2==LOW)
{

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
delay(400);
}
else
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
}
if(DIPSWITCH1==LOW && DIPSWITCH2==HIGH)
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
delay(400);

digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
delay(100);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
delay(400);

digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
delay(400);
}
else
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
}
if(DIPSWITCH1==HIGH && DIPSWITCH2==LOW)
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(400);

digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(1000);

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
delay(1000);
}
else
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
}
}

Ntshukumbana:
yes

I have reported this to the forum moderators.

Go read the forum guide now. You are repeatedly breaking forum rules. If this continues the moderators may ban you from the forum.

The easiest way to do what you want is to use bit-fields that represents each state of the LED’s and an array definig the corresponding pins:

#define NUM_LEDS 4
#define PATTERN_SIZE 5

//The pins used for LED's
const byte LEDS[NUM_LEDS] = { 13, 12, 11, 10 };

//A binary representation of the  first pattern
const byte PATTERN_1[PATTERN_SIZE] = {
  0b00000000,
  0b00000001,
  0b00000010,
  0b00000100,
  0b00001000,
  0b00010000,
};

//A binary representation of the  second pattern
const byte PATTERN_2[PATTERN_SIZE] = {
  0b00000000,
  0b00000000,
  0b00000000,
  0b00000000,
  0b00000000,
  0b00000000,
};

void show_pattern(const byte* pattern)
{
  for (byte i = 0; i < PATTERN_SIZE; i++)
  {
    for (byte p = 0; p < NUM_LEDS; p++)
    {
      digitalWrite(LEDS[p], (pattern[i] & (1 << p)) != 0 ? HIGH : LOW);
    }
    delay(400);
  }
}

void setup()
{
  //Your setup code here..
}

void loop()
{
  byte switch1 = digitalRead(4);
  byte switch2 = digitalRead(5);
  if ((switch1 == LOW) && (switch2 == LOW)) show_pattern(PATTERN_1);
  else if ((switch1 == LOW) && (switch2 == HIGH)) show_pattern(PATTERN_2);
  else
  {
    for (byte p = 0; p < NUM_LEDS; p++) digitalWrite(LEDS[p], LOW);
  }
}

The code is untested and deliberate bugs are included. Now you can figure out how to fix it! :wink:

@Ntshukumbana

TOPICS MERGED

DO NOT CROSS POST as it wastes people time and efforts to help you.

Could you please READ THIS to avoid any further problems.

Bob.