Go Down

Topic: [SOLVED] Potentiometer to sequentially light LEDs (Read 503 times) previous topic - next topic

PlagueDoc

Jan 13, 2018, 08:40 am Last Edit: Jan 13, 2018, 10:22 am by PlagueDoc Reason: Solved
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.

Code: [Select]

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.

UKHeliBob

Code: [Select]
  if (val = 0)
= is for assignment
== is for comparison
Your code is setting val to 0

Code: [Select]
  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()

Code: [Select]
  delay(1);Why ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PlagueDoc

#2
Jan 13, 2018, 08:54 am Last Edit: Jan 13, 2018, 09:23 am by PlagueDoc
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.
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.

PlagueDoc

Ahaa.

...my breadboard is faulty.

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

outsider

#4
Jan 13, 2018, 09:27 am Last Edit: Jan 13, 2018, 09:28 am by outsider
Are you sure your LEDs are wired right ( short leg is cathode, goes to GND )? Try this anyway:
Code: [Select]

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.  :)

UKHeliBob

Quote
...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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

outsider

#6
Jan 13, 2018, 09:43 am Last Edit: Jan 13, 2018, 09:50 am by outsider
Like this:

PlagueDoc

#7
Jan 13, 2018, 09:45 am Last Edit: Jan 13, 2018, 09:54 am by PlagueDoc
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)

UKHeliBob

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

outsider

Good attitude, let's see your latest code.

PlagueDoc

#10
Jan 13, 2018, 10:04 am Last Edit: Jan 13, 2018, 10:09 am by PlagueDoc
Still fiddling around, but here it is. Still only getting lights at values 0 and 2

Code: [Select]


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.

outsider

Way to go! One thing, hitting a single number with a pot may be hit & miss, I use a range like:
Code: [Select]
if(val >= 22 && val <= 28)
{
   do it;
}

UKHeliBob

Remove the map(),it is not needed

Test val like this
Code: [Select]

if (val < 10)
{
  turn on LED1
}
else if (val < 500)
{
turn on LED2
}
else
{
  turn on LED3
}
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

UKHeliBob

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

Code: [Select]
  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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up