Is there a better way?

I am new to coding, in this attempt I am trying to turn on LEDs exclusively one at a time. This code works but I am wondering if there is a better way of accomplishing the task. Just checking for a better method if there is one. Thank you for your time and consideration.

 int ledPin1=3;
int  ledPin2=4;
int ledPin3=5;
int ledPin4=6;
int delay1=250;


void setup() {
  // put your setup code here, to run once:
 pinMode(ledPin1,OUTPUT);
 pinMode(ledPin2,OUTPUT);
 pinMode(ledPin3,OUTPUT);
 pinMode(ledPin4,OUTPUT);
 delay(2000);
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,HIGH);
digitalWrite(ledPin4,HIGH);

Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  
int sensorValue=analogRead(A0);
Serial.println (sensorValue);
if (sensorValue<255)digitalWrite(ledPin1,LOW);

if (sensorValue>256)digitalWrite(ledPin1,HIGH);

if (sensorValue<256)digitalWrite(ledPin2,HIGH);
if (sensorValue>257)digitalWrite(ledPin2,LOW);
if (sensorValue>510)digitalWrite(ledPin2,HIGH);

if (sensorValue<510)digitalWrite(ledPin3,HIGH);
if (sensorValue>511)digitalWrite(ledPin3,LOW);
if (sensorValue>766)digitalWrite(ledPin3,HIGH);

if (sensorValue<766)digitalWrite(ledPin4,HIGH);
if (sensorValue>767)digitalWrite(ledPin4,LOW);



}

What is it about the code that you do not like?

never mind

I'd slow up amount of prints, which should speed up the program a tiny bit.

consider.

unsigned long printInterval = 1000;
unsigned long timePast = millis();

void loop() {
  // put your main code here, to run repeatedly:
  
int sensorValue=analogRead(A0);
if( (millis()-timepast) >= printInterval )
{
Serial.println (sensorValue);
timePast=millis();
}
if (sensorValue<255)digitalWrite(ledPin1,LOW);

if (sensorValue>256)digitalWrite(ledPin1,HIGH);

if (sensorValue<256)digitalWrite(ledPin2,HIGH);
if (sensorValue>257)digitalWrite(ledPin2,LOW);
if (sensorValue>510)digitalWrite(ledPin2,HIGH);

if (sensorValue<510)digitalWrite(ledPin3,HIGH);
if (sensorValue>511)digitalWrite(ledPin3,LOW);
if (sensorValue>766)digitalWrite(ledPin3,HIGH);

if (sensorValue<766)digitalWrite(ledPin4,HIGH);
if (sensorValue>767)digitalWrite(ledPin4,LOW);



}

What do you want the value of 255 or 256 to do?

The first two conditional lines will fail with a value of 255 and it will execute the < 256 conditional... turning on ledPin2, then it will execute the < 510 and set ledPin3.

If it's 256, it will fail the first 4 conditionals and set ledPin3 when it gets to < 510...

Anything below 255 will pass any conditional that is less then 256...

Is this the intended operation?

Might want to 'case' it or use an else clause....

If you want a range specify 'if (x < y && x > z) ...'


Not really clear on what you are attempting to do... the analog value is not mentioned, so I have no idea of the end product.... I think I see what you are trying to do, but a better explanation would be helpful.

:smiley_cat:

You could consider using a structure to hold an LED pin, a low and a high value. E.g.,

struct Led {
  uint8_t pin;
  uint16_t low;
  uint16_t high;
};

This can be used to make an array of LED objects as follows:

Led const leds[] {{3, 0, 255}, {4, 255, 510}, {5, 510, 766}, {6, 766, 1024}};

The array can then be used to control the LEDs in the main loop:

for (Led const& led: leds) {
  if (sensorValue >= led.low and sensorValue < led.high) {
    digitalWrite(led.pin, LOW);
  }
  else {
    digitalWrite(led.pin, HIGH);
  }
}

Putting it all together:

struct Led {
  uint8_t pin;
  uint16_t low;
  uint16_t high;
};

Led const leds[] {{3, 0, 255}, {4, 255, 510}, {5, 510, 766}, {6, 766, 1024}};


void setup() {
  Serial.begin(9600);

  for (Led const& led: leds) {
    pinMode(led.pin, OUTPUT);
    digitalWrite(led.pin, HIGH);
  }
}

void loop() {
  uint16_t sensorValue {analogRead(A0)};
  Serial.println(sensorValue);

  for (Led const& led: leds) {
    if (sensorValue >= led.low and sensorValue < led.high) {
      digitalWrite(led.pin, LOW);
    }
    else {
      digitalWrite(led.pin, HIGH);
    }
  }
}

Or use a range in the switch/case

    case 'A' ... 'Z' :  // capital letters

a7

It seems brutish to me. Of course it is only my second sketch.
The goal is to use the analog range of 0-1023 to turn on led 1, then led 2, then led 3 then led 4. One at a time, in sequence either going up or Dian and never having two LEDs on at one time.

Your original sketch has all but one LED lit at a time.

If you replace HIGH by LOW and vice versa in the sketch suggested in post #6, you get the behaviour you describe.

try:

int ledPin1 = 3;
int ledPin2 = 4;
int ledPin3 = 5;
int ledPin4 = 6;
int delay1 = 250;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  digitalWrite(ledPin1, LOW);//all LEDs ON for test period
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  delay(1000);
}

void loop() {
  int sensorValue = analogRead(A0);
  Serial.println (sensorValue);
  digitalWrite(ledPin1, HIGH);// default all to off, then immediately turn on the appropriate one
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, HIGH);
  if (sensorValue < 256)digitalWrite(ledPin1, LOW);
  if (255 < sensorValue < 512)digitalWrite(ledPin2, LOW);
  if (511 < sensorValue < 768)digitalWrite(ledPin3, LOW);
  if (767 < sensorValue)digitalWrite(ledPin4, LOW);
  delay(delay1); //slow loop down so printing isn't annoying
}

Another approach, if your scale is linear.