[SOLVED] Potentiometer to sequentially light LEDs

Hi all,
I have been Arduino-ing for all of 3 days now and I decided to try my first solo project. Unfortunately it hasn't worked, and despite reading through all the examples I can't seem to figure it out for myself.

I have a potentiometer wired in for an analog listen, and 3 outs to light LEDs. I'm trying to get the potentiometer to sequentially light up each LED as I turn it.

Literally nothing happens when I run the code and fiddle with the pot. Not a single LED lights up.

int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
int potPin = A0;
int val;

void setup() {
   pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
}


void loop() {
  val = analogRead(potPin);
  val = map(val, 0, 1023, 0, 180);
  
  if (val = 0) {
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, HIGH);
  } else if (val > 200) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    } else if (val > 400) {
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, LOW);
    } else {
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      }
    delay(1);
  }

One note: I'm pretty sure I don't need the line "val = map(val, 0, 1023, 0, 180);" but I threw it in just in whilst trying to troubleshoot, as I had used similar code in a simple potentiometer/servo project.

My wiring: +5v and ground to power rail's + & -, pot + & - to power rail and listen to A0, 13 out to 220 ohm resistor to LED then to ground rail.
Each part of the circuit works with my power supply, so I'm certain it's the code that's the problem.

Thanks for reading, and for any help you can lend.

  if (val = 0)

= is for assignment
== is for comparison
Your code is setting val to 0

  val = map(val, 0, 1023, 0, 180);

As a result of the map() val will be in the range 0 to 180 so comparisons with values above 180 will always return false. In your application you don't need map()

  delay(1);Why ?

Thank you very much for the quick reply!
I've used all of your advice but unfortunately I'm still not getting a single LED turning on when I run the code.

UKHeliBob:
Why ?

I heard it was good practice.

(Edit) Just discovered that the "L" LED on my uno is turning off when the potentiometer is turned fully clockwise. What on earth??!
(Edit 2) I made a quick test code to flash the LEDs on and off alone, and it turns out the "L" LED is running the code! Any clues as to why?
Also, when I upload a new sketch, the "RX" and "TX" LEDs run the code once, then the "L" runs every loop thereafter, but if I press the reset button only the "L" runs the code from the start.

(Edit 3) I changed the wiring, so that the ground of the LEDs all go to a different ground on the arduino to the potentiometer, and now I'm getting light from the 1st LED.

Ahaa.

...my breadboard is faulty.

Thanks for the help, it's working properly now hahahahahaha

Are you sure your LEDs are wired right ( short leg is cathode, goes to GND )? Try this anyway:

const byte LED1 = 13,
           LED2 = 12,
           LED3 = 11,
           potPin = A0;
int val;           

void setup()
{
   pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
}

void loop()
{
  val = analogRead(potPin);
  digitalWrite(LED1,val > 100);
  digitalWrite(LED2,val > 400); 
  digitalWrite(LED3,val > 800);
}

BTW: Don't believe every thing you hear. :slight_smile:

...my breadboard is faulty.

Is it faulty or is it one of the type where the long busbars have a gap or gaps in them indicated by the gap in the adjacent coloured line ?

Like this:
jumpers2.png

UKHeliBob:
Is it faulty or is it one of the type where the long busbars have a gap or gaps in them indicated by the gap in the adjacent coloured line ?

Definitely faulty. Found a break in the circuit where there definitely shouldn't be one (inside a single segment).

Still having trouble with the code, though it's half working.
I'm registering 0 at full clockwise tilt, but the only other signal I can get is having LED1 & 2 turn on, and it doesn't seem to matter what I set the numbers to.
I had it set to light 1 & 2 at 333 to 666 at first, so then I changed it to light only 1 at anything up to 500 and 1 & 2 at 500 to 666 and it still only lit up 1 and 2 or 3 alone. Any hints?

(Edit) Duh. I've changed the code to "else if (val == 333, 666)" and I'm sure that's not correct.

PS your code is gorgeous and works a treat, outsider, but I'm a little stubborn about making my first attempt work. After all, it's my first project conception/solo project (feeling lost for the right word here. Did a few projects off youtube and now I came up with this one, code and all, by myself)

Please post the code as it is now

Good attitude, let's see your latest code.

Still fiddling around, but here it is. Still only getting lights at values 0 and 2

int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
int potPin = A0;
int val;

void setup() {
   pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
}


void loop() {
  val = analogRead(potPin);
  val = map(val, 0, 1023, 0, 180);
  
  if (val == 0) {
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, HIGH);
  } else if (val == 1) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    } else if (val == 2) {
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, LOW);
    } else if (val < 3) {
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      }
    delay(1);
  }

(edit) Ahaa! Another clue! Changed it again to make the final variable == instead of < and now I only get all 3 or only LED3. It seems to be finding the highest number it can and applying that.

Aaaand before I could post again, I set the numbers to "if val == 5, 25 and 50" and it works perfectly.

Way to go! One thing, hitting a single number with a pot may be hit & miss, I use a range like:

if(val >= 22 && val <= 28)
{
   do it;
}

Remove the map(),it is not needed

Test val like this

if (val < 10)
{
  turn on LED1
}
else if (val < 500)
{
turn on LED2
}
else
{
  turn on LED3
}

My previous reply was a bit terse because I was using a 'phone but now back on a PC

  val = analogRead(potPin);
  val = map(val, 0, 1023, 0, 180);

would give map a value between 0 and 180 so testing it for equality to 0, 1, 2 or 3 is not going to get you very far.

Remove the mapping and use the raw values from analogRead() which will between 0 and 1023. Do not test for equality as using a pot is not easily going to give you exact values from analogRead(). You could map the 0 - 1023 range to 0 - 3 but why bother ?