Help With 4 LEDs & Potentiometer Programming

Hello all,

I cannot seem to get my code to write. I am trying to have 4 LEDs, in a straight line connected each to an analog output. The potentiometer is a connected to a 5 Volt power source. I would like to have four categories of 0-1.25V, 1.25 to 2.5 Volt, 2.5 to 3.75 volt, and 3.75 to 5 Volt and I am trying to write the program so that the brightness of the LED changes with respect to voltage. For each 1.25V interval, the brightness of one LED will be proportional to the output voltage of the connected potentiometer. Below is my code that is not working:

[color=#16192b]int LED1 = 9;      // LED 1 connected to digital pin 9[/color]
[color=#16192b]int LED2 = 10; //LED 2 connected to digital pin 10[/color]
[color=#16192b]int LED3 = 11; //LED 2 connected to digital pin 11[/color]
[color=#16192b]int LED4 = 12;  //LED 2 connected to digital pin 12[/color]
[color=#16192b]int analogPin = 3;   // potentiometer connected to analog pin 3[/color]
[color=#16192b]int val = 0;         // variable to store the read value[/color]
[color=#16192b] [/color]
[color=#16192b]void setup()[/color]
[color=#16192b]{[/color]
[color=#16192b]  pinMode(LED1, OUTPUT);  // sets the pin as output[/color]
[color=#16192b]  pinMode(LED2, OUTPUT);  // sets the pin as output[/color]
[color=#16192b]  pinMode(LED3, OUTPUT);  // sets the pin as output[/color]
[color=#16192b]  pinMode(LED4, OUTPUT);  // sets the pin as output[/color]
[color=#16192b]}[/color]
[color=#16192b] [/color]
[color=#16192b]void loop()[/color]
[color=#16192b]{[/color]
[color=#16192b]  val = analogRead(analogPin);  // read the input pin[/color]
[color=#16192b]  float voltage = val*(5.0/ 1023.0) //convert it so we can make our switch case[/color]
[color=#16192b]       if(voltage < 1.25)[/color]
[color=#16192b]      {[/color]
[color=#16192b]          analogWrite(LED1, val); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b]analogWrite(LED2, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] analogWrite(LED3, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b]            analogWrite(LED4, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] [/color]
[color=#16192b] [/color]
[color=#16192b]      }[/color]
[color=#16192b]        else[/color]
[color=#16192b]           {[/color]
[color=#16192b]              if (( x > 1.25)&&(x < 2.5) )[/color]
[color=#16192b]               {[/color]
[color=#16192b]                   analogWrite(LED1, val); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b]analogWrite(LED2, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] analogWrite(LED3, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b]            analogWrite(LED4, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] [/color]
[color=#16192b]               }[/color]
[color=#16192b]              else[/color]
[color=#16192b]               {[/color]
[color=#16192b]              if (( x > 2.5)&&(x < 3.75) )[/color]
[color=#16192b]               {[/color]
[color=#16192b]                   analogWrite(LED1, val); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b]analogWrite(LED2, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] analogWrite(LED3, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b]            analogWrite(LED4, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b]          }[/color]
[color=#16192b]                        else[/color]
[color=#16192b]              if (( x > 3.75)&&(x < 5) )[/color]
[color=#16192b]               {[/color]
[color=#16192b]                   analogWrite(LED1, val); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b]analogWrite(LED2, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b] analogWrite(LED3, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b]            analogWrite(LED4, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255[/color]
[color=#16192b]    }[/color]
}

I am not sure where I am going wrong. Thanks!

Thanks for using code tags but HTML tags such as color do not work in code tags. Please remove them

  float voltage = val * (5.0 / 1023.0) //convert it so we can make our switch case

semicolon missing ?

    if (( x > 1.25) && (x < 2.5) )

Where is x declared and given a value ?

UKHeliBob:
Thanks for using code tags but HTML tags such as color do not work in code tags. Please remove them

This is most likely because of a bug in the new forum "WYSIWYG" editor. If you paste text with markup (e.g. from a web page, from an editor with syntax highlighting like VSCode, etc.) it converts the markup to BB tags, regardless of whether it's inside of code tags or not. It looks fine in the preview, but when you post, you get something like the first post ─ hence the WYSIWYG in quotes :).

OP's code after auto-formatting, which reveals the missing semicolon as well as some missing closing braces.

int LED1 = 9;        // LED 1 connected to digital pin 9
int LED2 = 10;       // LED 2 connected to digital pin 10
int LED3 = 11;       // LED 2 connected to digital pin 11
int LED4 = 12;       // LED 2 connected to digital pin 12
int analogPin = 3;   // potentiometer connected to analog pin 3
int val = 0;         // variable to store the read value


void setup() {
  pinMode(LED1, OUTPUT);  // sets the pin as output
  pinMode(LED2, OUTPUT);  // sets the pin as output
  pinMode(LED3, OUTPUT);  // sets the pin as output
  pinMode(LED4, OUTPUT);  // sets the pin as output
}


void loop() {
  val = analogRead(analogPin);  // read the input pin
  float voltage = val * (5.0 / 1023.0) //convert it so we can make our switch case


                  if (voltage < 1.25)
  {
    analogWrite(LED1, val); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
    analogWrite(LED2, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
    analogWrite(LED3, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
    analogWrite(LED4, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255




  }
  else
  {
    if (( x > 1.25) && (x < 2.5) )
    {
      analogWrite(LED1, val); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
      analogWrite(LED2, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
      analogWrite(LED3, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
      analogWrite(LED4, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255


    }
    else
    {
      if (( x > 2.5) && (x < 3.75) )
      {
        analogWrite(LED1, val); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
        analogWrite(LED2, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
        analogWrite(LED3, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
        analogWrite(LED4, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
      }
      else if (( x > 3.75) && (x < 5) )
      {
        analogWrite(LED1, val); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
        analogWrite(LED2, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
        analogWrite(LED3, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
        analogWrite(LED4, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
      }
    }

Pieter

PieterP:
This is most likely because of a bug in the new forum "WYSIWYG" editor. If you paste text with markup (e.g. from a web page, from an editor with syntax highlighting like VSCode, etc.) it converts the markup to BB tags, regardless of whether it's inside of code tags or not. It looks fine in the preview, but when you post, you get something like the first post ─ hence the WYSIWYG in quotes :).

Pieter

Yes it is and it is very frustrating.

The code does not compile because of a missing semicolon and missing closing braces. Also, no matter the analog input you have the following lines:

    analogWrite(LED1, val); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
    analogWrite(LED2, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
    analogWrite(LED3, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
    analogWrite(LED4, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255

Not sure exactly what you are trying to accomplish.