just starting, code doesnt work?

hello!
a week ago i bought an arduino starter set, trying to understand and master the arduino.
i tried a few things from the book, but im also trying to build my own creations.

the thing i tried to build is a simple car direction indicator.
2 leds are being controlled by a potentiometer.
scale 0-1023,
0-340 = turn left led on
341-682 = no led
683-1023 = right led

the problem is, that the current situation is as following:
0-340 = left led on
341-682 = both leds on
683-1023 right led on
i dont understand why this is happening?

this is the current code:

int Lled = 2;
int Rled = 3;
int potMeter = A0;
int val = 0;

void setup() {
// put your setup code here, to run once:
pinMode (Lled, OUTPUT);
pinMode (Rled, OUTPUT);
pinMode (potMeter, INPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
val = analogRead(potMeter);
Serial.println(val);
if(val<341)
{
digitalWrite(Lled,HIGH);
digitalWrite(Rled,LOW);
}

if(val>342)
{
if(val<681)
{
digitalWrite(Lled,LOW);
digitalWrite(Rled,LOW);
}
}
if(val>682)
{
digitalWrite(Rled,HIGH);
digitalWrite(Lled,LOW);
}
delay(1);
}

i also tried to write it with switch case construction, resulting in the same problem.

could someone tell my what im doing wrong?

thnx!

could someone tell my what im doing wrong?

You are not posting your code correctly.
You are not using else if statements.
You are not commenting your code

  if(val < 341)
  {
    // Left
    digitalWrite(Lled,HIGH);
    digitalWrite(Rled,LOW);
  }
  else if(val < 681)
  {
     // Middle
     digitalWrite(Lled, LOW);
     digitalWrite(Rled, LOW);
  }
  else
  {
     // Right
     digitalWrite(Lled, LOW);
     digitalWrite(Rled, HIGH);
  }

How are the LEDs wired ?

Does taking an LED pin LOW turn the LED on or off ?

thanks for the fast and good replies! i used the code PaulS sent me, stil does the same thing so i think the problem is somewhere else?

i attached a photo of the configuration:

leftled is powered by port 2, and goes to GND through a 220 ohm resistor so is tightled, but powered by port 3

the potentiometer is connected to 5V and GND, the signal goes to port A0

Your conditionals are not complete (they miss 341 and 342 for instance), and mutually overlapping
(leading to confusion).

PaulS is correct, this is how to select ranges correctly:

if (val < 341)
{
   // val < 341
}
else if (val < 681)
{
  // 341 <= val < 681
}
else
{
  // val >= 681
}

So there’s some other problem - post the code as it is now, explain how the LEDs are connected (or figure
out how to add a photo!)

Create a sketch that JUST blinks the LED on pin 2, printing the state each time it changes.

Does the state of the LED match the output?

Change the sketch to diddle with pin 3, instead.

Does the state of the other LED match the output?

It sounds like you have a hardware problem, not a programming problem.

i attached a photo of the configuration:

To the men's room wall? You didn't attach it here.

hahah im sorry, couldnt post 5 minutes because of a maximum of posts?
the picture should be in this post!

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(2, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(2, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(2, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

i made the left led blink with this code, this appears to work normal

Are the two sides of your bread-board connected? Typically, they are not.

What happens if the the value is 341,342,681 or 682?

It would make sense to change all your “<” & “>” to “<=” (less than or equal to) and “>=” (greater than or equal to) so you can account for any value within your range.

You can also shorten your code some by using && (and). Such as

if(val >= 341 && val <= 681){
    digitalWrite(Rled,LOW);
    digitalWrite(Lled,LOW);
}

EDIT::: Wow, I need to learn to tap faster on this cell phone. Others already mentioned the non-included numbers. But the use of && can still be beneficial.

connected both sides of the breadboard now,

still both LEDs are bright, when they should be low.

what is very odd, is that if i change the states in the 341-681 zone to HIGH, they both go off.

whit this code, it does exactly what i want, but it is completely illogical

int Lled = 2;
int Rled = 3;
int potMeter = A0;
int val = 0;


void setup() {
  // put your setup code here, to run once:
pinMode (Lled, OUTPUT);
pinMode (Rled, OUTPUT);
pinMode (potMeter, INPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
val = analogRead(potMeter);
Serial.println(val);

  if(val < 341)
  {
    // Left
    digitalWrite(Lled,HIGH);
    digitalWrite(Rled,LOW);
  }
  else if(val < 681)
  {
     // Middle
     digitalWrite(Lled, HIGH);
     digitalWrite(Rled, HIGH);
  }
  else
  {
     // Right
     digitalWrite(Lled, LOW);
     digitalWrite(Rled, HIGH);
  }
}

someone knows what is going on now? :slight_smile:

whit this code, it does exactly what i want, but it is completely illogical

If the LEDs are wired backwards, this is the expected behavior.

PaulS: If the LEDs are wired backwards, this is the expected behavior.

how can a led be wired backwards?

WVDW: how can a led be wired backwards?

If you connect one leg to ground (the proper one, of course) and one leg to the digital pin, setting the pin HIGH will turn the LED on. Setting the pin LOW will turn the LED off.

If you connect one leg to 5V and one leg to the digital pin, setting the pin LOW will turn the LED on. Setting the pin HIGH will turn the LED off.

What I wanted you to do was print something in the blink sketch, showing ON and OFF in the Serial Monitor. If you see OFF when the LED is on, and ON when the LED is off, that is a clear indication that the LED is wired incorrectly.

With just yellow wires, too long, and a poor camera angle, it is impossible to tell just how your LEDs are wired.

you sir are a genius :D! it works now, i also fully understand the error! thank you very much!! :)

WVDW: you sir are a genius :D! it works now, i also fully understand the error! thank you very much!! :)

If only you had read and answered the questions in reply #2 you would have got there faster.