Alternate flashing using LED analog pins

When trying to execute this code:

/*
   Alternate Blink
   	1.	Turn on LED1, turn off LED2 for 
   		1 second (at the same time)
   	2.	Turn off LED1, turn on LED2 for 
   		1 second (at the same time)
   	3.	Repeat
*/

 int pinA=A1;
 int pinB=A2;

 void setup() {
   pinMode(pinA,OUTPUT);
   pinMode(pinB,OUTPUT);
 
 }

 void loop() {
   //turn on pinA and turn off pinB
   digitalWrite(pinA,HIGH);
   digitalWrite(pinB,LOW);
 
   //wait 1 sec
   delay(1000);

   //turn off pinA and turn on pinB
   digitalWrite(pinA,LOW);
   digitalWrite(pinB,HIGH);

   //wait 1 sec
   delay(1000);
 }

Only one pin lights up. For example, if i put A1 as the first int, then only A1 lights up. If I put A2 as the first int, only A2 lights up. Why can't they alternate flashing?

Thanks for trying to use code tags but the opening 3 backticks need to be on a line on their own

I have fixed it for you

Oh ok. Thanks

How are the LEDs wired and what value of current limiting resistors are you using ?

Don't see a problem with the code, so perhaps the wiring needs closer inspection. Are you wired to A1 and A2? Do you have both LEDs in the correct way? With resistors on each, I hope.

Both Leds are connected to a 220 ohm resistor.

Also, when I individually test each one, they work.

One resistor or one for each LED ?

I have 2 resistors, one for each LED.

how did you do that test? Swap the two outputs, see if the working one moves or stays the same.

Oh, I just tested again after you said, and the pins are alternating. I think it was a matter of the shield pins not being seated, finicky. Thanks for help!

That is the first mention of a shield

If only you had described how the LEDs were connected as requested ...

Yes, I said it was connected to the pins.

So where does the shield come into it ?

BTW this can be done with one pin, and one resistor with two LEDs

Because the headers on the shield that connect to the arduino apparently was not connecting/had some gunk on it from soldering

If you had initially mentioned that you were using a shield plugged into the Arduino then it is possible that someone would have suggested checking that it was plugged in firmly and making a connection

Ill make sure to say that next time. Thanks.

Sorry to bang on about it, but problems reported here are often caused by something not mentioned initially that emerges later

Good luck with your Arduino projects going forward

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.