well anyways i wrote my first code, it is meant to have one led turn on for 1 second and then off for 1 second...
a second led is meant to turn on when the first is off and turn off when the first is on...
the code compiled fine, and i ran the code... the first led went on for 1 second and then turned off and stayed off... the second led never went on
this is my code:
int ledaPin = 6; //first (a) LED connected to pin 6
int ledbPin = 8; //second (b) LED connected to pin 8
void setup()
{
pinMode(ledaPin, OUTPUT); // pin 6 to output
pinMode(ledbPin, OUTPUT); // pin 8 to output
}
void loop()
{
digitalWrite(ledaPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledaPin, LOW); // sets the LED off
delay(1000); // waits for a second
if (ledaPin = HIGH)
{
digitalWrite(ledbPin, LOW); // if led a is on, then led b is off
}
if (ledaPin = LOW)
{
digitalWrite(ledbPin, HIGH); // if led a is off, then led b is on
}
}
any help would be appreciated on what i did wrong...
First of all, you are using '=', which assigns the value HIGH to the variable leadaPin. I think what you meant was '==', which compares the two for equality. But this also has a problem, because the value of leadaPin is 6, and the value of HIGH is 1. Asking whether 6 == 1 is not what you meant either.
Perhaps you could try:
if (digitalRead(ledaPin) == HIGH)
but I don't see the point, because every single time you get to that line of code, you know that the value will be LOW, because that is the most recent value you wrote to ledaPin.
If I were trying to do literally what you describe, I would code it as follows:
char state = LOW;
void loop()
{
digitalWrite (ledaPin, state);
state = !state; // toggle the current state
digitalWrite (ledbPin, state);
delay(1000); // wait one second
}
of course you can use just one pin and one resistor, but two LED's. one is going to gnd, the other to 5V. as the pin goes high, led1 is on, as the pin goes low, LED 2 is on and the other off.
this is frequently used in digital probes to see what TTL level a pin has.