if else statement / blink

I'm trying to turn the LED on when it receives a signal in pin_12 (when pin 12 is HIGH) the problem is that the LED turns on always, like it doesn't respect the if-else statement

I'm using the arduino mini pro

I've tried almost everything, let me say: whatever input pin I choose, either I choose that pin to be HIGH or LOW, either I set "trig" to be OUTPUT or INPUT (see the code below to see the variable "trig") either I set "trig" LOW or HIGH even if I write digitalWrite(trig, LOW); either I put the if-else into the loop() or in the setup() I've tried with more than 1 arduino, the results are the same if after I write digitalWrite(13, LOW); it turns off. (as it is supposed)

here's the code: int trig=12;

void setup(){ pinMode(trig, INPUT); digitalWrite(trig, LOW); }

void loop(){ if(digitalRead(trig == HIGH)) { digitalWrite(13, HIGH); } }

You may have a bit of a logic problem.

 void loop(){
    if(digitalRead(trig) == HIGH)  {   
      digitalWrite(13, HIGH);
    } else {
      digitalWrite(13, LOW);
    }
 }

You could also do

void loop(){
   digitalWrite(13, digitalRead(trig));
}

Look at the reference for the functions.

http://arduino.cc/en/Reference/HomePage http://arduino.cc/en/Reference/DigitalRead

you are passing the result of 'trig == HIGH' to digital read rather than a pin number.

 byte trigger=12; 
 byte ledpin = 13;        
   
 void setup(){
   pinMode(trigger, INPUT); 
   digitalWrite(trigger, LOW);
   pinMode(ledpin, OUTPUT);
   digitalWrite(ledpin, LOW);
 }

 void loop(){
    if(digitalRead(trigger == HIGH))
    { 
    digitalWrite(ledpin, HIGH);
    }
    else
    {
    digitalWrite(ledpin, LOW);
    }
 }

Can you tell why I made those changes?

why you made those changes? thanks

softuino: why you made those changes? thanks

Two problems with your code:

if (digitalRead(pin==HIGH))

is not the same as

if (digitalRead(pin)==HIGH)

Notice the difference?

Secondly, when exactly is the LED going to be turned off? You don't ever tell it to turn off..

Post-N-Run

I’m of the opinion that multiple examples of doing the same thing, but in different ways, can be educational. Of course it can also be overwhelming and confusing.

With that said!

Something a bit more self documenting thus requiring less comments that tend to drift from the actual codes function.

It also adapts to changes such as perhaps ‘BUTTON_UP’ needing to be reversed which can be done by simply swapping the values assigned to BUTTON_UP and BUTTON_DOWN in a single place instead of everywhere used in the code.

// <http://arduino.cc/forum/index.php/topic,107260.msg805160.html#new>

const uint8_t   pinBUTTON       = 12;
const uint8_t   pinLED          = 13;       

const uint8_t   LED_OFF         =  LOW;     // SYNONYM, FOR READABILITY
const uint8_t   LED_ON          = HIGH;     // SYNONYM, FOR READABILITY

const uint8_t   BUTTON_UP       =  LOW;     // SYNONYM, FOR READABILITY
const uint8_t   BUTTON_DOWN     = HIGH;     // SYNONYM, FOR READABILITY

const uint8_t   PULL_UP_DISABLE =  LOW;
const uint8_t   PULL_UP_ENABLE  = HIGH;


void loop()
{
    if ( BUTTON_DOWN == digitalRead(pinBUTTON) )
    {
        digitalWrite(pinLED, LED_ON);
    }
    else
    {
        digitalWrite(pinLED, LED_OFF);
    }
}

void setup()
{
    pinMode(pinBUTTON, INPUT); 
    digitalWrite(pinBUTTON, PULL_UP_DISABLE);

    pinMode(pinLED, OUTPUT);
    digitalWrite(pinLED, LED_OFF);
}

Same as this one that uses a ‘ternary’ expression to set the LED state

// <http://arduino.cc/forum/index.php/topic,107260.msg805160.html#new>

const uint8_t   pinBUTTON       = 12;
const uint8_t   pinLED          = 13;       

const uint8_t   LED_OFF         =  LOW;     // SYNONYM, FOR READABILITY
const uint8_t   LED_ON          = HIGH;     // SYNONYM, FOR READABILITY

const uint8_t   BUTTON_UP       =  LOW;     // SYNONYM, FOR READABILITY
const uint8_t   BUTTON_DOWN     = HIGH;     // SYNONYM, FOR READABILITY

const uint8_t   PULL_UP_DISABLE =  LOW;
const uint8_t   PULL_UP_ENABLE  = HIGH;


void loop()
{
    digitalWrite(pinLED, ((BUTTON_DOWN == digitalRead(pinBUTTON)) ? LED_ON : LED_OFF));
}

void setup()
{
    pinMode(pinBUTTON, INPUT); 
    digitalWrite(pinBUTTON, PULL_UP_DISABLE);

    pinMode(pinLED, OUTPUT);
    digitalWrite(pinLED, LED_OFF);
}

I was talking about the changes GoForSmoke did

he didn't change that instruction, but attibuted variables to pin numbers, does it change anything ? I have to check out

GoForSmoke still got it wrong by not spotting the missing parenthesis in the conditional, which means that the digitalRead could only ever be of pin zero or pin one.

I really should have put that into the IDE and checked but I didn't have much time. The line should read as Arrch posted. BTW, those kind of errors are pretty normal when typing fast and part of what makes debugging as much fun as it is (no fun at all).

Softuino, the big difference is where I put the else in to turn the led off when the button isn't pushed. If you only have code to turn the led on, it won't turn back off.

Keeping pin numbers in byte variables is minor but good practice, but then it's good practice to check the code and I didn't do that....