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
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.
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
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.
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
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
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
change the definition of ON and OFF to suit your LED wiring
change the pin numbers to match your wiring
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