Trouble using if statements (yes I'm new)

I'm trying to program and LED to be turned on only if a certain LED is turned on.

For example while Green LED is lit, Red LED is lit as well.

I've tried over a million variations of using this code but nothing worked.

Can anyone help?

if (pinMode 7) = 2500) { digitalWrite(11, HIGH); // Turns LED on pin 11 on delay(2500); // LED on pin 11 remains on for 2.5 seconds }

ThunderBob: I'm trying to program and LED to be turned on only if a certain LED is turned on.

For example while Green LED is lit, Red LED is lit as well.

I've tried over a million variations of using this code but nothing worked.

Can anyone help?

if (pinMode 7) = 2500) { digitalWrite(11, HIGH); // Turns LED on pin 11 on delay(2500); // LED on pin 11 remains on for 2.5 seconds }

if (pinMode 7) = 2500) this looks wrong... if(digitalWrite 7) = HIGH try that. see if it works and add in your lower coing too

I tried that. I got this message "expected ')' before numeric constant"

and I've put ) before 2500 and still get the same message.

this should works:

if ( digitalRead( 7 ) ) { digitalWrite(11, HIGH); // Turns LED on pin 11 on delay(2500); // LED on pin 11 remains on for 2.5 seconds }

yeahh it compiles with no errors now but the LEDs aren't operating like they should. LEDpin 7 isn't even turning on.

I thought you turn it (7) on before, and in this statement just checking status of it, and depends on result working with 11. plz., post all code in #

PinMode() is not going to help you. http://arduino.cc/en/Reference/PinMode

and digitalRead() won't work because he's outputting to the led

why would you need to do a check? couldn't you just put the digital write code for the red led with the code that turns the green led on?

if you want one or the other then I don't think you are able to check the Output of a port whether it is high or not... i could be wrong, but i've never looked into it.

if your intention was if green is on turn red off and visa versa

then i would do this

void loop(){
  if (var == 0){
    digitalWrite(7, HIGH);
    digitalWrite(11, LOW);
    delay(2500)
    var = 1
  }
  if (var == 1){
    digitalWrite(11, HIGH);
    digitalWrite(7, LOW);
    delay(2500)
    var = 0
  }

}

now when you learn a little bit more you will see that this will become unproductive, but for this example it will work.

cheers, Dave.

In your code silver how would I declare 'var' in the scope?

Here is the code.

// Connect green LED to pin 13 and 7 // Connect yellow LED to pin 12 and 6 // Connect red LED to pin 11 and 5

void setup() { // declare pin 9,10,13 to be outputs: pinMode(11, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); pinMode(7, OUTPUT); pinMode(6, OUTPUT); pinMode(5, OUTPUT); }

void loop(){ digitalWrite(13, HIGH); // Turns LED on pin 13 on delay(2500); // LED on pin 13 remains on for 2.5 seconds digitalWrite(13, LOW); // Turns LED on pin 13 off digitalWrite(12, HIGH); // Turns lED on pin 12 on delay(2500); // LED on pin 12 remains on for 2.5 seconds digitalWrite(12, LOW); // Turns LED on pin 12 off digitalWrite(11, HIGH); // Turns lED on pin 11 on delay(2500); // LED on pin 11 remains on for 2.5 seconds digitalWrite(11, LOW); // Turns LED on pin 11 off

if ( digitalRead( 7 ) ) { digitalWrite(11, HIGH); // Turns LED on pin 11 on delay(2500); // LED on pin 11 remains on for 2.5 seconds }

digitalWrite(7, HIGH); delay(2500); digitalWrite(7, LOW); // Turns LED on pin 13 off digitalWrite(6, HIGH); // Turns lED on pin 12 on delay(2500); // LED on pin 12 remains on for 2.5 seconds digitalWrite(6, LOW); // Turns LED on pin 12 off digitalWrite(5, HIGH); // Turns lED on pin 11 on delay(2500); // LED on pin 11 remains on for 2.5 seconds digitalWrite(5, LOW); // Turns LED on pin 11 off }

my bad i just included the loop

just put this at the top of your program

int var = 0;

also try to use the Code tags, you'll earn some brownie points from everyone here if you do.

lol ok. So by top you mean this?

void loop()
{int var = 0;}{
  if ( var == 1 ; ){
    digitalWrite(7, HIGH);
    digitalWrite(11, LOW);
    delay(2500)
    var = 1
  }
  if (var == 1){
    digitalWrite(11, HIGH);
    digitalWrite(7, LOW);
    delay(2500)
    var = 0
  }

}

i think he means like this

int var = 0;
void loop()
{
  if ( var == 1 ; ){
    digitalWrite(7, HIGH);
    digitalWrite(11, LOW);
    delay(2500)
    var = 1
  }
  if (var == 1){
    digitalWrite(11, HIGH);
    digitalWrite(7, LOW);
    delay(2500)
    var = 0
  }

}

ThunderBob: I'm trying to program and LED to be turned on only if a certain LED is turned on.

For example while Green LED is lit, Red LED is lit as well.

I've tried over a million variations of using this code but nothing worked.

Can anyone help?

if (pinMode 7) = 2500) { digitalWrite(11, HIGH); // Turns LED on pin 11 on delay(2500); // LED on pin 11 remains on for 2.5 seconds }

this is actually the stupidest use of code when you think of what you want to do. You want 2 LED's to be on at the same time and off at the same time... Just write digitalWrite(9, HIGH); and digitalWrite(8, HIGH); and your done. I mean theres too much work involved here for just keeping 2 LED's on

@weirdo I still get this error message.

expected ')' before ';' token

and trust me doing that leads to more errors lol

wow I'm having a rough night!

again my bad what that means is I forgot ";" at the end of the lines. thats what happens when i type it out in the message instead of testing

this is the full code.

int var = 0;

void setup(){

}

void loop(){
  if (var == 0){
    digitalWrite(7, HIGH);
    delay(2500);
    var = 1;
  }
  if (var == 1){
    digitalWrite(11, HIGH);
    delay(2500);
    var = 0;
  }

}

Wow that code actually got me a good foot in the right direction. THANK YOU FORUM!

SilverCG: ``` int var = 0;

void setup(){

}

void loop(){  if (var == 0){    digitalWrite(7, HIGH);    delay(2500);    var = 1;  }  if (var == 1){    digitalWrite(11, HIGH);    delay(2500);    var = 0;  }

}

The first "if" test checks if var is 0 and if so sets it to 1. Then the second "if" test finds it is 1 and sets it back to zero.

How about:

int var = 0;

void setup(){
}

void loop()
{

  if (var == 0)
    digitalWrite(7, HIGH);
  else 
    digitalWrite(11, HIGH);

  // pause
   delay (2500);

  // toggle var
   var = !var;
 }

Hi ThunderBob

This will also work:

boolean ledState = 0;
int led1 = 7;
int led2 = 11;

void setup(){
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
}

void loop(){
  ledState = !ledState; // ledState will change from 0 to 1 to 0 to..
  digitalWrite(led1,ledState);
  digitalWrite(led2,ledState);
  delay(1000);
}

-Fletcher