Newbie :) 'if' help

Hi guys, got my self an arduino the other day :) ive just been going through the beginner tutorials and sketches and learning all the basics, i started learning about 'if' statements and thought i could try and use one to turn two led's one and off by modifying the blink example, for example i had one led running as a normal blink but then used the second led in the if statement to do the opposite of what led 1 does. But for some reason it wont work, its probably something obvious, led 1 blinks fine but led 2 just stays off. ill add the sketch sorry if its messy, onty had it three days :P Thanks! (sorry i dont know how to add the sketch properly)

int ledPin1 = 5; int ledPin2 = 7;

void setup() {

pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); }

void loop() { int ledPin1value = digitalRead(ledPin1); digitalWrite(ledPin1, HIGH); delay(1000); digitalWrite(ledPin1, LOW); delay(1000);

if (ledPin1value = HIGH) { digitalWrite(ledPin2, LOW); } else { digitalWrite(ledPin2, HIGH);

}

}

I understand what your trying to do buts its allot easier to just do this.

BTW code is added using the # button in the message edit field.

int ledPin1 =  5;  
int ledPin2 = 7;


void setup()   {                

 pinMode(ledPin1, OUTPUT);  
pinMode(ledPin2, OUTPUT);  
}

void loop()                    
{
 int ledPin1value = digitalRead(ledPin1);
 digitalWrite(ledPin1, HIGH);
 digitalWrite(ledPin2, LOW);  
 delay(1000);                  
 digitalWrite(ledPin1, LOW); 
 digitalWrite(ledPin2, HIGH);

delay(1000);

}

But the problem with your code is you need '==' not one '='.

To expand on digimike's explanation:

The "=" operator is what is known as the assignment operator; it assigns a value to a variable. The "==" operator is known as a comparison operator (it is one of many); in this case the comparison is for equality.

Comparison operators also have a couple of caveats (IIRC) in C:

  1. They can only compare primitive data types (ie, int, char, byte, etc) - you cannot compare strings (use the strcmp() function for that).

  2. You should only do your comparison on values/variables of the same primitive type; while you don't absolutely have to, your results might not match in all cases (ie, comparing an int to a byte, for instance).

In your if() statement, what it was doing was the following steps:

1) Assigning the value HIGH to the variable ledPin1value.

2) The result of that assignment evaluates to TRUE, so the if() statement always takes the "then" path, not the "else" path.

Do some more reading and investigation into C control structures and such; you are learning, don't get frustrated, and continue to ask if you don't understand.

Lastly, next time you post some code, using the code button (#) in the comment editor to post it properly:

Without:

void setup() { int test = HIGH; }

With (using the code (#) button):

void setup() {
  int test = HIGH;
}

Just highlight your code and click the button...

:)

Thanks digimike and cr0sh, i really appreciate the help and the effort you put into your reply :) i understand it now, just need to keep learning :p i tried changing the code to == but led 2 just stayed on now haha never mind, ill figure it out :) anyway, thanks again :)

Rich

int ledPin1value = digitalRead(ledPin1);

ledPin1 was declared an OUTPUT, and you're trying to read from it. What's up with that? What do you expect to get from the LED? Light level? Color?

sorry i did say ive only had it three days :-/ id appreciate it much more if you would be helpful rather than cynical, everyone starts somehwere.

Your arduino IDE comes with alot of useful stuff for starting out and it comments on them also in the sketches.

I'm not being cynical. You'll learn a lot more if you read the documentation than you will by copying and pasting code you don't understand.

If you were to look at the pinMode, digitalRead, and digitalWrite documentation, you'd see that digitalRead works on pins that have been declared as INPUT, and digitalWrite works on pins declared as OUTPUT.

Think about input pins like a microphone, and output pins like a speaker. You wouldn't try to use a speaker as an input device, would you? You wouldn't try to plug the microphone into the audio out port, would you?

All I was suggesting, apparently too subtly, was that you read the documentation, and think about what trying to read from an OUTPUT port meant. It's like plugging a speaker into the audio in port, and wondering why you can't get any input.

digitalWrite works on pins declared as OUTPUT.

OTOH, it also works on pins configured as INPUT too! ;)

digitalWrite works on pins declared as OUTPUT.

OK. How’s this:

digitalWrite is intended to be used on pins declared as OUTPUT.

PaulS, no, there is a valid reason for digitalWrite() to be used on an INPUT mode pin. It engages or disengages the internal pull-high resistor. About all you can say is that digitalWrite() is intended to be used on an I/O pin. :)

It engages or disengages the internal pull-high resistor.

Point taken. I don't think that this feature was what OP was making use of, though.

digitalRead also works on an output pin - it returns the current level of the pin.