Go Down

Topic: how can i fix the following code? (Read 518 times) previous topic - next topic

yangfizz

int led1=13;
int led2=12;

int led2state=LOW;

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

void loop()
{
digitalWrite(led1,HIGH);
delay(1000);
digitalWrite(led1,LOW);
delay(1000);
if (led1==LOW)
{led2state==HIGH;}
}



i am trying to write code that when one led is on the other becomes off, and vice versa.

yangfizz

int led1=13;
int led2=12;

int led1state=HIGH;
int led2state=LOW;


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

void loop()
{
digitalWrite(led1,led1state);
delay(1000);
digitalWrite(led1,led1state=LOW);
delay(1000);
if (led1state=LOW)
{
  led2state=HIGH;
  digitalWrite(led2,led2state);
}

}

here is the new code, i still don't know why it doesn't work.

tuxduino

Hint: = is an assigment, == is a test.

johnwasser

This should do what you want.
Code: [Select]

const int led1 = 13;
const int led2 = 12;

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

void loop()
{
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
delay(1000);
digitalWrite(led1,LOW);
digitalWrite(led2,HIGH);
delay(1000);
}

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

guix

#4
Oct 07, 2012, 09:13 pm Last Edit: Oct 07, 2012, 09:17 pm by guix Reason: 1
Or, a nice way to invert the states:
Code: [Select]

const int led1 = 13;
const int led2 = 12;

bool led1state = HIGH;

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

void loop()
{
digitalWrite(led1, led1state);
digitalWrite(led2, !led1state);
led1state = !led1state;
delay(1000);
}

Go Up