Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!




LED Combination con...
 
Share:
Notifications
Clear all

LED Combination control using Arduino


(@nikhil_m)
Active Member
Joined: 5 months ago
Posts: 9
Topic starter  

Hello!

 

I am a beginner at Arduino. I need to implement a simple circuit to control various LED glow scenarios and their reset using tact switches.

I request to please help me with this project.

I am attaching the image of the assembly done on bread board.

 

The circuit is intended to function as below:

 

System must take the input from the user.

 

User selects Scenario 1.

LEDs L1, L2 and L4 should turn ON.

User presses S1 which will turn off L1, similarly S2 will turn off L2 and S4 will turn off L4.

All LEDs are now turned OFF.

 

System waits for next input from user.

 

User selects Scenario 2.

LEDs L1, L3 and L5 should turn ON.

User presses S1 which will turn off L1, similarly S3 will turn off L3 and S5 will turn off L5.

All LEDs are now turned OFF.

 

​System waits for next input from user.

 

User selects Scenario 3.

LEDs L2, L3 and L5 should turn ON.

User presses S2 which will turn off L2, similarly S3 will turn off L3 and S5 will turn off L5.

All LEDs are now turned OFF.

 

​System waits for next input from user.

 

User selects Scenario 4.

LEDs L1, L3 and L4 should turn ON.

User presses S1 which will turn off L1, similarly S3 will turn off L3 and S4 will turn off L4.

All LEDs are now turned OFF.

 

Please help me with the logic and the code to make this work.

 

Thank you in advance!

 

 

 

 

 

 


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 9 years ago
Posts: 2201
 

Hi Nikhil_m,

I'm not sure how far you've gotten with the code ...?

How does the user select a scenario? By pressing one of the Sx buttons?

Once a scenario is selected, I'd imagine that the code waits for a key to be pressed.
If at least one of the LEDs isn't off, then the LED matching the switch should be switched off.
If however all LEDs are already off then the new scenario should be applied.

I'd define the PINs for that something like so:

#define LEDPIN1 6
#define LEDPIN2 7
#define LEDPIN3 8
#define LEDPIN4 9
#define LEDPIN5 10

 

The numbers 6-10 are just example numbers - modify to what you're using.

Since you seem to be using regular LEDs, I'd use an array to hold the status (on or off) of each LED.
Something like this:

bool LEDs[5]; // hold status of each individual LED

 

The same way I'd make a function to see if all LEDs are OFF:

bool AllLEDsOff() {
  // LEDs[0] || LEDs[1] = LEDs[0] or LEDs[1] -> so if one of these is TRUE the result will be "true". 
  // But we want the opposite, so !true = false

  return = !(LEDs[0]||LEDs[1]||LEDs[2]||LEDs[3]||LEDs[4]);
}

 

And we'd want to have a function to turn a LED on or off (pass LED pin number and true=ON and false=OFF).

void setLED(int LEDpin, bool LEDOn) {
  if (LEDOn) {
    digitalWrite(LEDpin, HIGH);
  } else {
    digitalWrite(LEDpin, LOW);
  }
}

 

In setup we also have to set all LED pins to being outputs;

pinMode(LEDPIN1, OUTPUT);
pinMode(LEDPIN2, OUTPUT);
pinMode(LEDPIN3, OUTPUT);
pinMode(LEDPIN4, OUTPUT);
pinMode(LEDPIN5, OUTPUT);

 

For setting a scenario I'd make a function, something like this:

void setScenario(int S) {
  // Note:
  // Arrays start counting with zero, so the 1ste LED would be index 0
  // Below I use || (or) to determine which LEDs should be on or off
  // for example: LEDs[0] = true when S=1 or S=2 or S=4
  //   so for Scenario's 1, 2 and 4 (that's why I aligned them a little weird to make it more readable)

  LEDs[0] = (S=1)||(S=2)||       (S=4);   // note: array indexes start counting with zero ... 
  LEDs[1] = (S=1)||       (S=3)       ; 
  LEDs[2] =        (S=2)||(S=3)||(S=4); 
  LEDs[3] = (S=1)||              (S=4); 
  LEDs[4] =        (S=2)||       (S=4); 

  setLED(LEDPIN1, LEDs[0]);
  setLED(LEDPIN2, LEDs[1]);
  setLED(LEDPIN3, LEDs[2]);
  setLED(LEDPIN4, LEDs[3]);
  setLED(LEDPIN5, LEDs[4]);
}

 

Next I'll start with setting the array to false, all LEDs off, by calling setScenario(0) -> this will result in all being false since all equations fail and all LEDs to OFF.
We should call that in the setup() function as well.

Now in the loop() I'd wait for buttons being pressed.

When a button is being pressed, I'd first make sure that we are not looking at scene selection (all LEDs off).
So if AllLEDsOff==true then all LEDs are off and we need to call setScenario to populate the array for the correct scenario.
If AllLEDsOff==false, and at least one LED is still on, then we need to switch a LED off based on the switch that was pressed.

Does this help to get started? (none of the code has been tested) 


nikhil_m liked
ReplyQuote
(@nikhil_m)
Active Member
Joined: 5 months ago
Posts: 9
Topic starter  

Hello @hans ,

Thank you for your reply.
I am currently able to turn the LEDs ON using a Arduino code for each scenario.
But unable to turn them OFF by pressing corresponding switches.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 9 years ago
Posts: 2201
 

Would you mind posting your code? This way I may be able to see what is going wrong ... 😊 


nikhil_m liked
ReplyQuote
(@nikhil_m)
Active Member
Joined: 5 months ago
Posts: 9
Topic starter  

@hans I am trying to post the code but unable to do so


ReplyQuote


(@nikhil_m)
Active Member
Joined: 5 months ago
Posts: 9
Topic starter  

@hans Thank you for supporting me.

I was able to run the program with desired output.

I took some help from a friend, and we also referred to the reply from you on this thread.

I am trying to share the code here but not able to see the reply once I post it.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 9 years ago
Posts: 2201
 

Hi Nikhil_m,

Sorry for the inconvenience ....
The forum will not allow posting code or attachments until at least 5 posts have been manually approved - too many people spam forums like this and we'd like to prevent that

I think you just managed to get 5 posts 😊 
Feel free to try to post the code again. (ideally also attached as an ino file).


ReplyQuote
(@nikhil_m)
Active Member
Joined: 5 months ago
Posts: 9
Topic starter  

@hans - I tried doing it again but still unable to. Let me see if I can attach a file.


ReplyQuote
(@nikhil_m)
Active Member
Joined: 5 months ago
Posts: 9
Topic starter  
#include < EventButton.h>

EventButton firstButton(9);
EventButton secondButton(2);
EventButton thirdButton(3);
EventButton fourthButton(10);
EventButton fifthButton(11);
int a1 = 12;
int a2 = 8;
int a3 = 4;
int a4 = 5;
int a5 = 6;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(a1, OUTPUT);
  pinMode(a2, OUTPUT);
  pinMode(a3, OUTPUT);
  pinMode(a4, OUTPUT);
  pinMode(a5, OUTPUT);

  firstButton.setClickHandler(onFirstButtonClicked);
  secondButton.setClickHandler(onSecondButtonClicked);
  thirdButton.setClickHandler(onThirdButtonClicked);
  fourthButton.setClickHandler(onFourthButtonClicked);
  fifthButton.setClickHandler(onFifthButtonClicked);
}

void loop() {

  char data = Serial.read();
  switch (data) //Selection Control Statement
  {
  case '1':
    digitalWrite(a1, HIGH);
    digitalWrite(a2, HIGH);
    digitalWrite(a4, HIGH);
    break;
  case '2':
    digitalWrite(a1, HIGH);
    digitalWrite(a3, HIGH);
    digitalWrite(a5, HIGH);
    break;
  case '3':
    digitalWrite(a1, HIGH);
    digitalWrite(a2, HIGH);
    digitalWrite(a3, HIGH);
    break;
  case '4':
    digitalWrite(a1, HIGH);
    digitalWrite(a3, HIGH);
    digitalWrite(a4, HIGH);
    break;

  }

  firstButton.update();
  secondButton.update();
  thirdButton.update();
  fourthButton.update();
  fifthButton.update();
}

void onFirstButtonClicked(EventButton & eb) {
  Serial.print("First button clicked ");
  digitalWrite(a1, LOW);
}

void onSecondButtonClicked(EventButton & eb) {
  Serial.print("Second button clicked: ");
  digitalWrite(a2, LOW);
}

void onThirdButtonClicked(EventButton & eb) {
  Serial.print("Third button clicked: ");
  digitalWrite(a3, LOW);
}

void onFourthButtonClicked(EventButton & eb) {
  Serial.print("Fourth button clicked ");
  digitalWrite(a4, LOW);
}

void onFifthButtonClicked(EventButton & eb) {
  Serial.print("Fifth button clicked ");
  digitalWrite(a5, LOW);
}

ReplyQuote
(@nikhil_m)
Active Member
Joined: 5 months ago
Posts: 9
Topic starter  

@hans

What I need to do now is make the logic work for 60 LEDs.

I know it will not be done the same way the code has been written, but can you guide me with something that will work?


ReplyQuote


 Hans
(@hans)
Noble Member Admin
Joined: 9 years ago
Posts: 2201
 

First of all, thanks for making me familiar with the event library - I didn't know this one existed. 😊 

As for applying this to 60 LEDs ... do you want 60 switches with that as well?

60 LEDs: I'd start using addressable RGB LEDs instead of individual LEDs - like the WS2811 or WS2812.
These are available as strips, or "individual" LEDs ... you will be able to get those from Amazon and the look like this:

 (strips)

 (individuals)

This way you'd need only one pin for all 60 LEDs (see my article on WS2811/WS2812).

If you want 60 buttons as well, then you'll need to look into some kind of multiplexer, so you can connect that many switches.
Unfortunately, a matrix won't work - as it would still need too many pins (6x10 = 6+10 = 16 pins needed).
My experience with multiplexers and shift-registers is pretty much zero.

You could also try using a regular keyboard (has more than 100 keys, and you could take it apart so you can wire switches to replace the keyboard switches).
I found a project that actually allows you to connect a USB keyboard to your Arduino.

Hope this helps ...


ReplyQuote
(@nikhil_m)
Active Member
Joined: 5 months ago
Posts: 9
Topic starter  

@hans 

Thank you for your detailed reply.

I am trying to procure WS2812 and will try to control select LEDs using an Arduino UNO.

I was just curious about one thing, if I am able to control the switching ON of the required LEDs, more than half of my task is done. Now to reset a particular LED that is glowing, can I just add a tactile switch between the +ve and the GND pins and press it to short the supply to ground which will switch the LED off for that moment and when I release the tact switch, the LED won't glow again in absence of a trigger that initially switched it ON.

In that case, I will not have a feedback to the system to know that the LED is now OFF, which is pretty much okay with me.

So basically if I have a switching ON mechanism in place, I just need to find a way to reset the LED and make it OFF.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 9 years ago
Posts: 2201
 

I'm not sure what you have in mind, but shorting V and GND is never a good idea. So I would not take that as a viable option to implement.


ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: