Go Down

Topic: Switch On Multiple LEDs with Single Switch (Read 696 times) previous topic - next topic

hashan22

Here My Problem Is Anyone Have An Idea To Switch on Multiple LEDs Using a Single Pushbutton  :smiley-confuse:  (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 !

larryd

#1
May 12, 2017, 07:20 pm Last Edit: May 12, 2017, 07:21 pm by LarryD
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?


.
No technical PMs.
The last thing you did is where you should start looking.

hashan22

Actually I'm new to Arduino Programming...  :(
LarryD ,please Show Me A path to Overcome my Problem. :smiley-confuse:  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 :-\

larryd

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


.
No technical PMs.
The last thing you did is where you should start looking.

hashan22

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.
Code: [Select]
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
Code: [Select]
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

larryd

#5
May 12, 2017, 10:07 pm Last Edit: May 12, 2017, 10:09 pm by LarryD
"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?


.
No technical PMs.
The last thing you did is where you should start looking.

hashan22

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

larryd

#7
May 13, 2017, 04:14 pm Last Edit: May 13, 2017, 04:14 pm by LarryD
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.
.
No technical PMs.
The last thing you did is where you should start looking.

hashan22

#8
May 13, 2017, 04:22 pm Last Edit: May 13, 2017, 04:30 pm by hashan22
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

larryd

We can help 'you' write the sketch.

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


.
No technical PMs.
The last thing you did is where you should start looking.

hashan22

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 "

larryd

Code: [Select]

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++;
    }
  }
}
No technical PMs.
The last thing you did is where you should start looking.

larryd

No technical PMs.
The last thing you did is where you should start looking.

hashan22

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!


hashan22

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

Go Up