Switch On Multiple LEDs with Single Switch

Here My Problem Is Anyone Have An Idea To Switch on Multiple LEDs Using a Single Pushbutton :confused: (or using any other method LDR,Flame Sensor or Whatever)

Actually I Want to Light up 18 LEDs One at a time Using a Single LDR. :o Please Give Some Help !

What leds are you using?

You can do this easily with an Arduino.
My assumption is you have not looked at the examples that come with the IDE, is this correct?

.

Actually I'm new to Arduino Programming... :frowning:
LarryD ,please Show Me A path to Overcome my Problem. :confused: I am Using Mega 2560 board. I'm not clear what you mean by asking "What leds are you using?" . The LEDs are normal LEDs works fine with 5v.Thank You :-\

Some people here used WS2812b led strips, but sounds like you are just using leds like these:

Have you written any scketches by yourself?

Have you run any example sketches such as 'blink'.

.

Yes I'm using those types of LEDs( stritcly speaking,the last yellow LED is Looks same with My LEDs)

And Of Course I Do all the Basic Examples .And I myself(I Mean The Code I typed Without Any Help From Another) Found A Way To Light Up A LED Using A LDR.Yeah I know That Code Is Simple For You Guyz. But For Me It's A EUREKA !! That Code Is Given Below.

int ledPin = 7;
int ldrPin = A0;
int ldrValue = 0;

void setup(){
  pinMode(ledPin, OUTPUT);
}

void loop(){
  ldrValue=analogRead(ldrPin);
  if (ldrValue > 450){
    digitalWrite( ledPin , HIGH );
  }
}

And I tried to modify this Code To Light 18 LEDs One At a time by using if and goto commands.but I'm getting error when compiling.The Code which I was tried is

int ledPin[2] = {7,8}; //For Testing I only Use 2 LEDs On Pins 7 & 8
int ldrPin = A0;
int ldrValue = 0;

void setup(){
  pinMode(ledPin[0], OUTPUT);
  pinMode(ledPin[1], OUTPUT);
}

void loop(){
  switch (ledPin[2])
  case ledPin[0]{
    ldrValue=analogRead(ldrPin);
  if (ldrValue > 450){
    digitalWrite( ledPin , HIGH );
    break;
  }
  case ledPin[1]{
    if (ledPin[0]= HIGH)
   for(ldrValue < 400, ldrValue > 450,ldrValue++)
   digitalWrite(ledpin[1], HIGH);
  }
  else {
    goto case ledPin[0]
  }
  }
}

Please Teach Me How To Do This

"But For Me It's A EUREKA !! "

This is a very very good sign!

Note: ledPin[2] Identifies a 2 element array.
You would then have ledPin[0] and ledPin[1]
switch (ledPin[2])
Therefore, ledPin[2] is not defined.

Do you understand the above?

.

LarryD,Thank You For Your Reply..

Could You Please Explain Me more... I'm little bit confused with this..

"Note: ledPin[2] Identifies a 2 element array.
You would then have ledPin[0] and ledPin[1]
switch (ledPin[2])
Therefore, ledPin[2] is not defined"

Thank You

switch (ledPin[2])
In the above line of code, you are trying to access a value stored at element [2]
Only [ 0 ] and [ 1 ] are actually available to you.
You are trying to use [2] which you have no idea what is stored at that location.

Arrays are zero relative.
.

yes I Understand..larryD could you Please type a code for me to light Up Two LEDs One At a time using Single LDR ? if You can I'm Able to learn from it.
thank you

We can help 'you' write the sketch.

Please explain this better:
"light Up Two LEDs One At a time using Single LDR "

.

Yeah Of Course I Need Yours Help !!

"light Up Two LEDs One At a time using Single LDR "

for explain this,i replace LDR with a Button.

think if you have more than one LED and a button ( whatever type of switching mechanism).if i named LEDs as A,B,C,D likewise, I want to Light Up those LEDs one by one.I mean I press the Button first time LED A light up.when I press the button second time LED B lights up ( Now Both LEDs A & B are In HIGH state).like wise using a single button I want to Light Up all.As a example think you have a one fire match and 10 candles.you can lit all those candles using this single fire match.like that i want to ""light Up more than one LED One At a time using Single LDR "

const byte leds[] = {3, 4, 5}; //HIGH = ON
const byte pushButton = 2;     //pushed = LOW

unsigned long switchMillis;

void setup()
{
  for (byte x = 0; x < sizeof(leds); x++)
  {
    pinMode(leds[x], OUTPUT);
    digitalWrite(leds[x], LOW);
  }
  pinMode(pushButton, INPUT_PULLUP);
}

void loop()
{
  if (millis() - switchMillis >= 20)
  {
    checkSwitches();
    switchMillis = millis();
  }
}

void checkSwitches()
{
  static byte counter   = 0;
  static byte lastState = HIGH;

  if (digitalRead(pushButton) != lastState && counter < sizeof(leds))
  {
    lastState = !lastState;
    if (lastState == LOW)
    {
      digitalWrite(leds[counter], HIGH);
      counter++;
    }
  }
}

How are your LEDs wired?

.

Thank you Larry...

I checked out your code. It works Perfectly..but the problem is as soon as the Arduino Powered, The pin 3 LED is in HIGH state. How do i Overcome this.

And If you can please tell me the logic behind this code.

Thank you!

LarryD:
How are your LEDs wired?

.

By a mistake I removed the previous reply. The LEDs obeys with button. I made a mistake when wiring circuit.sorry

Wire your LEDs as in the schematic below:

.

"And If you can please tell me the logic behind this code."

What do you not understand?

.

i understand the code... But I want to know how you think.. The logic you build on your mind

Always write your sketches so they are neat and easy to follow.

pushButton = 2; use names that mean something to you

Your goal should be to have code that can be understood at some time in the future.

The only thing I would add to the code are comments which I purposely left out of this sketch.
const byte pushButton = 2; //pushed = LOW tells you the switch is wired between GND and the pin.

Write your software so it can be changed to allow for expansion.
const byte leds[] = {3, 4, 5}; >>>>------> const byte leds[] = {3, 4, 5, 6, 7, 8, 9}

Avoid hard coding, let the compiler do the work for you.
use: sizeof(leds) instead of 3

Always put braces on separate lines by themself.
{
}

One line for each line of code.

Use arrays if possible.
leds[] = {3,4,5};

Use the smallest 'type' as you can.
byte counter; instead of int counter

Use 'const' for variables that do not change.
const byte counter

"i understand the code..."
How would you change the code to get a pattern on the LEDs as below?
000, 001, 011, 111, 011, 001, 000, 001 etc.

.

What is the quickest way to scare off an OP.

Answer: >> Ask them a question. <<