Flashing Multiple LEDs at the Same Time

Advert

Flashing Multiple LEDs at the Same Time

Home Forums Electronics in the Workshop Flashing Multiple LEDs at the Same Time

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #776658
    James Alford
    Participant
      @jamesalford67616

      I though that it would be sensible to start a fresh thread for this, following on from my last one on missing libraries.

      The code posted in this post Sample Code has been a great help and has reminded me how I succeeded in flashing several LEDs at the same time in my old, corrupted, code. I used the same principle, but added variables that were initiated when the correct time was set on the RTC. These variables ensured the the LEDs flashed at the start of each minute, hour and so forth. My current iteration of the code has them flashing once a minute and hour, but not at the start of the period.

      I shall post my progress here.

      James.

      Advert
      #776661
      John Haine
      Participant
        @johnhaine32865

        I’m not sure how the code you linked to does it but one way would be to wire all the LED anodes together and feed them from Vcc via a series switch (transistor).  Then the cathodes would be fed from individual outputs, active low.  You set the appropriate combination of outputs for the pattern you want then pulse the switch on/off to flash.

        #776664
        James Alford
        Participant
          @jamesalford67616
          On John Haine Said:

          I’m not sure how the code you linked to does it but one way would be to wire all the LED anodes together and feed them from Vcc via a series switch (transistor).  Then the cathodes would be fed from individual outputs, active low.  You set the appropriate combination of outputs for the pattern you want then pulse the switch on/off to flash.

          Thank you for the suggestion which I shall have a look at.

          For clarity, I am trying to achieve is flashing one LED one the second, each second, another at the start of the minute and another at the start of the hour. On the hour, I want all three to flash at the same time (all bar the few milliseconds that the code needs to run), rather than one after the other.

          James.

          #776672
          duncan webster 1
          Participant
            @duncanwebster1

            If you drive them all off the same port you can make them all react at exactly the same time by changing the port output. It’s a bit mind blowing to start with, but once the penny has dropped it’s easier than multiple digitalWrite statements. I used it for driving a stepper motor in half step mode

            #776691
            SillyOldDuffer
            Moderator
              @sillyoldduffer
              On duncan webster 1 Said:

              If you drive them all off the same port you can make them all react at exactly the same time by changing the port output. It’s a bit mind blowing to start with, but once the penny has dropped it’s easier than multiple digitalWrite statements. I used it for driving a stepper motor in half step mode

              Good idea, but I’d put it on the back burner for the time being.   Not only is it ‘a bit mind blowing to start with’, but the programmer has to get port code absolutely right, and, unlike digitalWrite(), there is no error checking.  The computer will silently do as it’s told even if that completely screws up other internal functions!    checks for such nightmears, and, though the program won’t work as expected, digitalWrite() doesn’t cause any weird hard to debug side effects.

              Here’s the ATmega328P’s block diagram:

              at328block

              Bottom left is a box labelled ‘Port D (8)’, meaning ‘I am an 8 bit register connected to 8 pins, that do digital Input-Output’.  The pins it services are listed on the arrow below – PD0 to PD7.   Next diagram shows how they relate to physical pins on an Arduino Nano, not always logically – PB5 is D13:

              Arduino-Nano-Pinout-Diagram-Image

               

              The advantage of writing directly to PortD is that eight pins will be switched together simultaneously on the next clock tick. Super-fast and synchronised, wonderful, and the code is simple.

              DDRD = B11111111;  // Set all pins controlled by PORT D to HIGH

              DDRD = B0000000;  // All pins set LOW

              The problem with my DDRD example is that PortD is also connected to Timer/Counter 0, and USART0. (Universal Synchronous/Asynchronous Receiver/Transmitter)   As Arduino Nano Pins PD0 and PD1 are dedicated by Arduino to Serial Transmit and Receive, my DDRD code example collides with a system function.  Eek – don’t do it by accident!   The chip’s other Ports are also shared with other system functions, so proceed with care, and watch out for weird bugs – probably indicate a collision under the bonnet!

              Good explanation of how to read and write ports directly here.

              Excellent technique, but I was James, I’d save it for later.  In his application, I doubt it matters if the second, minute and hour pulses are staggered by a few tens of microseconds.

              Also, when speed matters, the DirectIO library is a good compromise. digitalWrite() wastes a lot of time by checking for conflicts every time it’s called. DirectIO checks at compile time, and doesn’t worry thereafter.    On the downside, DirectIO isn’t quite as bomb-proof, and it uses a C++ template, which is a step harder to understand in the first place than a function.   But easier than writing directly to a port register.

              Dave

            Viewing 5 posts - 1 through 5 (of 5 total)
            • Please log in to reply to this topic. Registering is free and easy using the links on the menu at the top of this page.

            Advert

            Latest Replies

            Viewing 25 topics - 1 through 25 (of 25 total)
            Viewing 25 topics - 1 through 25 (of 25 total)

            View full reply list.

            Advert

            Newsletter Sign-up