Easier programming for project?

Hello!
I am embarking on a project and I need a little help in simplifying my code. Essentially, part of the project simply involves this:

Input values range from 0 to 1023.
For values >0, the light connected to pin 1 only lights up.
For values >100, the light connected to pins 1 and 2 light up.
For values >200, the light connected to pins 1, 2 and 3 light up.
And so on. I have a total of 9 lights to configure.

As of now, I am manually coding each value to correspond with its output. This means that I have around 10 if-else codes.
Eg. For values above 200, I am using this if-else code.

if(input>200)
{
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
}
else
{
digitalWrite(1,LOW);
digitalWrite(2,LOW);
digitalWrite(3,HIGH);
}

Are there any suggestions on how I can make this more automated and more concise? I've been toying around with various variables and constants, but have yet to find a way to make the code more concise. Any help would be greatly appreciated. Thank you!

Switch off all the LEDs.
If input > 0 light up pin 1
If input > 100 light up pin 2
If input > 200 light up pin 3
...
If input > 800 light up pin 9

Don't use else, just a load of if statements.

Protip - don't connect things to pins 0 or 1 on an Uno if you want to use the USB port for uploading/monitoring.

GypsumFantastic:
Switch off all the LEDs.
If input > 0 light up pin 1
If input > 100 light up pin 2
If input > 200 light up pin 3
...
If input > 800 light up pin 9

Don't use else, just a load of if statements.

Protip - don't connect things to pins 0 or 1 on an Uno if you want to use the USB port for uploading/monitoring.

Thanks for the help! However, I realised that by doing this, after I increase the input values to say, 400, then decrease it to 0, all the lights remain lit up instead of the corresponding lights only.

There’s an old adage

If it’s easier for the user, it’s harder for the developers, and
If it’s easier for the developers, it’s harder for the user

No way out of it i’m sorry!

You should be able to use an array and a for loop with a wee bit of math to do what you want (compiles, not tested…):

#define NUM_LEDS    9

const byte pinInput = A0;   //analog input

const byte pinLED0 = 2; //substitute you own pin values here
const byte pinLED1 = 3;
const byte pinLED2 = 4;
const byte pinLED3 = 5;
const byte pinLED4 = 6;
const byte pinLED5 = 7;
const byte pinLED6 = 8;
const byte pinLED7 = 9;
const byte pinLED8 = 10;

//array of LED control pins
const byte grPins[NUM_LEDS] = 
{
    pinLED0,
    pinLED1,
    pinLED2,
    pinLED3,
    pinLED4,
    pinLED5,
    pinLED6,
    pinLED7,
    pinLED8
    
};

void setup() 
{
    pinMode( pinInput, INPUT );
    for( int i=0; i<NUM_LEDS; i++ )
        pinMode( grPins[i], OUTPUT );
        
}//setup


void loop() 
{
    int
        val;
        
    val = analogRead( pinInput );
    for( int comp=0; comp<NUM_LEDS; comp++ )
        digitalWrite( grPins[comp], (val > (comp * 100)) ? HIGH:LOW );

}//loop

Here is a snippet from a code I use for a distance sensor to trigger a traffic light as I back into my garage with my truck… It works quite well but I need a more accurate sensor…

  if (distance <= 225) {
    Serial.println("AT GREEN LIGHT");
    digitalWrite(green,LOW); 
    digitalWrite(yellow,HIGH);
    digitalWrite(red,HIGH); 

  }
  if (distance <= 125) {  
    Serial.println("AND YELLOW LIGHT");
    digitalWrite(green,HIGH); 
    digitalWrite(yellow,LOW); 
    digitalWrite(red,HIGH); 
  }

  if (distance <= 75) {  
    Serial.println("AND RED LIGHT");
    digitalWrite(green,HIGH); 
    digitalWrite(yellow,HIGH);
    digitalWrite(red,LOW);
  }

  if (distance >= 240 || distance <= 0){
    Serial.println("Out of range");
    digitalWrite(green,HIGH);
    digitalWrite(yellow,HIGH);
    digitalWrite(red,HIGH);
   
  }

Just for what it’s worth, try rewriting that with if-else-if
and see if you can make it shorter/more readable!

vthy:
after I increase the input values to say, 400, then decrease it to 0, all the lights remain lit up instead of the corresponding lights only.

I think you missed the first line of the piece you quoted.

...R

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

vthy:
Thanks for the help! However, I realised that by doing this, after I increase the input values to say, 400, then decrease it to 0, all the lights remain lit up instead of the corresponding lights only.

If you start the loop by switching them all off that won't happen.

For simpler code:

  • one array with the pin numbers: LEDPIN[N_LEDS] = {2, 3, 4, …}
  • one array with the thresholds: threshold[N_LEDS] = {0, 100, 200, …}
  • one for loop to iterate over the pins, checking the thresholds and switching on or off the LEDs based on it:
for (uint8_t i = 0; i < N_LEDS; i++) {
  digitalWrite(LEDPIN[i], value > threshold[i]);
}

i was thinking smaller learning steps, but that’s certainly relevant!

Of course you can do it with with a bunch of if statements (or if/else statements, with the else switching the specific LED off).

However I think arrays and for loops are more than appropriate fro this situation. Ten LEDs that are all treated the same - just with a different threshold, that's just calling for arrays and a for loop.

Using the boolean expression to switch on/off the LED directly is maybe a bit big a step for a beginner, if OP needs explanation I'd be happy to provide.

for (int i=0; i<9; i++) {
  digitalWrite(i+1, (input > i*100) ? HIGH : LOW);
}

Or use an array of thresholds for more flexibility.

…and an array of pin numbers for even greater flexibility

I wonder what's the big picture for turning on and off some LEDs always at the same time? If it's required to have them all on or off at the same time, I'd use a single pin and a transistor for powering multiple LEDs. Otherwise, if each LED signals something different, each LED has to be turned on and off by its dedicated events, not related to the other LEDs.