Newbie 'if' question

Hi,

I'm after a bit of advise on the if command

I have played around with the blink sketch and now have this:

int led = 10;
int led2 = 11;
int led3 = 13;
int check = digitalRead(12);

void setup() {
// initialize digital int led as an output.
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(12, INPUT);
pinMode(led3, OUTPUT);
}

void loop() {
digitalWrite(led, HIGH);
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
if (check == HIGH)
{digitalWrite(led3, HIGH);}
}

My theory was if pin 12 went high it put pin 13 to high, however this isn't happening. Not sure if this is a hardware or software problem, to make the pin go high I am putting 5v from the board onto pin12

Any advise would be appreciated,

Thanks

You placed a resistor between the 5V pin and the digital pin 12?

  if (check == HIGH)
  {digitalWrite(led3, HIGH);}
}

Why not just do:

if (digitalRead(12)==1){
digitalWrite(led3,HIGH);
}

The variable "check" is not really needed imho.

You need to read the pin in loop. You're not doing that.

int led = 10;
int led2 = 11;
int led3 = 13;
int check; 

void setup() {
  // initialize digital int led as an output.
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(12, INPUT);
  pinMode(led3, OUTPUT);
}


void loop() {
  digitalWrite(led, HIGH);
  digitalWrite(led2, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
  check = digitalRead(12); 
  if (check == HIGH)           
  {digitalWrite(led3, HIGH);}
}

As noted above, you can replace

  check = digitalRead(12); 
  if (check == HIGH)

with this - the important thing is that you're digitalRead()ing the pin every time

  if (digitalRead(12) == HIGH)

or just:

  if (digitalRead(12))

what is the ==1 referring to, I thought it could only see if it was high or low?

And I didn't have a resistor, just tried with and didn't make any difference

ok I have updated it to

if (digitalRead(12) == HIGH)
{digitalWrite(led3, HIGH);}

and now got it working,

so if this is in a loop when the pin goes low it should turn led3 off? At the moment it isn't doing that when I put pin12 to ground it still stays lit. I can put an 'else' in to put it to low, just trying to understand why I need to?

digitalWrite(led3,digitalRead(12));

Breazle:
ok I have updated it to

if (digitalRead(12) == HIGH)
{digitalWrite(led3, HIGH);}

and now got it working,

so if this is in a loop when the pin goes low it should turn led3 off?

No, that code only has a line to write it HIGH. There is no line anywhere that says to write it LOW ever. THis code just says when pin 12 is HIGH write the led HIGH, it doesn't say what to do when pin 12 is LOW.

int check = digitalRead(12);

placed outside of any code section of the sketch, is nonsense.

If you made it:

int check() {
  return (digitalRead(12));

and then

if (check())

as your test would work.