# How to do if or ? (beginners question)

Oke so i m still on the starter kid guides, i m pritty new with all this stuff but i m starting to learn pritty fast (in my oppinion).
ATM i just follow the guides and then i try to figure out something extra to add to challange myself. Im at the servo motor now where you let a servo motor rotate to left or right with a potentiometer, meaning when the potentiometer gives an analoge reading of 0 the motor will turn to the left. Everywhere in between the motor is set at an angle, i guess for the guys who read this thay probably get the idea. Oke so i got this whole guide setup and it works perfect. Now to challange myself i wanted 2 LED burning on sertain points of the motor’s placement. Lets say i want 1 Red LED burn when the motor is all the way to the left (giving me a indication the motor is all the way at the left). And i want a green LED burn when the motor is all the way to the right. Since i cant measure the motors location (atleast not that i know of) i use the potmeter to do so.

So i started off with this,

``````if(potMeeting == 0){
digitalWrite(2,HIGH);
}
else{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
``````

So if the Potmeter is at 0 the led will burn, easy enough. Now i wanted to do it for the other side aswell and i did this:

``````if(potMeeting == 0){
digitalWrite(2,HIGH);
}
else{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
}
if(potMeeting == 1023){
digitalWrite(3,HIGH);
}
else{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
}
``````

But it looks like this onley works for the right LED and it cancles out the left LED.
Could somebody help me with this matter on how i should approach it? i been looking on google abit but couldent realy find what i needed. Also i wanted to change the values exept for just 0 i would like to make it BETWEEN 0 and 20, and then the LED burns.

here is a full print of the script i made.

``````#include <Servo.h>
Servo myServo;
int const potPin = A0;
int potMeeting;
int hoek;

void setup(){
myServo.attach(9);
Serial.print(9600);//Port
pinMode(2,OUTPUT);//LED left
pinMode(3,OUTPUT);//LED right
}
void loop (){
Serial.print("potMeeting: ");
Serial.print(potMeeting);
hoek = map(potMeeting, 0, 1023, 0, 179);
Serial.print("hoek: ");
Serial.print(hoek);
myServo.write(hoek);
delay(5);

if(potMeeting == 0){
digitalWrite(2,HIGH);
}
else{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
}
if(potMeeting == 1023){
digitalWrite(3,HIGH);
}
else{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
}
}
``````

Its partly dutch so here are some translations
(potMeeting = potMeasurement or potValue)
(hoek = angle)

I hope somebody could help me out and give me some tips, i realy appriciate it.

I don't see any problems that jump out at me. You may want to give your pot reading a bit more latitude.

rather than if(potMeeting == 1023) try if(potMeeting > 1000) and also give some latitude on the low side.

UPDATE: Yes, I see a code problem. You should remark/remove a line. Change:

``````  if(potMeeting == 1023){
digitalWrite(3,HIGH);
}
else{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
}
``````

TO:

if(potMeeting == 1023){ digitalWrite(3,HIGH); } else{ //digitalWrite(2,LOW); digitalWrite(3,LOW); }

yes, but it won't be enough :

if(potMeeting == 0){ digitalWrite(2,HIGH); } else{ digitalWrite(2,LOW); ** digitalWrite(3,LOW);** } if(potMeeting == 1023){ digitalWrite(3,HIGH); } else{ digitalWrite(2,LOW); digitalWrite(3,LOW); }

then, if potMeeting = 1023 (different than 0 ), pin 3 goes LOW and HIGH again and again same thing for output 2 . you could avoid that by adding an "if" whith the else :

else if(potMeeting!=1023){ ..... }

or use a switch instruction :

switch(potMeeting) { case 0 :instructions; break; case 1023 : instructions ; break; default : instructions; }

the "default" part is executed if none of the "case" conditions are met

Good call.
If you use a case, then you can’t use < and > can you?

no, but no need to use "and" , I think. You can do whatever you want, using or not using "break" if you don't put a "break; " after a "case" , then the following "case" will be evaluated even if the previous one was true .

Right.
The possible question I was looking at, if the pot does not go ALL the way to ground, or 5V, the OP could check for <9 and check for > 1000. I think some pots do not go all the way (like some girls I know) LOL

yes, it would be wise to give a little margin
I may be wrong, but I think this should work :

switch(potMeeting) {
case (<9) : …;
break;
case (>= 1023) : …;
break;
default : … ;

not even sure the ( ) are mandatory .

I may be wrong

Yup. you are.

Thanks guys for the help, like jack sed in the first post actually worked. I forgot that as long as my potvalue was higher then 0 both leds would just shut off period, overwriting the idea to put on the other LED on at value 1023.

jackwp: Change:

``````  if(potMeeting == 1023){
digitalWrite(3,HIGH);
}
else{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
}
``````

TO:

if(potMeeting == 1023){ digitalWrite(3,HIGH); } else{ //digitalWrite(2,LOW); digitalWrite(3,LOW); }

AWOL:

I may be wrong

Yup. you are.

Right, thanks :) I should have checked before I wrote it ;) but I was almost sure..... :blush:

alnath:

AWOL:

I may be wrong

Yup. you are.

Right, thanks :) I should have checked before I wrote it ;) but I was almost sure..... :blush:

I've read that, instead, you can give case a range (eg 0 to 9) but can't remember the syntax.

I would like to see that.
I don’t use case often, because it is so restrictive ( no < > between), but if I misunderstand, I would like to know how. Thanks

``````case 0... 9 :
``````