int ledOne=7;
int ledTwo=8;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(ledOne, HIGH); // turn on ledOne
delay(1000); // wait for a second
if (ledOne=HIGH){
digitalWrite (ledTwo,LOW); // when ledOne turns on, ledTwo turns off
}
digitalWrite(ledOne, LOW); // turn off ledOne
delay(1000); // wait for a second
if (ledOne=LOW){
digitalWrite (ledTwo,HIGH); //when ledOne turns off, ledTwo turns on
}
}
I have a very basic code that just switches LEDs on and off. One is supposed to turn on and off based on time. The other LED is supposed to turn on and off when the other turns off and on. When I run my code, ledOne just stays on. Nothing else happens. Can anyone explain why? The only thing I could think of to try was adding "==" instead of just "=". This got ledOne to blink again, but nothing happens with ledTwo.
LarryD:
You have to use ==
Look into the digitRead function with ledOne (hint)
Also look at the, else, statement.
Very good hint! Thanks. However, now with my updated code, I have come across and oddity. When I ready ledOne as HIGH, ledTwo should be set to LOW, and vice versa. However, in order to have them blink apart from each other, I have had to set ledTwo to HIGH when ledOne is read as HIGH for ledTwo to off. How come?
int ledOne=7;
int ledTwo=8;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output. ????Huh???? Where are you doing that?
//Erroneous comments are worse than useless.
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(ledOne, HIGH); // turn on ledOne
digitalWrite (ledTwo,LOW); // turn off ledTwo
delay(1000); // wait for a second
digitalWrite(ledOne, LOW); // turn off ledOne
digitalWrite (ledTwo,HIGH); //turn on ledTwo
delay(1000); // wait for a second
}
During those 1 second delays, your Arduino will not do anything else and sit there, twiddling its thumbs.
I suggest that you look up the 'Blink without delay' example in the IDE or the examples page of this site.
Read, understand and embrace the principle.