simultaneously turn on 2 led's

hi all i'm using this sketch to flash led's at the end of the sketch i would like to flash leds on pins 4 and 7 simultaneously can any one tell me if its possible and how to do it thanks marko

/* Blink Turns on an LED This example code is in the public domain. */

void setup() { // initialize the digital pin as an output. // Pin 2/7 has an LED connected on most Arduino boards: pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); }

void loop() { digitalWrite(2, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(2, LOW); // set the LED off delay(0); // wait for a second

digitalWrite(3, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(3, LOW); // set the LED off delay(0); // wait for a second

digitalWrite(4, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(4, LOW); // set the LED off delay(0); // wait for a second

digitalWrite(5, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(5, LOW); // set the LED off delay(0); // wait for a second

digitalWrite(6, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(6, LOW); // set the LED off delay(2000); // wait for a second digitalWrite(7, HIGH); // set the LED on delay(50); // wait for a second digitalWrite(7, LOW); // set the LED off delay(50);

}

As you have done everything else except turn both on at once, delay, then turn them off. You can do two digitalWrite together.

how do i digital write 2 pins ?

i tried this but one led lights up befor the other but i want them simultaneously

digitalWrite(3, HIGH); digitalWrite(7, HIGH); delay(50); digitalWrite(3, LOW); digitalWrite(7, LOW);

volatile uint8_t* led1 = portOutputRegister(digitalPinToPort(7)); //Note the 7 is the arduino pin number
volatile uint8_t* led2 = portOutputRegister(digitalPinToPort(3)); //As is the 3
byte on1 = digitalPinToBitMask(7);
byte on2 = digitalPinToBitMask(3);
byte off1 = ~on1;
byte off2 = ~on2;

*led1 |= on1;
*led2 |= on2;
delay(50); 
*led1 &= off1;
*led2 &= off2;

That will get you alot closer. digitalWrite() is very slow, so I have written the code above to do the same as it, but to brake it up to first do all the necessary calculations, and then change the pin. This will get them to changing within 2 clock cycles.

If the two are on the same port, which if it is an Arduino Uno, then D3 and D7 are, you can get them to change at exactly the same time:

volatile uint8_t* leds = portOutputRegister(digitalPinToPort(7)); //Bot on the same port, so only need one.
byte on = digitalPinToBitMask(7) | digitalPinToBitMask(3);
byte off = ~on;

*leds |= on;
delay(50); 
*leds &= off;

i'm using a mega so what you are saying is use another pin to connect to both led's ? am i right and what is the best transistor to use for 2 amps

The first of those two blocks will work for an Arduino Mega using 7 and 3. There will be a 125ns delay between the two LEDs changing, but that is so small its not worth worrying about.

thats acceptable 125ns but where would i put the code in the sketch ?

If the LEDs are on the same Port, then Direct Port Manipulation is a way to light them up at the same time: http://www.arduino.cc/playground/Learning/PortManipulation

DDRC = B11111111; // sets all pins on Port C to outputs

PORTC = PORTC & B11111100; // makes Port C bit 1, 0 Low, leaves the rest alone

PORTC = PORTC || B00000011; // makes Port C bit 1, 0 High, leave the rest alone

On a mega, they wouldn’t be on the same port. But that direct pin manipulation is the same as what this code does. My version just uses the arduino cores functions to determine the bit masks and port register for you from the Arduino Digital pin names.

This would be how you could add my code:

volatile uint8_t* digital7Port;
volatile uint8_t* digital3Port;
byte digital7On;
byte digital3On;
byte digital7Off;
byte digital3Off;
//These defines make it easier to follow any code you write.
#define digitalWrite3High *digital3Port |= digital3On 
#define digitalWrite7High *digital7Port |= digital7On
#define digitalWrite3Low *digital3Port &= digital3Off 
#define digitalWrite7Low *digital7Port &= digital7Off

void setup(){
  //Determine the correct port and bit masks.
  digital7Port = portOutputRegister(digitalPinToPort(7));
  digital3Port = portOutputRegister(digitalPinToPort(3));
  digital7On= digitalPinToBitMask(7);
  digital3On = digitalPinToBitMask(3);
  digital7Off = ~digital7On;
  digital3Off = ~digital3On ;

  pinMode(7,OUTPUT);
  pinMode(3,OUTPUT);
  ...
  The rest of your setup
  ...
}

void loop(){
  ...
  Your code
  ...
  
  //Write this when you want to change the pins.
  digitalWrite3High; //This writes Digital 3 high
  digitalWrite7High; //This writes Digital 7 high
  delay(50);
  digitalWrite3Low;  //This writes Digital 3 low
  digitalWrite7Low;  //This writes Digital 7 low
}

markthespark: i tried this but one led lights up befor the other but i want them simultaneously

digitalWrite(3, HIGH); digitalWrite(7, HIGH); delay(50); digitalWrite(3, LOW); digitalWrite(7, LOW);

Could you see one lighting up before the other?

markthespark: how do i digital write 2 pins ?

use 2 pins on the same port. then you read the port into a byte, set the bits for your two pins and write that back to the port. The pins will change as simultaneous as possible. You could do 8.

For an "indicator" LED, writing one after the other will be plenty fast enough. Doing digitalWrite is more readable than fooling around with PORT bits.

I can't believe you need two LEDs that are to be viewed by a human to light simultaneously, is there something else at play here?


Rob

Couldn't you just use one, brighter LED and a beam splitter? :stuck_out_tongue_closed_eyes:

Once the pins are set as to mode and you have read the port bits, writing to the port is like writing to a variable -- x = y and the pins change.

But that's when turning one on maybe less than a microsecond before the other isn't good enough!

I've read a port to software-latch simultaneous readings. That wasn't messy.

this sketch works on my mega can anyone see any errors with it ?

/* Blink Turns on an LED This example code is in the public domain. */

void setup() { // initialize the digital pin as an output. // Pin 2/ has an LED connected on most Arduino boards: pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); }

void loop() { digitalWrite(2, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(2, LOW); // set the LED off delay(0); // wait for a second

digitalWrite(3, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(3, LOW); // set the LED off delay(0); // wait for a second

digitalWrite(4, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(4, LOW); // set the LED off delay(0); // wait for a second

digitalWrite(5, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(5, LOW); // set the LED off delay(0); // wait for a second

digitalWrite(6, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(6, LOW); // set the LED off delay(100); // wait for a second

digitalWrite(7, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(7, LOW); // set the LED off delay(100);

digitalWrite(3, HIGH); digitalWrite(6, HIGH); delay(50); digitalWrite(3, LOW); digitalWrite(6, LOW); delay(500); digitalWrite(4, HIGH); digitalWrite(5, HIGH); delay(50); digitalWrite(4, LOW); digitalWrite(5, LOW);

digitalWrite(4, HIGH); delay(100); digitalWrite(4, LOW); delay(100); }

Syntactically it’s correct, does it do what you want it to do? If so it’s OK, however it’s quite badly written.

For example you could use a loop and an array to reduce most of that duplicated code. Here’s a hint

for (int i = 2; i <= 7; i++)
   pinMode(i, OUTPUT);

EDIT: I just noticed this

delay(0);              // wait for a second

Apart from the comment being totally incorrect what is that code supposed to do?

BTW do you still need “simultaneous” LEDs? You haven’t answered any question about that.


Rob

@markthespark

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the # button above the posting area.

I'm a newbie in programming. Just wanna ask if how will I be able to code what I'm doing right now. 2 led that will turn on at the same time let's say a red and yellow LED, after a second the yellow led will turn off while the other one is still on. Then after another second the red led will turn off.