If - Else

Hi all

I am trying to test IF - ELSE.

This is what I wrote

const int readPin = 8;

void setup() { pinMode (11, OUTPUT); // Yellow pinMode (readPin, INPUT); pinMode (12, OUTPUT); // Red

}

void loop(){ if digitalRead (readPin, HIGH);

{ digitalWrite (11, HIGH);// Yellow LED on } else { digitalWrite (12, HIGH);// Red LED on }

}

what happens at the moment is that pin 8 is not connected to a 5V pin but the yellow LED is on and the Red is off.

Could you please help me to find what's wrong?

Thanks a lot guys :)

if digitalRead (readPin, HIGH);Delete the trailing semicolon.

And because you made it just an input and not connect it it's floating. A pin that's not connected does not mean it's low, it's like undefined, it can be high or low. To overcome that, use pinMode(readPin, INPUT_PULLUP). This will pull a pin high when it's not connected. So connect the pin to GND to make it switch ;)

if digitalRead (readPin, HIGH);

Lose the semicolon and the HIGH, add some parentheses and USE CODE TAGS.

Ok, thank you guys

This is how I re-wrote it

void setup() { pinMode (11, OUTPUT); // Yellow pinMode (8, INPUT); //jumper to GND pinMode (12, OUTPUT); // Red pinMode(8, INPUT_PULLUP); //Thanks for the tip :)

}

void loop(){ if (digitalRead (8)==HIGH) // jumper not connected to GND

{ digitalWrite (11, HIGH);// Yellow LED on digitalWrite (12, LOW) ;//Red LED off } else { digitalWrite (12, HIGH);// Red LED on digitalWrite (11, LOW);//Yellow LED off }

}

it works :)

. . . and that's why I asked you to USE CODE TAGS.

const byte YELLOW = 11;
const byte RED = 12;
const byte JUMPER = 8;

void setup() 
{
  pinMode (YELLOW, OUTPUT);
  pinMode (RED, OUTPUT);
  pinMode(JUMPER, INPUT_PULLUP);
}

void loop()
{
  int jumperVal = digitalRead (JUMPER);
  digitalWrite (YELLOW, jumperVal);
  digitalWrite (RED, ! jumperVal);
}

AWOL: . . . and that's why I asked you to USE CODE TAGS.

const byte YELLOW = 11;
const byte RED = 12;
const byte JUMPER = 8;

void setup() {   pinMode (YELLOW, OUTPUT);   pinMode (RED, OUTPUT);   pinMode(JUMPER, INPUT_PULLUP); }

void loop() {   int jumperVal = digitalRead (JUMPER);   digitalWrite (YELLOW, jumperVal);   digitalWrite (RED, ! jumperVal); }

I'm sorry AWOL, I tought CODE TAGS were the desctiptions after //

Thanks! :)