Raj7912
February 12, 2020, 3:56pm
1
can i use library so that i dont have to type a large code,
for that the code look small.
for example
if(a=='b')
{
digitalwrite(led,LOW);
}
if(a=='c')
{
digitalwrite(led1,LOW);
}
...
...
...if(a=='n')
{
digitalwrite(ledn,LOW)//upto n time
can it be type in single line of program?
if yes then how please give suggestions.
If you are going to use exactly that sequence of tests in several programs then it may be worth putting it in a function accessible to multiple programs whether or not you regard that as a library.
If, on the other hand, the code is only going to be used once then it is a waste of time. From the small section of code provided, the most economical way of implementing it would seem to be a couple of arrays or a struct but more detail is required
Please explain what exactly you are doing
A shorter version:
switch (a)
{
case 'b': digitalwrite(led, LOW); break;
case 'c': digitalwrite(led1, LOW); break;
.
.
.
case 'n': digitalwrite(ledn, LOW); break;
}
And another way using arrays:
const char Values[] = {'b', 'c', ... 'n'}; // Or "bc...n"
const byte ValCount = sizeof Values / sizeof Values[0];
const byte LEDPins[ValCount] = {led, led1, ... ledn};
for (int i = 0; i < ValCount; i++)
{
if (a == Values[i])
digitalWrite(LEDPins[i], LOW);
}
1 Like
hammy
February 12, 2020, 4:57pm
4
Don’t forget that it can be harder to debug if you go for very short code too.
Raj7912:
can it be type in single line of program?
You can define a function to do all the work and call the function in one line. For example you could define the function like this:
void TurnOffMatchingLED(char match)
{
switch (match)
{
case 'b': digitalwrite(led, LOW); break;
case 'c': digitalwrite(led1, LOW); break;
.
.
.
case 'n': digitalwrite(ledn, LOW); break;
}
}
And call the function like this:
TurnOffMatchingLED(a);
1 Like