Control 12 leds individually by using a keyboard

Dear Arduino community,

I am trying to control 12 different leds with 6 different light intensities (logarithmic scale i.e., 3.125%, 6.25%, 12.5%, 25%, 50%, 100%). I have to turn OFF and ON every LED individually and I want to receive a message from the serial monitor, for example "LED2 is turned ON" ... "LED3 is turned OFF". I want to use the keyboard of my laptop and assign letters from the alphabet. (A to turn on LED2, B to turn OFF LED2, C to turn ON LED3, D to turn OFF LED3...).Up until now, I can only control one LED (based on this video:https://www.youtube.com/watch?v=g0pSfyXOXj8) but I dont know how to control the other ones. I am using a the SoftPWM library (GitHub - bhagman/SoftPWM: A Wiring (and Arduino) Library to produce PWM signals on arbitrary pins.) to assign the logarithmic values to the LEDs relying on Pulse width modulation (255 maximum and 8 the minimum) because I am using an Arduino UNO and it only has 6 PWM pins.

here is the code

#include "SoftPWM.h"

int ledPin2 = 2;
int ledPin3 = 3;

void setup() {

  Serial.begin(9600);  //Initialize serial port to send and receive at 9600 baud

  pinMode(ledPin2, OUTPUT);  //set pins as output
  pinMode(ledPin3, OUTPUT);

  SoftPWMBegin();
  SoftPWMSet(2, 0);
  SoftPWMSet(3, 0);
}

void loop() {

  while (Serial.available() == 0)
    ;  //Check to see if at least one character is available

  int val2 = Serial.read() - '0';  // ASCII value converted to numeric value 
  int val3 = Serial.read() - '0';

  //LED2 ON
  if (val2 == 1) {
    Serial.println("Led2 is ON");
    SoftPWMSet(2, 8);
  }
  //LED2 OFF
  else if (val2 == 0) {
    Serial.println("Led2 is OFF");
    SoftPWMSet(2, 0);
  }

  //LED3 ON
  if (val3 == 2) {
    Serial.println("Led3 is ON");
    SoftPWMSet(3, 8);
  }
  //LED3 OFF
  else if (val3 == 3) {
    Serial.println("Led3 is OFF");
    SoftPWMSet(3, 0);
  }




}  // end of loop

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

This is the easiest way to tidy up the code and add the code tags

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Try again with the code tags please!

yes sorry! Trying to figure it out

1 Like

Ok, but how will you select the 6 light intensities?

by using values between 255 and 8.
255 (100%), 127.5 (50%), 63.75 (25%) etc...
I would just set in the SoftPWMset line

  //LED3 ON
  if (val3 == 2) {
    Serial.println("Led3 is ON");
    SoftPWMSet(3, x);

x being the values mentioned above (255 to 8)

Please post an example of the command that you would enter in the Serial monitor to set the intensity if the third LED to 25%

LED 2 and 3 would have a light intensity of 8 (~3.125%). I would just press "2" on the keyboard in the serial monitor and the LED should work giving the message "LED3 is ON". If I press "3" LED3 should turn off again. I think that my problem is probably how to chain many if commands to control the 12 leds

So the intensity is fixed for each LED. Is that correct ?

How do you envisage entering a chain of commands to say change the state of several LEDs at the same time ?

Once the requirement is finalised then you can move on to writing code

Yes the intensity is fixed for each LED. I just need to turn on one LED at a time and then turn it OFF again to turn ON the next LED.

LED3 --> ON
LED3--> OFF
LED4 --> ON
LED4--> OFF

So of the 12 LEDS just ONE is ON. All the others are OFF.
I want to use the keyboard buttons as switches for the LEDS becaus I dont want to build switches directly on the cicruit.

How about using commands in this format ?
2,on
5,off
0,off
etc

That will make parsing the commands easier

So I will just specify that all the other LEDs will turn OFF once I turn one LED on?

Hold on

You said earlier

That sounds like you do not want the user to be able to change the state of each LED individually.

But then you said

Sorry, but I don't understand what you actually want to do

Suppose you start with all of the LEDs off. What do you want the user to be able to do next ?

Ok sorry for the confusion. So if all the LEDs are OFF, I want to press a button on my keyboard to turn the first LED with the lowest intensity ON and then have another button on the keyboard to turn it OFF again. All 12 LEDs are turned OFF. Then I want to turn ON one LED with the next highest intensity and the cycle starts again

It's getting clearer, I think

You could use a single keyboard input, such as an "A" to toggle the first LED, ie turn it on if it is off and vice versa. How about doing that ?

Note that using the Serial monitor the user will need to press the Enter key in order to send a command. Other terminal programs would remove the need to press Enter to initiate the command

Yes! Thats a good idea... I´ve seen that you could use putty...

Could you maybe write an example on how to do that please? I am new to arduino...

I am trying to use if, while, then and else statements

If you only need on/off, why do you need SoftPWM?

because the LEDs need fixed intensities ranging from 255 to 8. Group 1 (LED2/3 have intensites of 3.125%) Group 2 (LED4/5 have intensites of 6.25% and so on)

Here is an example of toggling the state of an LED based on the character entered

#define OFF HIGH        //set these HGH or LOW to suit your LED wiring 
#define ON LOW

struct ledData          //data needed for each LED
{
    byte pinNum;
    byte currentState;
    char key;
};

ledData leds[] = {          //initialiase the LED data
    { 3, OFF, 'A' },
    { 5, OFF, 'B' },
    { 6, OFF, 'C' },
    { 9, OFF, 'D' }
};

const byte LED_COUNT = sizeof(leds) / sizeof(leds[0]);  //calculate the number of LEDs

void setup()
{
    Serial.begin(115200);
    for (int c = 0; c < LED_COUNT; c++)
    {
        pinMode(leds[c].pinNum, OUTPUT);
    }
    showLeds();     //show LEDs in there initial state
}

void loop()
{
    if (Serial.available())     //wait for Serial input
    {
        char c = Serial.read(); //get a character
        for (int x = 0; x < LED_COUNT; x++) //search for the character in the LED data
        {
            if (leds[x].key == c)   //if there is a match
            {
                leds[x].currentState = !leds[x].currentState;   //change the state of matching LED
                showLeds();     //display the LEDs in their current state
            }
        }
    }
}

void showLeds()
{
    for (int c = 0; c < LED_COUNT; c++) //for each LED
    {
        digitalWrite(leds[c].pinNum, leds[c].currentState);     //write the state to the LED pin
    }
}

The sketch will seem complicated but I have used plenty of comments.

Things for you to change to suit your project

  1. change the definition of ON and OFF to suit your LED wiring
  2. change the pin numbers to match your wiring
  3. feel free to add more LED definitions. The sketch will calculate how many there are

Once you have got the LEDs turning on and off we can talk about using PWM instead to set the intensity of the LEDs which will actually be easy because of how the program is structured

1 Like