Pages: [1]   Go Down
Author Topic: how can i fix the following code?  (Read 438 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8695
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This should do what you want.
Code:
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);
}
Logged

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

France
Offline Offline
God Member
*****
Karma: 34
Posts: 987
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or, a nice way to invert the states:
Code:
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);
}
« Last Edit: October 07, 2012, 02:17:57 pm by guix » Logged

Pages: [1]   Go Up
Jump to: